AppleScript Plist System Events

[Plist]AppleShowAllFiles 非表示(不可視)ファイルの表示 非表示の切り替え

コマンド System Eventが実行される毎で 値を保存してくれるので、この手の処理には向いている 構文が短いのもいい

【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions


set strPlistFileName to "com.apple.finder.plist" as text

##############################################
## ファイルパス関連
##############################################
####preferences folderまでのパス
tell application "Finder"
  set aliasPreferencesDir to (path to preferences folder from user domain) as alias
  set strPreferencesDir to POSIX path of aliasPreferencesDir as text
end tell
####ファイル名と繋げてパスにする
set strPlistPath to (strPreferencesDir & strPlistFileName) as text

##############################################
## まずは今の設定を読み込む
##############################################
tell application "System Events"
  tell property list file strPlistPath
    set boolCreateDesktop to value of (property list item "AppleShowAllFiles")
  end tell
end tell
##############################################
## 今の設定内容と逆のことを実行する
##############################################

if boolCreateDesktop is true then
  display dialog "隠しファイル非表示ファイルを『表示しない』に設定変更します" buttons "OK" default button 1
  tell application "System Events"
    tell property list file strPlistPath
      set value of property list item "AppleShowAllFiles" to false
    end tell
  end tell
else
  display dialog "隠しファイル非表示ファイルを『表示する』に設定変更します" buttons "OK" default button 1
  tell application "System Events"
    tell property list file strPlistPath
      set value of property list item "AppleShowAllFiles" to true
    end tell
  end tell
end if
##############################################
## 結果確認
##############################################
tell application "System Events"
  tell property list file strPlistPath
    log value of (property list item "AppleShowAllFiles") as boolean
  end tell
end tell

##############################################
## Finder 再起動
##############################################

try
  tell application "Finder" to quit
end try
delay 2
launch application "Finder"
tell application "Finder" to activate



return

|

[Plist]設定変更の方法3種類(すべてのファイルを表示する AppleShowAllFiles)

コマンドで実行

#!/usr/bin/env osascript

----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

#

#

(*

Finderに不可視ファイルを表示するか?を切り替えます

今:不可視ファイル表示通常表示に切り替え

今:通常表示なら不可視ファイル表示に切り替えます

*)

#

#

# com.cocolog-nifty.quicktimer.icefloe

----+----1----+----2----+-----3----+----4----+----5----+----6----+----7


try

    ---AppleShowAllFilesを調べて

    set numResponce to (do shell script "defaults read  com.apple.finder AppleShowAllFiles") as number

    ---1なら

    if numResponce = 1 then

        ---不可視ファイルを表示しないに変更

        do shell script "defaults write  com.apple.finder AppleShowAllFiles -bool true"

    else

        ---不可視ファイルを表示するに変更

        do shell script "defaults write  com.apple.finder AppleShowAllFiles -bool false"

    end if

on error

    ---エラーが発生したらとりあえず 不可視ファイルを表示しないに変更

    do shell script "defaults write  com.apple.finder AppleShowAllFiles -bool true"

end try

---ファインダーを再起動して表示を確定

do shell script "killall Finder"






システムイベントを使う

#!/usr/bin/env osascript

----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

#

#

#

#

# com.cocolog-nifty.quicktimer.icefloe

----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

##自分環境がos12なので2.8にしているだけです

use AppleScript version "2.8"

use framework "Foundation"

use scripting additions



set strPlistFileName to "com.apple.finder.plist" as text


##############################################

## ファイルパス関連

##############################################

####preferences folderまでのパス

tell application "Finder"

    set aliasPreferencesDir to (path to preferences folder from user domain) as alias

    set strPreferencesDir to POSIX path of aliasPreferencesDir as text

end tell

####ファイル名と繋げてパスにする

set strPlistPath to (strPreferencesDir & strPlistFileName) as text


##############################################

## まずは今の設定を読み込む

##############################################


tell application "System Events"

    tell property list file strPlistPath

        set boolCreateDesktop to value of (property list item "AppleShowAllFiles")

    end tell

end tell

##############################################

## 今の設定内容と逆のことを実行する

##############################################


if boolCreateDesktop is true then

    display dialog "デスクトップのアイコンを隠します" buttons "OK" default button 1

    tell application "System Events"

        tell property list file strPlistPath

            set value of property list item "AppleShowAllFiles" to false

        end tell

    end tell

    

else

    display dialog "デスクトップのアイコンを表示します" buttons "OK" default button 1

    tell application "System Events"

        tell property list file strPlistPath

            set value of property list item "AppleShowAllFiles" to true

        end tell

    end tell

end if

##############################################

## 結果確認

##############################################

tell application "System Events"

    tell property list file strPlistPath

        log value of (property list item "AppleShowAllFiles") as boolean

    end tell

end tell


##############################################

## Finder 再起動

##############################################


try

    tell application "Finder" to quit

end try

delay 2

launch application "Finder"

tell application "Finder" to activate




return




Plistの内容を書き換える

#!/usr/bin/env osascript

----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

#

#

#

#

# com.cocolog-nifty.quicktimer.icefloe

----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

##自分環境がos12なので2.8にしているだけです

use AppleScript version "2.8"

use framework "Foundation"

use scripting additions


property refMe : a reference to current application


set objFileManager to refMe's NSFileManager's defaultManager()



#####設定項目

set strPlistFileName to "com.apple.finder.plist" as text


##############################################

## ファイルパス関連

##############################################

set listUserLibraryPath to (objFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask))

set ocidFilePathURL to item 1 of listUserLibraryPath

####ファイル名を付与したURL

set ocidFilePathURL to ocidFilePathURL's URLByAppendingPathComponent:"Preferences"

set ocidFilePathURL to ocidFilePathURL's URLByAppendingPathComponent:strPlistFileName

##############################################

## 本処理

##############################################

set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0


####ファイル読み込み

set listReadPlistData to refMe's NSMutableDictionary's dictionaryWithContentsOfURL:ocidFilePathURL |error|:(reference)

set ocidPlistDict to item 1 of listReadPlistData


set ocidNSErrorData to item 2 of listReadPlistData

if ocidNSErrorData is not (missing value) then

    doGetErrorData(ocidNSErrorData)

end if



##############################################

## Plistの種類を調べる

##############################################


##set boolBin to (refNSPropertyListSerialization's propertyList:ocidPlistDict isValidForFormat:(refMe's NSPropertyListBinaryFormat_v1_0))

##set boolXML to (refNSPropertyListSerialization's propertyList:ocidPlistDict isValidForFormat:(refMe's NSPropertyListXMLFormat_v1_0))

set boolOpen to (refMe's NSPropertyListSerialization's propertyList:ocidPlistDict isValidForFormat:(refMe's NSPropertyListOpenStepFormat))


if boolOpen is true then

    return "読み込んだデータのフォーマットが不正です-->OPEN STEP形式です"

end if


set ocidNSbplist to refMe's NSPropertyListBinaryFormat_v1_0


##############################################

##現在の値を取得する

##############################################

-->false

set ocidFalse to (refMe's NSNumber's numberWithBool:false)'s boolValue

-->true

set ocidTrue to (refMe's NSNumber's numberWithBool:true)'s boolValue


set boolNumber to ocidPlistDict's valueForKey:"AppleShowAllFiles"


if boolNumber = ocidTrue then

    log "現在の値はTRUE=1です"

else if boolNumber = ocidFalse then

    log "現在の値はFALSE=0です"

else

    log "値を取得できませんでした"

end if

##############################################

##現在の値の逆の値をセットする

##############################################

if boolNumber = ocidTrue then

    log "現在の値はTRUE=1です-->FALSE=0をセットします"

    ocidPlistDict's setObject:ocidFalse forKey:"AppleShowAllFiles"

else if boolNumber = ocidFalse then

    log "現在の値はFALSE=0です-->TRUE=1をセットします"

    ocidPlistDict's setObject:ocidTrue forKey:"AppleShowAllFiles"

else

    log "値を取得できませんでした-->Key を作ってTRUE=1をセットします"

    ocidPlistDict's setObject:ocidTrue forKey:"AppleShowAllFiles"

end if

####変更後の値

log (ocidPlistDict's valueForKey:"AppleShowAllFiles") as boolean


##############################################

## ファイルを書き込みます

##############################################


###書き込み用にバイナリーデータに変換

set ocidPlistEditDataArray to refMe's NSPropertyListSerialization's dataWithPropertyList:ocidPlistDict format:ocidNSbplist options:0 |error|:(reference)

set ocidPlistEditData to item 1 of ocidPlistEditDataArray


set ocidNSErrorData to item 2 of ocidPlistEditDataArray

if ocidNSErrorData is not (missing value) then

    doGetErrorData(ocidNSErrorData)

end if


####書き込み

set boolWritetoUrlArray to ocidPlistEditData's writeToURL:ocidFilePathURL options:0 |error|:(reference)

log item 2 of boolWritetoUrlArray


set ocidNSErrorData to item 2 of boolWritetoUrlArray

if ocidNSErrorData is not (missing value) then

    doGetErrorData(ocidNSErrorData)

end if


##############################################

## Finder再起動

##############################################

set ocidAppList to refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:"com.apple.finder"

if (count of ocidAppList) ≠ 0 then

    ###Finderを取得して

    set ocidAppFinder to ocidAppList's objectAtIndex:0

    ####終了させて

    ocidAppFinder's terminate()

    ###2秒まって

    delay 2

    ###起動させる

    set ocidNewFinderApp to refMe's NSWorkspace's sharedWorkspace()

    ocidNewFinderApp's launchAppWithBundleIdentifier:"com.apple.finder" options:(refMe's NSWorkspaceLaunchDefault) additionalEventParamDescriptor:(missing value) launchIdentifier:(missing value)

else if (count of ocidAppList) = 0 then

    ###Finderが無いなら起動

    set ocidNewFinderApp to refMe's NSWorkspace's sharedWorkspace()

    ocidNewFinderApp's launchAppWithBundleIdentifier:"com.apple.finder" options:(refMe's NSWorkspaceLaunchDefault) additionalEventParamDescriptor:(missing value) launchIdentifier:(missing value)

end if

#############起動確認 10回10秒間

repeat 10 times

    set ocidAppList to refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:"com.apple.finder"

    if (count of ocidAppList) = 0 then

        ###Finderが無いなら起動

        set ocidNewFinderApp to refMe's NSWorkspace's sharedWorkspace()

        ocidNewFinderApp's launchAppWithBundleIdentifier:"com.apple.finder" options:(refMe's NSWorkspaceLaunchDefault) additionalEventParamDescriptor:(missing value) launchIdentifier:(missing value)

    else

        ###あるなら起動確認終了

        exit repeat

    end if

    delay 1

end repeat




##############################################

## エラー発生時のログ用

##############################################



to doGetErrorData(ocidNSErrorData)

    #####個別のエラー情報

    log "エラーコード:" & ocidNSErrorData's code() as text

    log "エラードメイン:" & ocidNSErrorData's domain() as text

    log "Description:" & ocidNSErrorData's localizedDescription() as text

    log "FailureReason:" & ocidNSErrorData's localizedFailureReason() as text

    log ocidNSErrorData's localizedRecoverySuggestion() as text

    log ocidNSErrorData's localizedRecoveryOptions() as text

    log ocidNSErrorData's recoveryAttempter() as text

    log ocidNSErrorData's helpAnchor() as text

    set ocidNSErrorUserInfo to ocidNSErrorData's userInfo()

    set ocidAllValues to ocidNSErrorUserInfo's allValues() as list

    set ocidAllKeys to ocidNSErrorUserInfo's allKeys() as list

    repeat with ocidKeys in ocidAllKeys

        if (ocidKeys as text) is "NSUnderlyingError" then

            log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedDescription() as text

            log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedFailureReason() as text

            log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedRecoverySuggestion() as text

            log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedRecoveryOptions() as text

            log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s recoveryAttempter() as text

            log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s helpAnchor() as text

        else

            ####それ以外の値はそのままテキストで読める

            log (ocidKeys as text) & ": " & (ocidNSErrorUserInfo's valueForKey:ocidKeys) as text

        end if

    end repeat

    

end doGetErrorData

|

[plist]property list に新規項目を作成する


#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions


set strPlistFileName to "com.apple.finder.plist" as text

####preferences folderまでのパス
tell application "Finder"
set aliasPreferencesDir to (path to preferences folder from user domain) as alias
set strPreferencesDir to POSIX path of aliasPreferencesDir as text
end tell
####ファイル名と繋げてパスにする
set strPlistPath to (strPreferencesDir & strPlistFileName) as text
####まずは今の設定を読み込む
try
tell application "System Events"
tell property list file strPlistPath
set boolCreateDesktop to value of (property list item "CreateDesktop")
end tell
end tell
on error
tell application "System Events"
tell property list file strPlistPath

make new property list item with properties {name:"CreateDesktop", value:true}
set value of property list item "CreateDesktop" to true
end tell
end tell
end try

###今の設定内容と逆のことを実行する
if boolCreateDesktop is true then
display dialog "デスクトップのアイコンを隠します" buttons "OK" default button 1
tell application "System Events"
tell property list file strPlistPath
set value of property list item "CreateDesktop" to false
end tell
end tell

else
display dialog "デスクトップのアイコンを表示します" buttons "OK" default button 1
tell application "System Events"
tell property list file strPlistPath
set value of property list item "CreateDesktop" to true
end tell
end tell
end if


####Finder 再起動
try
tell application "Finder" to quit
end try
delay 2
launch application "Finder"
tell application "Finder" to activate

|

[property list]AppleScriptでのplist操作の基本

ダウンロード - property20list20file20sample.zip

↑LINEアプリのplist同封してあります

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

tell application "Finder"
set aliasPathToMe to path to me as alias
set aliasContainerDir to container of aliasPathToMe as alias
end tell
set strContainerDir to POSIX path of aliasContainerDir
set strOutPlistFilePath to ("" & strContainerDir & "sample.plist") as text


###Rootの構造を調べます
tell application "System Events"
tell property list file strOutPlistFilePath
count of (every property list item)
--->plistrootに2つの項目がある事がわかります
kind of property list item 1
--->ひとつはlist=Array
set strListName to name of property list item 1
--->Arrayの名前
########
kind of property list item 2
--->もうひとつはnumbernamevalueがある普通の項目です
name of property list item 2
--->項目の名前
value of property list item 2
--->項目の値
end tell
end tell

####
tell application "System Events"
tell property list file strOutPlistFilePath
tell property list item strListName
--->先に調べたROOTArrayの名前で全てを格納
set strListName to every property list item
--->全ての
set objMyKind to kind as class
--->listならArray recordならDict
set numListCnt to (count of every property list item) as number
--->項目が何個あるか? 2つのArrayがある事がわかります
###
kind of property list item 1
kind of property list item 2
--->両方ともrecord=Dictなのがわかります
set numCntPlist to 1
repeat numListCnt times
tell property list item numCntPlist
name of every property list item
value of every property list item
end tell

set numCntPlist to numCntPlist + 1
end repeat

end tell
end tell
end tell

####################
####なのでどれか1つの値を取る場合はこのようになります
tell application "System Events"
tell property list file strOutPlistFilePath
tell property list item "results"
tell property list item 1
tell property list item "bundleId"
set strUTI to value
end tell
end tell
end tell
end tell
end tell

####################
####値を変更する場合はこのようになります
tell application "System Events"
tell property list file strOutPlistFilePath
tell property list item "results"
tell property list item 2
tell property list item "artistId"
set value to 9999999
end tell
end tell
end tell
end tell
end tell

log strUTI
strUTI

|

[SystemEvent]Plistへの書き込み基本編(デスクトップアイコンの表示・非表示)

Plistへのデータの書き込みです。

ダウンロード - e38386e38299e382b9e382afe38388e38383e38395e3829ae382a2e382a4e382b3e383b3.zip



#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions


set strPlistFileName to "com.apple.finder.plist" as text

####preferences folderまでのパス
tell application "Finder"
set aliasPreferencesDir to (path to preferences folder from user domain) as alias
set strPreferencesDir to POSIX path of aliasPreferencesDir as text
end tell
####ファイル名と繋げてパスにする
set strPlistPath to (strPreferencesDir & strPlistFileName) as text
####まずは今の設定を読み込む
tell application "System Events"
tell property list file strPlistPath
set boolCreateDesktop to value of (property list item "CreateDesktop")
end tell
end tell

###今の設定内容と逆のことを実行する
if boolCreateDesktop is true then
display dialog "デスクトップのアイコンを隠します" buttons "OK" default button 1
tell application "System Events"
tell property list file strPlistPath
set value of property list item "CreateDesktop" to false
end tell
end tell

else
display dialog "デスクトップのアイコンを表示します" buttons "OK" default button 1
tell application "System Events"
tell property list file strPlistPath
set value of property list item "CreateDesktop" to true
end tell
end tell
end if


####Finder 再起動
try
tell application "Finder" to quit
end try
delay 2
launch application "Finder"
tell application "Finder" to activate

|

[SystemEvent]Plistを作成する(Dict構成で検索まで)

Plistの内容を検索するには、Array構成よりDict構成の方がシンプルになる
Array構成の場合どうしてもListを検索するのに『repeat』を使う事になるので
やや複雑になるし、元になるデータの並び順が重要だったりして

ダウンロード - e983bde98193e5ba9ce79c8cdict.zip

Dict構造だと
key(applescript内ではname)で検索できるのは簡単でいい
2022041194806_1280x792
↓『京都府』で検索できる

####選択した語句のリストを取得する
tell application "System Events"
tell property list file "ファイルへのパス"
tell property list item "団体コード"
set strValue to value of (property list item "京都府") as list
end tell
end tell
end tell



#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

tell application "Finder"
set aliasPathToMe to path to me
end tell

set aliasFile to (choose file with prompt "ファイルを選んでください" default location (aliasPathToMe) of type {"public.comma-separated-values-text"} with invisibles and showing package contents without multiple selections allowed) as alias

##########とりあえず空のPlistを作る
tell application "Finder"
set objInfo to info for aliasFile
set strCSVfilePath to (POSIX path of aliasFile) as text
set strOrgName to name of objInfo as text
set strNewPlistName to ("" & strOrgName & ".plist") as text
set aliasCountaierDir to container of aliasFile as alias
set strCountaierDir to (POSIX path of aliasCountaierDir) as text
end tell
set strSaveFilePath to ("" & strCountaierDir & strNewPlistName & "") as text

tell application "System Events"
set objPlist to make new property list file with properties {name:strSaveFilePath}
end tell
###########CSVデータの整形
###CSVを読み込む
set strCommandText to "cat \"" & strCSVfilePath & "\"" as text
set strReadData to (do shell script strCommandText) as text
###改行毎のリストにする
set AppleScript's text item delimiters to "\r"
set listReadData to every text item of strReadData
set AppleScript's text item delimiters to ""
###タイトル行を処理
set strTitleLine to text item 1 of listReadData
set AppleScript's text item delimiters to ","
set listTitleLine to every text item of strTitleLine
set AppleScript's text item delimiters to ""
set numCntTiteleLine to (count of listTitleLine) as number
set numTitleIndexNo to 1
###データ部の処理
set numCntListData to (count of listReadData) as number


###タイトルの数だけ々
repeat (numCntTiteleLine) times
set strTitle to text item numTitleIndexNo of listTitleLine as text
####ここでタイトル部のDictを作成
tell application "System Events"
tell objPlist
set objDict to make new property list item at end with properties {kind:record, name:strTitle}
end tell
end tell
###タイトルがあるので2行目から読み込み
set numDataListNo to 2
repeat (numCntListData - 1) times
###データ部の整形
set strDataLine to text item numDataListNo of listReadData
set AppleScript's text item delimiters to ","
set listDataLine to every text item of strDataLine
set AppleScript's text item delimiters to ""
set strData to text item numTitleIndexNo of listDataLine as text
set strDivName to text item 2 of listDataLine
###ここでDictにデータを追加していきます
tell application "System Events"
tell objPlist
tell objDict
make new property list item at end with properties {kind:string, name:strDivName, value:strData}
end tell
end tell
end tell
###データ部繰り返しのカウントアップ
set numDataListNo to numDataListNo + 1
end repeat
###タイトル部繰り返しのカウントアップ
set numTitleIndexNo to numTitleIndexNo + 1
end repeat

|

[SystemEvent]Plistを作成する(Dict構成)

plistを作ってrootにDictを構成します。

ダウンロード - plistdict.scpt.zip


20220410203534_1280x547



Dictを作成
#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions


##########とりあえず空のPlistを作る
set strCommandText to "echo $HOME/Desktop/PlistText.plist"
set strSaveFilePath to (do shell script strCommandText) as text

tell application "System Events"
set objPlist to make new property list file with properties {name:strSaveFilePath}
end tell
####Dict項目
set listDictTitle to {"団体コード", "団体名", "更新日", "追加日", "サイトのURL1", "サイトのURL2", "サイトのURL3"}

###タイトルの数だけ繰り返し
repeat with objDictTitle in listDictTitle
set strDictTitle to objDictTitle as text
####ここでタイトル部のDictを作成
tell application "System Events"
####ここのtellplistファイル
tell objPlist
###各種DictobjDictとしておく
set objDict to make new property list item at end with properties {kind:record, name:strDictTitle}
end tell
end tell
end repeat



Dictにデータ

ダウンロード - plistdictdata.scpt.zip


20220410211244_1280x796

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions


##########とりあえず空のPlistを作る
set strCommandText to "echo $HOME/Desktop/PlistText.plist"
set strSaveFilePath to (do shell script strCommandText) as text

tell application "System Events"
set objPlist to make new property list file with properties {name:strSaveFilePath}
end tell
####Dict項目
set listDictTitle to {"団体名", "更新日", "追加日", "サイトのURL1", "サイトのURL2", "サイトのURL3"}

###タイトルの数だけ繰り返し
repeat with objDictTitle in listDictTitle
set strDictTitle to objDictTitle as text
####ここでタイトル部のDictを作成
tell application "System Events"
####ここのtellplistファイル
tell objPlist
###各種DictobjDictとしておく
set objDict to make new property list item at end with properties {kind:record, name:strDictTitle}
end tell
end tell
end repeat



###########↑ここまででDictが出来ましたので
###########↓ここからDictにデータ入れていきます
set listDictTitle to {"団体名", "更新日", "追加日", "サイトのURL1", "サイトのURL2", "サイトのURL3"}
###↑この順番に
###↓このデータを入れていきます
set listValue to {"999999", "都道府県名", "20220101", "20221231", "https://hoo.hoge.com", "", ""}

set numChkValueNo to 1
###タイトルの数だけ繰り返し
repeat with objDictTitle in listDictTitle
set strDictTitle to objDictTitle as text
####追加する項目分だけ繰り返し
tell application "System Events"
###plistのファイル
tell objPlist
###plistlist item
tell property list item strDictTitle
###リストからデータを取り出して
set strValue to text item numChkValueNo of listValue
###plistに書き込み
make new property list item at end with properties {kind:string, name:"団体コード", value:strValue}
end tell
end tell
end tell

set numChkValueNo to numChkValueNo + 1
end repeat

|

[SystemEvent]Plistを作成する(Arrayを構成)

20220410195434_1280x5332



Arrayを構成
#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions


##########とりあえず空のPlistを作る
set strCommandText to "echo $HOME/Desktop/PlistText.plist"
set strSaveFilePath to (do shell script strCommandText) as text

tell application "System Events"
set objPlist to make new property list file with properties {name:strSaveFilePath}
end tell
####Array項目
set listArrayTitle to {"追加日", "団体コード", "団体名", "更新日", "サイトのURL1", "サイトのURL2", "サイトのURL3"}

###タイトルの数だけ繰り返し
repeat with objArrayTitle in listArrayTitle
set strArrayTitle to objArrayTitle as text
####ここでタイトル部のArrayを作成
tell application "System Events"
tell objPlist
set objArray to make new property list item at end with properties {kind:list, name:strArrayTitle}
end tell
end tell
end repeat



Arrayにデータを入れる

ダウンロード - plistarraydata.scpt.zip


20220410202640_1280x953

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions


##########とりあえず空のPlistを作る
set strCommandText to "echo $HOME/Desktop/PlistText.plist"
set strSaveFilePath to (do shell script strCommandText) as text

tell application "System Events"
set objPlist to make new property list file with properties {name:strSaveFilePath}
end tell
####Array項目
set listArrayTitle to {"団体コード", "団体名", "更新日", "追加日", "サイトのURL1", "サイトのURL2", "サイトのURL3"}

###タイトルの数だけ繰り返し
repeat with objArrayTitle in listArrayTitle
set strArrayTitle to objArrayTitle as text
####ここでタイトル部のArrayを作成
tell application "System Events"
####ここのtellplistファイル
tell objPlist
###各種ArrayobjArrayとしておく
set objArray to make new property list item at end with properties {kind:list, name:strArrayTitle}
end tell
end tell
end repeat

###########↑ここまででArrayが出来ましたので
###########↓ここからArrayにデータ入れていきます
set listArrayTitle to {"団体コード", "団体名", "更新日", "追加日", "サイトのURL1", "サイトのURL2", "サイトのURL3"}
###↑この順番に
###↓このデータを入れていきます
set listValue to {"999999", "都道府県名", "20220101", "20221231", "https://hoo.hoge.com", "", ""}

set numChkValueNo to 1
###タイトルの数だけ繰り返し
repeat with objArrayTitle in listArrayTitle
set strArrayTitle to objArrayTitle as text
####追加する項目分だけ繰り返し
tell application "System Events"
###plistのファイル
tell objPlist
###plistlist item
tell property list item strArrayTitle
###リストからデータを取り出して
set strValue to text item numChkValueNo of listValue
###plistに書き込み
make new property list item at end with properties {kind:string, value:strValue}
end tell
end tell
end tell

set numChkValueNo to numChkValueNo + 1
end repeat

|

[SystemEvent]Plistを作成する(Arrayを構成で検索まで)

オープンデータ取組済自治体
https://www.digital.go.jp/news/qcm0KZc-/ のカンマ区切りテキストから
plistを作成します。
今回のテーマはplistにArrayを作成して値を追加するです。
行タイトルの『団体コード,団体名,サイトのURL1,サイトのURL2,サイトのURL3,追加日,更新日』で
Arrayを作成して
そこに順番にValueとしての値を入れていきます
各値の順番=検索に使います。

ダウンロード - e983bde98193e5ba9ce79c8c.zip


#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

tell application "Finder"
set aliasPathToMe to path to me
end tell

set aliasFile to (choose file with prompt "ファイルを選んでください" default location (aliasPathToMe) of type {"public.comma-separated-values-text"} with invisibles and showing package contents without multiple selections allowed) as alias

##########とりあえず空のPlistを作る
tell application "Finder"
set objInfo to info for aliasFile
set strCSVfilePath to (POSIX path of aliasFile) as text
set strOrgName to name of objInfo as text
set strNewPlistName to ("" & strOrgName & ".plist") as text
set aliasCountaierDir to container of aliasFile as alias
set strCountaierDir to (POSIX path of aliasCountaierDir) as text
end tell
set strSaveFilePath to ("" & strCountaierDir & strNewPlistName & "") as text

tell application "System Events"
set objPlist to make new property list file with properties {name:strSaveFilePath}
end tell
###########CSVデータの整形
###CSVを読み込む
set strCommandText to "cat \"" & strCSVfilePath & "\"" as text
set strReadData to (do shell script strCommandText) as text
###改行毎のリストにする
set AppleScript's text item delimiters to "\r"
set listReadData to every text item of strReadData
set AppleScript's text item delimiters to ""
###タイトル行を処理
set strTitleLine to text item 1 of listReadData
set AppleScript's text item delimiters to ","
set listTitleLine to every text item of strTitleLine
set AppleScript's text item delimiters to ""
set numCntTiteleLine to (count of listTitleLine) as number
set numTitleIndexNo to 1
###データ部の処理
set numCntListData to (count of listReadData) as number

###タイトルの数だけ々
repeat (numCntTiteleLine) times
set strTitle to text item numTitleIndexNo of listTitleLine as text
####ここでタイトル部のArrayを作成
tell application "System Events"
tell objPlist
set objArray to make new property list item at end with properties {kind:list, name:strTitle}
end tell
end tell
###タイトルがあるので2行目から読み込み
set numDataListNo to 2
###データ部の繰り返し
repeat (numCntListData - 1) times
###データ部の整形
set strDataLine to text item numDataListNo of listReadData
set AppleScript's text item delimiters to ","
set listDataLine to every text item of strDataLine
set AppleScript's text item delimiters to ""
set strData to text item numTitleIndexNo of listDataLine as text
###ここでArrayにデータを追加していきます
tell application "System Events"
tell objPlist
tell objArray
make new property list item at end with properties {kind:string, value:strData}
end tell
end tell
end tell
###データ部繰り返しのカウントアップ
set numDataListNo to numDataListNo + 1
end repeat
###タイトル部繰り返しのカウントアップ
set numTitleIndexNo to numTitleIndexNo + 1
end repeat



検索用サンプル


#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

tell application "Finder"
set aliasPathToMe to path to me
end tell

set aliasFile to (choose file with prompt "PLISTを選んでください" default location (aliasPathToMe) of type {"com.apple.property-list"} without invisibles, showing package contents and multiple selections allowed) as alias

###Plistのファイルパス
tell application "Finder"
set objInfo to info for aliasFile
set strPlistFilePath to (POSIX path of aliasFile) as text
end tell
###RootArrayの名前を取得
tell application "System Events"
tell property list file strPlistFilePath
set listArray to name of (every property list item)
end tell
end tell
###選んでもらいます。
try
set objSerchArrayName to (choose from list listArray with title "検索" with prompt "何で検索しますか?" default items (item 4 of listArray) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed)
on error
log "エラーしました"
return
end try
if objSerchArrayName is false then
return
end if
set theSerchArrayName to (objSerchArrayName) as text

####選択した語句のリストを取得する
tell application "System Events"
tell property list file strPlistFilePath
tell property list item theSerchArrayName
set listValue to value of (every property list item) as list
end tell
end tell
end tell

########検索する語句を選択14番目は神奈川県
set theResponse to text item 14 of listValue
try
set objSerchValue to (choose from list listValue with title "検索" with prompt "選んでください?" default items (item 14 of listValue) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed)
on error
log "エラーしました"
return
end try
if objSerchValue is false then
return
end if
set theSerchValueName to (objSerchValue) as text
#####選択した物が何個目?かを数える
set numSerchID to 1
repeat with objValue in listValue
set strValue to objValue as text
if theSerchValueName is strValue then
exit repeat
end if
set numSerchID to numSerchID + 1
end repeat

#####回答用の団体名のリスト
tell application "System Events"
tell property list file strPlistFilePath
tell property list item "団体名"
set listDivValue to value of (every property list item) as list
end tell
end tell
end tell
#####回答用の団体コードのリスト
tell application "System Events"
tell property list file strPlistFilePath
tell property list item "団体コード"
set listCodeValue to value of (every property list item) as list
end tell
end tell
end tell
###検索結果
set strDivValue to text item numSerchID of listDivValue
set strCodeValue to text item numSerchID of listCodeValue

set theResponse to "団体コード:" & strCodeValue & "団体名:" & strDivValue & ""

set aliasIconPath to POSIX file "/System/Applications/Preview.app/Contents/Resources/AppIcon.icns" as alias

try
set objResponse to (display dialog "検索結果\r団体コード:" & strCodeValue & " 団体名:" & strDivValue & "" with title "検索結果です" default answer theResponse buttons {"OK", "キャンセル"} default button "OK" cancel button "キャンセル" with icon aliasIconPath giving up after 10 without hidden answer)
on error
log "エラーしました"
return
end try
if true is equal to (gave up of objResponse) then
return "時間切れですやりなおしてください"
end if
if "OK" is equal to (button returned of objResponse) then
set theResponse to (text returned of objResponse) as text
else
return "キャンセル"
end if

|

[plist]plistの第一階層のkey一覧を取得する

dictのキーの一覧です。

ダウンロード - property20list20item.scpt.zip



#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

set theURL to "http://www.apple.com"

set aliasFile to (choose file with prompt "ファイルを選んでください" default location (path to preferences folder from user domain) of type {"com.apple.property-list"} with invisibles and showing package contents without multiple selections allowed) as alias

tell application "Finder"

set objInfo to info for aliasFile

end tell

set strFilePath to POSIX path of aliasFile


tell application "System Events"
tell contents of property list file strFilePath
set listDictKey to every property list item
end tell
end tell

set strKeyName to ""

repeat with objDictKey in listDictKey
tell application "System Events"
tell objDictKey
set strKeyName to strKeyName & "\n" & name
end tell
end tell

end repeat


log strKeyName
strKeyName

|

より以前の記事一覧

その他のカテゴリー

Acrobat Acrobat Annotation Acrobat Dialog Acrobat Form Acrobat JS Acrobat Open Acrobat Plugin Acrobat python Acrobat Reader Acrobat Sequ Acrobat Stamps Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin System Information Admin Tools Admin Users Admin Volumes Adobe Apple AppleScript AppleScript Guide AppleScript Accessibility AppleScript AppKit AppleScript Applications AppleScript AppStore AppleScript Archive AppleScript Attributes AppleScript Automator AppleScript AVAsset AppleScript AVconvert AppleScript AVFoundation AppleScript AVURLAsset AppleScript BackUp AppleScript Barcode AppleScript Bash AppleScript Basic AppleScript Basic Path AppleScript Bluetooth AppleScript BOX AppleScript Browser AppleScript Calendar AppleScript CD/DVD AppleScript Choose AppleScript Chrome AppleScript CIImage AppleScript CloudStorage AppleScript Color AppleScript com.apple.LaunchServices.OpenWith AppleScript Console AppleScript Contacts AppleScript CotEditor AppleScript CURL AppleScript current application AppleScript Date&Time AppleScript delimiters AppleScript Desktop AppleScript Device AppleScript Disk AppleScript do shell script AppleScript Dock AppleScript DropBox AppleScript eMail AppleScript Encode Decode AppleScript Error AppleScript EXIFData AppleScript ffmpeg AppleScript File AppleScript Finder AppleScript Firefox AppleScript Folder AppleScript Fonts AppleScript GIF AppleScript HTML AppleScript Icon AppleScript Illustrator AppleScript Image Events AppleScript Image2PDF AppleScript ImageOptim Applescript iWork AppleScript Javascript AppleScript Jedit AppleScript Json AppleScript Label AppleScript List AppleScript locationd AppleScript LRC AppleScript LSSharedFileList AppleScript m3u8 AppleScript MakePDF AppleScript Map AppleScript Math AppleScript Microsoft AppleScript Microsoft Edge AppleScript Microsoft Excel AppleScript Mobileconfig AppleScript Mouse AppleScript Movie AppleScript Music AppleScript NetWork AppleScript Notes AppleScript NSArray AppleScript NSBitmapImageRep AppleScript NSBundle AppleScript NSCFBoolean AppleScript NSCharacterSet AppleScript NSColor AppleScript NSColorList AppleScript NSData AppleScript NSDictionary AppleScript NSError AppleScript NSEvent AppleScript NSFileAttributes AppleScript NSFileManager AppleScript NSFont AppleScript NSFontManager AppleScript NSImage AppleScript NSIndex AppleScript NSKeyedArchiver AppleScript NSKeyedUnarchiver AppleScript NSLocale AppleScript NSMutableArray AppleScript NSMutableDictionary AppleScript NSMutableString AppleScript NSNotFound AppleScript NSNumber AppleScript NSOpenPanel AppleScript NSPasteboard AppleScript NSPDFImageRep AppleScript NSpoint AppleScript NSPredicate AppleScript NSRange AppleScript NSRegularExpression AppleScript NSRunningApplication AppleScript NSScreen AppleScript NSSize AppleScript NSString AppleScript NSStringCompareOptions AppleScript NSTask AppleScript NSTimeZone AppleScript NSURL AppleScript NSURL File AppleScript NSURLBookmark AppleScript NSURLComponents AppleScript NSURLResourceKey AppleScript NSUserDefaults AppleScript NSUUID AppleScript NSView AppleScript NSWorkspace AppleScript Numbers AppleScript OAuth AppleScript ObjC AppleScript OneDrive AppleScript Osax AppleScript PDF AppleScript PDFAnnotationWidget AppleScript PDFContext AppleScript PDFDisplayBox AppleScript PDFDocumentPermissions AppleScript PDFKit AppleScript PDFnUP AppleScript PDFOutline AppleScript Photos AppleScript Photoshop AppleScript Plist System Events AppleScript PostScript AppleScript prefPane AppleScript Preview AppleScript Python AppleScript QR AppleScript QR Decode AppleScript QuickLook AppleScript QuickTime AppleScript record AppleScript Regular Expression AppleScript Reminders AppleScript ReName AppleScript Repeat AppleScript RTF AppleScript Safari AppleScript SaveFile AppleScript ScreenCapture AppleScript ScreenSaver AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript Sort AppleScript Sound AppleScript Spotlight AppleScript SRT AppleScript StandardAdditions AppleScript stringByApplyingTransform AppleScript System Events AppleScript System Settings AppleScript TemporaryItems AppleScript Terminal AppleScript Text AppleScript Text CSV AppleScript Text TSV AppleScript TextEdit AppleScript Translate AppleScript Trash AppleScript Twitter AppleScript UI AppleScript Unit Conversion AppleScript UTType AppleScript valueForKeyPath AppleScript Video AppleScript VisionKit AppleScript Visual Studio Code AppleScript webarchive AppleScript webp AppleScript Wifi AppleScript XML AppleScript XML EPUB AppleScript XML OPML AppleScript XML Plist AppleScript XML RSS AppleScript XML SVG AppleScript XML TTML AppleScript XML webloc AppleScript YouTube AppleScript Zero Suppress Applications Barcode OnLine Tools CityCode Design iPhone List Logs lsappinfo Memo Music perl PlistBuddy pluginkit postalcode ReadMe SF Symbols character id SF Symbols Entity sips Skype Slack sqlite TCC Tools Typography Video Wacom zoom