Use applescript to read plist file

I got a plist file like this form:


<dict>
     <key>items</key>
     <array>
          <dict>
               <key>key</key>
               <string>value</string>
          </dict>
     </array>
</dict>


I want to read value .

And my apple script like this:


tell application "System Events"
  tell property list file "/Users/sun/Desktop/exp.plist"
       set p1 to the value of property list item "items"
       repeat with i from 1 to number of items in p1
            set p2 to the value of property list item "key" of property list item i of p1
            return p2
       end repeat
  end tell
endtell

but i got an error like this :can not get “property list item 1 of {{|key|:"value"}}”。

what can i do to correct this?

Thanks a lot .

Accepted Reply

The problem here is that you an array in the mix. What you’re trying to do here is the equivalent of:

property list item "key" of property list item "items"

and what you want to do is:

property list item "key" of property list item 1 of property list item "items"

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

The problem here is that you an array in the mix. What you’re trying to do here is the equivalent of:

property list item "key" of property list item "items"

and what you want to do is:

property list item "key" of property list item 1 of property list item "items"

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I use a general-purpose handler such as:


getPlistValue from {"/Users/sun/Desktop/exp.plist", "items", 1, "key"} -- example
log result

to getPlistValue from plistItems -- where plistItems is a list of the elements to traverse
  try
    if (count plistItems) < 2 then error "not enough plist items"
    tell application "System Events"
      set theElement to property list file ((first item of plistItems) as text) -- root element
      repeat with anItem in rest of plistItems -- add on the sub items
        try
          set anItem to anItem as integer -- index number?
        end try
        set theElement to (get property list item anItem of theElement)
      end repeat
      return value of theElement
    end tell
  on error errMess number errNum
    log errMess
    error "getPlistValue error:  " & errMess & " (" & errNum & ")" -- pass it on
  end try
end getPlistValue


You can also use some AppleScriptObjC to read the property list into a record.

Thanks a lot.🙂

Tanks ,🙂.Do you mind show me a little example about using AppleScriptObjC to read the property list into a record?

Sure:

use framework "Foundation"
use scripting additions

set fileURL to current application's NSURL's fileURLWithPath:(POSIX path of (choose file of type "com.apple.property-list"))
set fileData to current application's NSData's dataWithContentsOfURL:fileURL
set plist to (current application's NSPropertyListSerialization's propertyListWithData:fileData options:(current application's NSPropertyListMutableContainersAndLeaves) format:(missing value) |error|:(missing value)) as record

Tanks a lot .It helpfu.