Manipulating plist files on OS X - easy saving of prefs
Ian Wood
revlist at azurevision.co.uk
Sun May 11 07:11:29 EDT 2008
I've been having another look at storing preferences for Rev apps,
this time for a Mac-only one (guess which one starting with AA...).
I remembered that Rev automatically makes a plist file in the user's
preferences folder, and it occurred to me that it would make a lot of
sense to use this file... ;-)
Then I remembered the 'defaults' read and write command...
function ijwPList_Write tDomain, tKey, tString
put "defaults write" && tDomain && quote & tkey & quote && "'" &
tString & "'" into tShell
return shell(tShell)
end ijwPList_Write
function ijwPlist_Read tDomain, tKey
put "defaults read" && tDomain && quote & tKey & quote into tShell
return shell(tShell)
end ijwPlist_Read
These two functions allow you to read and write pretty well any info
from and to any plist preferences file - no need to deal with text
files or XML or anything.
Note 1 - the write command will also add new keys to a plist file.
Note 2 - 'tDomain' is the name of the file minus the extension,
usually taking the form 'com.companyname.appname', or for a specific
example, 'com.apple.iTunes'. Multiple periods can also be used -
'com.apple.iWork.Keynote'.
OS X 10.5 defaults to storing most plist prefs files as some form of
binary that isn't very pretty, but the built-in plutil command line
app will convert them back into a regular XML file if required:
function ijwPlist_convertToXML tPath
-- tpath must be a Terminal-safe path
put "plutil -convert" && quote & "xml1" & quote && tPath into
tShell
return shell(tShell)
end ijwPlist_convertToXML
Hope people find this useful, and have a good time in Las Vegas!
Ian
P.S. Only tested on 10.5, but should (in theory) be fine back to 10.3.
More information about the use-livecode
mailing list