Re: plist and AppleScript [WAS: Scripting]
Re: plist and AppleScript [WAS: Scripting]
- Subject: Re: plist and AppleScript [WAS: Scripting]
- From: has <email@hidden>
- Date: Mon, 3 Mar 2003 17:18:20 +0000
Daniel A. Shockley:
I've had some wrestling with PLists and AppleScript (not using
Studio!) lately. I wrote a handler to convert a PList array of
dictionaries into an AppleScript list of records. May come in handy
for those working with PLists.
See also
<
http://www.barple.pwp.blueyonder.co.uk/libraries/index.html#PListLib>.
This can convert a plist string to object model and back.
Manipulating these is pretty similar to working with a scriptable
application's object model; mostly it's just a difference in syntax -
eg:
tell plistObj
setVal("some value") of itemKey("some key")
val() of itemKey("some key") --> "some value"
end tell
instead of the usual:
tell application "Foo"
tell document 1
set value of item "key name" to "some value"
value of item "key name" --> "some value"
end tell
end tell
Code to get you rolling; just modify the 'do stuff' handler to suit
(also requires ASLoader from my site):
======================================================================
----------------------------------------------------------------------
-- load libraries
property ASLoader : load script ((((path to scripts folder from local
[NO-BREAK]domain) as text) & "ASLoader.scpt") as alias) -- boilerplate
property PListLib : missing value
on __load__(loader)
set PListLib to loader's loadLib("PListLib")
end __load__
----------------------------------------------------------------------
-- main code
on run
__load__(ASLoader's makeLoader()) -- boilerplate
--
set plistFile to choose file with prompt "Select plist file:"
set txt to read plistFile
set plistObj to PListLib's parsePList(txt)
doStuffWithPListObj(plistObj)
set txt to PListLib's generatePList(plistObj)
writeFile(plistFile, txt)
end run
on writeFile(fileSpec, txt)
open for access fileSpec with write permission returning fileRef
set eof fileRef to 0
write txt to fileRef
close access fileRef
return
end writeFile
on doStuffWithPListObj(plistObj)
tell plistObj
-- your code goes here - eg:
setVal("blah-blah-blah") of itemKey("foo")
setVal(42) of itemKey("2b") of itemKey("bar")
end tell
end doStuffWithPListObj
======================================================================
Two things to remember:
- Apple provide official APIs for working with plists (which are only
available to ASers via shell scripting or AS-Studio, as others have
said). Any other way (e.g. PListLib): use at own risk, etc.
- PListLib isn't finished, so some functionality isn't there yet
(e.g. ability to handle super-long numbers in plist <integer>
elements; ability to add/remove elements). Again, use at own risk,
etc.
has
--
http://www.barple.pwp.blueyonder.co.uk -- The Little Page of AppleScripts
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.