Guides and Sample Code

Developer

Mac Automation Scripting Guide

On This Page

Working with Property List Files

Many apps store settings in property list files (also called plists). Scripts can also store and retrieve data in plists. The terminology for interacting with plists is found in the Property List Suite of the System Events scripting dictionary (see Figure 35-1).

Figure 35-1Property list terminology in the System Events scripting dictionary image: ../Art/systemevents_dictionary_propertylistsuite_2x.png

Creating a New Property List File

Listing 35-1 demonstrates how to create a new property list file. First, an empty plist file (class property list file) is created. Next, individual property list items (class property list item) of varying type (Boolean, date, list, number, record, string) are added to the file.

APPLESCRIPT

Open in Script Editor

Listing 35-1AppleScript: Creating a property list file
  1. tell application "System Events"
  2. -- Create an empty property list dictionary item
  3. set theParentDictionary to make new property list item with properties {kind:record}
  4. -- Create a new property list file using the empty dictionary list item as contents
  5. set thePropertyListFilePath to "~/Desktop/Example.plist"
  6. set thePropertyListFile to make new property list file with properties {contents:theParentDictionary, name:thePropertyListFilePath}
  7. -- Add a Boolean key
  8. tell property list items of thePropertyListFile
  9. make new property list item at end with properties {kind:boolean, name:"booleanKey", value:true}
  10. -- Add a date key
  11. make new property list item at end with properties {kind:date, name:"dateKey", value:current date}
  12. -- Add a list key
  13. make new property list item at end with properties {kind:list, name:"listKey"}
  14. -- Add a number key
  15. make new property list item at end with properties {kind:number, name:"numberKey", value:5}
  16. -- Add a record/dictionary key
  17. make new property list item at end with properties {kind:record, name:"recordKey"}
  18. -- Add a string key
  19. make new property list item at end with properties {kind:string, name:"stringKey", value:"string value"}
  20. end tell
  21. end tell

Listing 35-2 shows the contents of a property list file created by the script in Listing 35-1.

Listing 35-2Example XML for a property list file created by a script
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  3. <plist version="1.0">
  4. <dict>
  5. <key>booleanKey</key>
  6. <true/>
  7. <key>dateKey</key>
  8. <date>2016-01-28T19:34:13Z</date>
  9. <key>listKey</key>
  10. <array/>
  11. <key>numberKey</key>
  12. <integer>5</integer>
  13. <key>recordKey</key>
  14. <dict/>
  15. <key>stringKey</key>
  16. <string>string value</string>
  17. </dict>
  18. </plist>

Reading a Property List Key Value

Listing 35-3 shows how to read a value of a key in a property list file.

Open in Script Editor

Listing 35-3AppleScript: Reading a key value in a property list file
  1. tell application "System Events"
  2. tell property list file thePropertyListFilePath
  3. return value of property list item "stringKey"
  4. end tell
  5. end tell
  6. --> Result: "string value"

Changing a Property List Key Value

Listing 35-4 shows how to change the value of a key in a property list file.

Open in Script Editor

Listing 35-4AppleScript: Changing the value of a key in a property list file
  1. tell application "System Events"
  2. tell property list file thePropertyListFilePath
  3. set value of property list item "stringKey" to "new string value"
  4. end tell
  5. end tell

Adding a New Property List Item

Listing 35-3 shows how to add a new key and value to a property list file.

Open in Script Editor

Listing 35-5AppleScript: Adding a new key and value to a property list file
  1. tell application "System Events"
  2. tell property list items of property list file thePropertyListFilePath
  3. make new property list item at end with properties {kind:string, name:"newStringKey", value:"new string value"}
  4. end tell
  5. end tell