Convert record to NSDictionary

Shane.

On pages 43 of “Everyday AppleScriptObjC 3.4” the script C9-1 the roundTrip() handler shows an example of handling 2 records. The first record got both items in the list while the second record showed up as empty. I got the same results when typed the same way as the book. When i added 4 pipes it behaved differently.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use framework "Foundation"

set Record1 to {firstName:"Martin", lastName:"Rohde"}
set Record2 to {|button returned|:"OK", |text returned|:"Hello"}

set Result1 to current application's NSDictionary's dictionaryWithDictionary:Record1 --> (NSDictionary) {firstName:"Martin", lastName:"Rohde"}
set Result2 to current application's NSDictionary's dictionaryWithDictionary:Record2 --> (NSDictionary) {button returned:"OK", text returned:"Hello"}

When I added the pipes both records showed up in the output of roundTrip().
That is to say in the second line in the script, that sets the value of Record2, I changed button returned to |button returned| and text returned to |text returned| and both record showed up while with no added pipes record2 contents vanished. A few lines below the handler the book says “The scripting bridge cannot handle such record entries.” Am I missing something or does this actually work. I am still running El Capitan.

The script returned:
(NSDictionary) {
button returned:“OK”,
text returned:“Hello”
}

The line before the last line returned:
(NSDictionary) {
firstName:“Martin”,
lastName:“Rohde”
}

Bill

Adding pipes as you did changes the record into something completely different – you’re no longer using the scripting addition terms button returned and text returned, but user identifiers that contain spaces. You can see this clearly by the different styling they receive when compiled. Pipes around AppleScript variables are just an escaping mechanism so they can contain otherwise illegal characters, in this case a space, a bit like putting quotes around values in shell scripts. The characters themselves don’t get converted.

The point of the example is to show that only user defined values round-trip, and that’s what you have shown.

1 Like

Thanks Shane. I was missing something. I asked the wrong question. I should of asked is this in any real way useful :slight_smile:. I really didn’t think that through well enough. It makes total sense once you explained it. Once I put the pipes around “button returned” and “text returned” I changed what the object was. The dictionary would have different keys and values, and the AS records would have different field specifiers and different field values. In a real functioning script that would break a lot of things.

But on the up side I did add yet another proof to the point you were making in your book. I’m glad I could do that for your book :slight_smile:

Bill