A frequent AppleScript task involves saving values from one running of a script to the next. There are a number of options including:
- A text file
- A script library
- A plist file
The text-file option is fairly straightforward, and the script-library alternative is described here. There are several approaches that can be used to save values to a plist file.
The first approach is to use NSUserDefaults, which is the fastest and simplest alternative. It will return a default value and create the plist file if it doesnât exist. This approach works with text, integers, booleans, lists, records, and dates (and their ASObjC equivalents). The following example saves a list.
use framework "Foundation"
use scripting additions
--the preference value is retrieved at the beginning of the script
set thePreference to getPreference()
--the body of the script goes here and typically sets a preference value
set preferenceValue to {"aa", 3}
--the preference value is saved at the end of script
writePreference(preferenceValue)
on getPreference()
set theDefaults to current application's NSUserDefaults's alloc()'s initWithSuiteName:"com.peavine.test"
theDefaults's registerDefaults:{keyOne:{"", 0}} --set default value as desired
return keyOne of theDefaults as list
end getPreference
on writePreference(theValue)
set theDefaults to current application's NSUserDefaults's alloc()'s initWithSuiteName:"com.peavine.test"
set keyOne of theDefaults to theValue
end writePreference
Another approach is to use System Events, although itâs a bit complicated and marginally slower. It will work with the same values as NSUserDefaults.
--write new or overwrite existing key and value (plist must exist)
set thePlist to (path to preferences folder as text) & "com.peavine.test.plist"
set theKey to "keyOne"
set theValue to {"a", 3}
tell application "System Events" to make new property list item at property list file thePlist with properties {kind:list, name:theKey, value:theValue}
--write value of existing key
set thePlist to (path to preferences folder as text) & "com.peavine.test.plist"
set theKey to "keyOne"
set theValue to {"a", 3}
tell application "System Events" to set value of property list item theKey of property list file thePlist to theValue
--read value of a key
set thePlist to (path to preferences folder as text) & "com.peavine.test.plist"
set theKey to "keyOne"
tell application "System Events" to set theValue to value of property list item theKey of property list file thePlist
--create new or overwrite existing plist
set thePlist to (path to preferences folder as text) & "com.peavine.test.plist"
tell application "System Events"
set theDictionary to make new property list item with properties {kind:record}
make new property list file with properties {contents:theDictionary, name:thePlist}
end tell
A final approach is to use the defaults shell command. This is slower yet and is pretty much restricted to text, integers, and booleans. However, it is useful with a shortcut.
--write and read a key-value pair to a plist file
--the plist file will be created if it doesn't exist
--the key needs to be enclosed in double quotation marks if it contains a space
--the value must always be enclosed in single quotation marks
--write and read a string
set thePlist to quoted form of (POSIX path of (path to preferences folder as text) & "com.peavine.test")
set theKey to "keyOne"
set theValue to quoted form of "aa aa"
do shell script "defaults write " & thePlist & space & theKey & " -string " & theValue
set keyOneValue to do shell script "defaults read " & thePlist & space & theKey -->"aa aa"
--write and read an integer
set thePlist to quoted form of (POSIX path of (path to preferences folder as text) & "com.peavine.test")
set theKey to "keyTwo"
set theValue to quoted form of "3"
do shell script "defaults write " & thePlist & space & theKey & " -integer " & theValue
set keyTwoValue to (do shell script "defaults read " & thePlist & space & theKey) as integer -->3
--write and read a boolean
set thePlist to quoted form of (POSIX path of (path to preferences folder as text) & "com.peavine.test")
set theKey to "keyThree"
set theValue to quoted form of "true"
do shell script "defaults write " & thePlist & space & theKey & " -boolean " & theValue
set keyThreeValue to (do shell script "defaults read " & thePlist & space & theKey) as integer as boolean -->true