On Jul 14, 2009, at 5:45 PM, OSullivan, Steven wrote: I am trying to make a small AppleScript Studio application (using XCode 3.1.2 on OS X 10.5.7) that will open a .plist, display the one string member within the .plist file in a Label text file, and allow the user to click a button that will replace the string member of the .plist with a string that was entered into a text field on the interface. Please note that this is not a question about AppleScript Studio per se, but about how to manipulate .plist files using System Events through AppleScript. Thanks your consideration! I am trying to learn how to use "System Events" to do this, and am not having any real luck finding documentation on a reliable syntax to open the .plist and read the element into a variable, and then how to take the content of a text field, and overwrite the value in the string element of the .plist file.
I commented on this in 2007 and 2008. So, I looked into my archive and edited my comments to bring them up to date. Here's the result ...
A Quick Tutorial ...
You will need a copy of "Property List Editor" to read the files you create. It's in Developer Tools.
AVOID all knowledge about XML. You don't need it. It adds confusion. When you open a .plist file with Property List Editor AVOID any use of the Dump button. It shows you the XML and adds to the confusion.
There is no command in System Events to create a new .plist file. (But, I have a handler to do that. See below.)
Reading --------------- -- fileAlias is an alias to the .plist file tell application "System Events" fileAlias as text set fileRecord to (value of property list file the result) end tell
'fileRecord' is now a record whose value is the root record of the file. The entire file, in other words.
You can also read a single key from a .plist file, provided you know it exists. For example ... tell application "System Events" fileAlias as text value of property list item "someKey" of property list file the result end tell
The result can be a String, Dictionary, Array, Number, Boolean, Date, Data. (where String = Unicode Text, Dictionary = Record, Array = List)
Writing --------------- You can write the entire contents of the .plist file ... tell application "System Events" fileAlias as text set (value of property list file the result) to fileRecord end tell
or you can write the value of single key, if it exists ... tell application "System Events" fileAlias as text set (value of property list item "someKey" of property list file the result) to someValue end tell
Modifying --------------- When you read a .plist file into the record 'fileRecord', you may want to write it back to the file after modifying or updating it. For example ...
set fileRecord to {|newKey|: newValue} & fileRecord & {|defaultKey|: defaultValue}
1. fileRecord may or may not have had a 'newKey', but it now does have a 'newKey' which has value 'newValue'. 2. If fileRecord did not have a 'defaultKey', it now does have a 'defaultKey' and it value is 'defaultValue', but if fileRecord did have a 'defaultKey', it retains its original value. 3. Using || around key names avoids conflict with variable names in your AppleScript code.
And here's how to create a new .plist file ----------------- In this example, I am creating a new preference file for an application. 'prefsFolder' is an alias to the user's /Library/Preferences/ folder and 'prefsName' is the name of the new preference file.
set posixPath to quoted form of (POSIX path of prefsFolder) do shell script "defaults write " & posixPath & prefsName & " x y" -- creates the new property list file with dummy value tell application "Finder" set prefsFile to (file (prefsName & ".plist") of prefsFolder) as alias end tell tell application "System Events" prefsFile as text set value of property list file the result to defaultPrefsRecord end tell
Hope this helps. Ask if more needed.
|