A p-list tutorial
Home Documentation Smile XML and property lists A p-list tutorial  
This short tutorial presents the main commands of the plist Suite with simple examples, which use a local file on your machine. We shall use a preferences file managed by AddressBook.

You can simply read the tutorial below, and at any moment you can copy the scripts to your machine and test them for good. To import the scripts, click the link below.

The tutorial is intended for displaying a personal message to the user, using the information that the user provided in AddressBook, and which is stored in a p-list file in the Preferences folder.

We shall start by opening the p-list.
set the_folder to (path to preferences from user domain) as text
set the_file to (the_folder & "AddressBookMe.plist") as alias
set the_plist to PlistOpen the_file
The result returned in the Console (something like «data CFob0000000100000000») is an opaque reference. If you are curious to see what it contains, PlistGetXML produces the string representation. Here we work with a very small p-list:
PlistGetXML the_plist
  --  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE plist PUBLIC "-//Apple Computer etc.
When you open a p-list, the root element may be of any type. Usually it is a "dict" (a list of key-value pairs) but it may be an "array" of "dict" (a list of several lists of key-value pairs) and in principle it could as well be any elementary type such as integer. Thus the first thing is to know what kind of element we are facing.
There is no use storing the type in a variable. Rather, we just run the line below and check the result in the Console.
PlistType the_plist
The Console says "dictionary": a list of key-value pairs. This means that we can collect the list of all the keys in this dictionary, in order to choose what we shall display to the user.
Still, there is no use storing the list of the keys: having them printed in the Console is enough.
PlistGetKeys the_plist
-- {"CountryName", "ExistingEmailAddress", "FirstName", "StreetAddr1", "LastName",etc.}
Let us choose three keys out of the list printed in the Console: FirstName, LastName and City.
set the_FName to PlistGet (PlistChild the_plist key "FirstName")
set the_LName to PlistGet (PlistChild the_plist key "LastName")
set the_City to PlistGet (PlistChild the_plist key "City")
Now we can use the strings to build up a message to the user. But why not close the p-list first? We do not need it any longer.
PlistClose the_plist
Now we can proceed with the message.
set the_msg to "Hello " & the_FName & " " & the_LName & "! "
set the_msg to the_msg & "I wish you a good day in " & the_City & "."
Copyright ©2008 Paris, Satimage