• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Accessing Child Elements of Property List Records or Lists
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Accessing Child Elements of Property List Records or Lists


  • Subject: Re: Accessing Child Elements of Property List Records or Lists
  • From: red_menace <email@hidden>
  • Date: Sat, 15 Aug 2009 18:51:52 -0600

Hello

Each property list item can also contain other property list items.  In your example,
you are using the value of one property list item (a record) as the key for getting
another property list item.  You just need to break down each key/subitem - I use
a list of items to build a reference to the desired property list element:

to GetPlistElement from PlistItems
(*
get the specified element from a simple Plist structure by name or index
note that the indexes are not necessarily the order shown in the XML file (?!)
    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]: additional sub item(s)
returns [mixed]: value of the element - null if not found or error
*)
try
tell application "System Events"
set thePlistElement to property list file ((get the 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?
end try
set thePlistElement to (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


For your example, the usage would be  my (GetPlistElement from {thePlist, "Bounds", "Height"})


Dave



On Aug 15, 2009, at 1:00 PM, Grant Erickson wrote:

I am attempting to use the Property List Suite of the System Events
dictionary to customize window views using a property list of attributes for
those customizations (bounds, view style, view options, etc.).

I seem to be able to successfully access all the top-level items from the
property list using:

   on GetObjectForKeyInFile(inFile, inKey)
       tell application "System Events"
           tell property list file inFile
               tell contents
                   return value of property list item inKey
               end tell
           end tell
       end tell
   end GetObjectForKeyInFile

with examples such as:

   set theBoolean to my GetObjectForKeyInFile(thePlist, "Tool Bar")

   if theBoolean is not {} then
           set toolbar visible to theBoolean
   end if

However, when retrieve a dictionary (record) and then get a child of them
it:

   on GetObjectForKeyInRecord(inRecord, inKey)
       tell application "System Events"
           tell property list item inRecord
               return value of property list item inKey
           end tell
       end tell
   end GetObjectForKeyInRecord

with an example such as:

   set theBounds to my GetObjectForKeyInFile(thePlist, "Bounds")
   set theHeight to my GetObjectForKeyInRecord(theBounds, "Height")

I get an error of:

   System Events got an error: Can¹t get property list item {|X|:10,
|Y|:10, |Width|:480, |Height|:320}. Invalid index. (-1719)

Any insights? I am certain there is some "of..." or "as..." syntax I am
missing here; however, various combinations of "as record", etc. have proved
fruitless.

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Accessing Child Elements of Property List Records or Lists
      • From: Grant Erickson <email@hidden>
  • Prev by Date: Accessing Child Elements of Property List Records or Lists
  • Next by Date: Script for mail
  • Previous by thread: Accessing Child Elements of Property List Records or Lists
  • Next by thread: Re: Accessing Child Elements of Property List Records or Lists
  • Index(es):
    • Date
    • Thread