2 Replies Latest reply: Jan 18, 2009 7:36 PM by red_menace
Rob Riley1 Level 1 Level 1 (15 points)
Hi,

I'm building an app that relies heavily on reading plist files. I've got the basics down using System Events, for example:

tell application "System Events"
set plistFile to "/Library/Preferences/com.domain.prefs.plist"
set returnVal to (value of property list item "MyValue" of property list file plistFile)
display dialog returnVal
end tell

To extract from the following plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>MyValue</key>
<string>1000</string>
</dict>
</plist>

But of course I need to read further into plist files, count the number of re-occurrences of certain items and so on. For example, if I wanted to read into the following plist file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>MyValue</key>
<key>ArrayInfo</key>
<array>
<dict>
<key>Bool</key>
<true/>
<key>Value1</key>
<string>1000</string>>
</dict>
<dict>
<key>Bool</key>
<true/>
<key>Value1</key>
<string>4000</string>>
</dict>
</array>
</dict>
</plist>

How can I extract, for example, the number of records within the array and then create an AS list of each re-occurrence of 'Value1' or 'bool'?

Any help on this or any links to good tutorials will be much appreciated.

Many thanks.

MacBook Pro, Mac OS X (10.5.6)
  • Camelot Level 8 Level 8 (46,395 points)
    If you read a property list item that is an array you get a list of items returned to your script. From there you can iterate through your items as required.

    For example, in your .plist, you'd have to fetch property list item "ArrayInfo"

    From there you'd get a list with two items, so you'd have to iterate through that:

    repeat with eachArrayItem in property list item "ArrayInfo"


    Now you have each item in the array and can query the low level values.
  • red_menace Level 6 Level 6 (14,845 points)
    Satimage also has a XMLLib osax with terminology to handle property lists, for example:

    set TheOpenPlist to PlistOpen (choose file)
    repeat with x in items of (PlistGet TheOpenPlist key "ArrayInfo")
          log x
    end repeat
    PlistClose TheOpenPlist


    There is documentation and some examples here.