Property list clippings

I’ve been dealing with property list files a bit lately, so I finally got around to writing some clippings for the job. Unlike the existing plist clippings, these read/write binary format files, as well as reading non-binary format. They’re also better in terms of error handling.

This is the clipping for reading a property list file from disk and converting the result to an AppleScript value (usually a record or list):

[[*framework:Foundation]]set {theData, theError} to [[*class:NSData]]'s dataWithContentsOfFile:[[template:posixPath]] [[*piped:options]]:0 |error|:(reference)
if theData = missing value then error theError's localizedDescription() as text
set {[[linked-template:newValue]], theError} to [[*class:NSPropertyListSerialization]]'s propertyListWithData:theData options:[[*enum:2:NSPropertyListMutableContainersAndLeaves]] |format|:(missing value) |error|:(reference)
if [[linked-template:newValue]] = missing value then error theError's localizedDescription() as text
set [[template:newASValue]] to item 1 of (([[*class:NSArray]]'s arrayWithObject:[[linked-template:newValue]]) as list) -- converts newValue to AS class
[[selected-lines]]

This is for saving a suitable AppleScript (or ASObjC) object to a property list file:

[[*framework:Foundation]]set {theData, theError} to [[*class:NSPropertyListSerialization]]'s dataWithPropertyList:[[template:valueToSave]] |format|:[[*enum:200:NSPropertyListBinaryFormat_v1_0]] [[*piped:options]]:0 |error|:(reference)
if theData = missing value then error theError's localizedDescription() as text
set {theResult, theError} to theData's writeToFile:[[template:posixPath]] [[*piped:options]]:[[*enum:1:NSDataWritingAtomic]] |error|:(reference)
if not theResult as boolean then error theError's localizedDescription() as text
[[selected-lines]]

And this is for when you have a property list in text form, such as the result of call to do shell script:

[[*framework:Foundation]]set [[linked-template:theString]] to [[*class:NSString]]'s stringWithString:[[linked-template:theString]]
set theData to [[linked-template:theString]]'s dataUsingEncoding:[[*enum:4:NSUTF8StringEncoding]] -- convert to NSData
set {[[linked-template:newValue]], theError} to [[*class:NSPropertyListSerialization]]'s propertyListWithData:theData options:[[*enum:2:NSPropertyListMutableContainersAndLeaves]] |format|:(missing value) |error|:(reference)
if [[linked-template:newValue]] = missing value then error theError's localizedDescription() as text
set [[template:newASValue]] to item 1 of (([[*class:NSArray]]'s arrayWithObject:[[linked-template:newValue]]) as list) -- converts newValue to AS class
[[selected-lines]]
2 Likes

Nice, and thanks much for providing these.

Any chance you could provide a clipping for reading a single property from a plist? Reading a large plist and Iterating through it seems less elegant somehow.

A property list file is just a way of taking something like a record, and serializing it to disk; reading one simply reverses the process. The structure of records can vary enormously, so there’s no one way reading a value from one.

The one useful shortcut is if you have cascading properties, where you can use valueforKeyPath:"prop1.prop2[. …]". But I’m not sure that’s worth a clipping.

Hello Shane,

first of all - I am sorry to bump this old thread.
Also many many thanks for providing these Clippings.

There is somewhat unclear to me speaking of this:

  • Do you mean that the third Clipping is for writing Plist Data in Text Form to a Plist File in binary Format ?
  • How would a Clipping look like for reading Plist binary data Format to text using do shell script ?

Greetings from Germany

Tobias

No, it’s for taking the result of a shell command that returns a result in text property list style, and converting it accordingly.

You’d have to convert the contents to data first.

Hello Shane,

thanks for pointing that out - maybe did not understand correctly.
Now this is clear to me.

Since I try to learn something that is new to me - could you please provide an exampe ?
I would apprecheate that.

Greetings from Germany

Tobias

What the shell example you’re thinking of?

I have no idea …
maybe using bash or zsh ? does this help you ?

Greetings from Germany

Tobias

Then I’m not sure what you’re actually asking for.

Hello Shane,

I am asking for a clipping with the shell command that returns a result in text property list style

If there is a way to do this using ASObjC only that would be great to have, too …

Greetings from Germany

Tobias

I think we’re at cross purposes here. Some shell commands do that – I’m really not in a position to trawl through them checking.