on run -- example
set plistFile to (choose file without invisibles)
set theValue to (getPlistElement from {plistFile, "actions", 2, "action", "ActionParameters", 1})
if result is not null then try -- show the element
if (class of theValue) is in {list, record} then (theValue as number) -- force an error
display alert "value:" message (theValue as text)
on error errmess -- lists and records don't format very well, so cheat
set {here, there} to {(offset of "{" in errmess), -(offset of "}" in (reverse of text items of errmess) as text)}
display alert "value:" message text here thru there of errmess
end try
end run
to getPlistElement from plistItems
(*
get the specified element from a Plist structure by name or index
note that the elements are not necessarily in the order shown by the Property List Editor
the number of items is not fixed, but must be at least 2 (the Plist file and a Plist element)
parameters: plistItems [list] -
item 1 [mixed]: the Plist file path (Finder or POSIX)
item 2 [mixed]: the Plist element name or index (names are case sensitive)
item(s) 3+ [mixed]: sub item(s)
returns [mixed]: contents of the element, null if not found or error
*)
try
if (count plistItems) is less than 2 then error "getPlistElement handler: item list contains too few items"
tell application "System Events"
set thePlistElement to property list file ((first item of plistItems) as text) -- start at the root element
repeat with anItem in rest of plistItems -- add on the sub items
set anItem to the contents of anItem
try
set anItem to anItem as integer -- index number? (indexes start at 1)
end try
set thePlistElement to (get property list item anItem of thePlistElement)
end repeat
return value of thePlistElement
end tell
on error errorMessage number errorNumber
log errorMessage
return null -- error "getPlistElement handler: element not found (" & ErrorNumber & ")"
end try
end getPlistElement
to setPlistElement from plistItems to someValue
(*
set the specified (existing) element of a Plist structure (by name or index) to someValue
note that the elements are not necessarily in the order shown by the Property List Editor
the number of items is not fixed, but must be at least 2 (the Plist file and a Plist element)
parameters: plistItems [list] -
item 1 [mixed]: the Plist file path (Finder or POSIX)
item 2 [mixed]: the Plist element name or index (names are case sensitive)
item(s) 3+ [mixed]: sub item(s)
someValue [mixed]: the value to set
returns [boolean]: true if successful, false if not
*)
try
if (count plistItems) is less than 2 then error "getPlistElement handler: item list contains too few items"
tell application "System Events"
set thePlistElement to property list file ((first item of plistItems) as text) -- start at the root element
repeat with anItem in rest of plistItems -- add on the sub items
set anItem to the contents of anItem
try
set anItem to anItem as integer -- index number? (indexes start at 1)
end try
set thePlistElement to (get property list item anItem of thePlistElement)
end repeat
set value of thePlistElement to someValue
set property list file ((first item of plistItems) as text) to thePlistElement -- update plist file
return true
end tell
on error errorMessage number errorNumber
log errorMessage
return false -- error "getPlistElement handler: element not found (" & ErrorNumber & ")"
end try
end setPlistElement