Generate NSDictionary from AppleScript variable

Hi,

Usually, I use the command to get Dictionary from a plist file:

set xmlData to (current application’s NSDictionary’s dictionaryWithContentsOfFile:pPathXMLPosix)

But I need to set my variable xmlData from an AppleScript variable that contains the entire pList file and after use the standard objectForKey command to extract value from Dictionary.

How I can generate the Cocoa Dict from AS variable?

Stefano - Ame

Model: MacBook Pro
Browser: Safari 604.5.6
Operating System: Mac OS X (10.13 Developer Beta 3)

You should read property lists something like this:

set theData to current application's NSData's dataWithContentsOfFile:thePath
-- convert to Cocoa object
set {theDict, theError} to current application's NSPropertyListSerialization's propertyListWithData:theData options:(current application's NSPropertyListMutableContainersAndLeaves) |format|:(missing value) |error|:(reference)
if theDict is missing value then error (theError's localizedDescription() as text)

Hi Shane thank you for your input.
I changed something in your script.
In reality, I read the plist crypted from a http web page that contains privileges and use access for myApps based on macAddress, registered in the plist, of users.
After I decode using openSSL and base64 commands and my variable contains “as string/text” the entire plist file like if I read it from disk using AS standard read command.

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

set rawXML to do shell script "curl http://mywebsite.com/abilitazioni.plist.enc"

-- Decode the file because is encrypted using OpenSSL...

-- Convert AppleScript string to NSString
set cocoaString to current application's NSString's stringWithString:rawXML

-- Convert NSString to NSData
set cocoaData to cocoaString's dataUsingEncoding:(current application's NSUTF8StringEncoding)

-- Create pList from NSData
set {xmlData, theError} to current application's NSPropertyListSerialization's propertyListWithData:cocoaData options:(current application's NSPropertyListMutableContainersAndLeaves) format:(missing value) |error|:(reference)

-- OR
-- set xmlData to current application's NSPropertyListSerialization's propertyListFromData:theData mutabilityOption:(current application's NSPropertyListImmutable) format:(missing value) errorDescription:(missing value)

I have two questions:

I found googling a second command similar to your for generate Dictionary starting from NSData.
What are the differences? (propertyListWithData vs propertyListFromData…)?

When I decode using AS and shell script the crypted file in memory at the end in AS all should be in UTF-16 right?
Example when I read using AppleScript a file written as UTF-8 using read as UTF-8 at the end in memory all is in UTF-16?
So when I convert to NSData I need to use UTF16String Encoding?

Thanks in advance and for point me in the right direction

Stefano - Ame

The latter is deprecated; the one I used is a replacement for propertyListFromData::::.

What’s in memory doesn’t matter. You should use UTF8StringEncoding because property lists use UTF8StringEncoding.

Hi Shane,

Thanks a lot :wink: