Conversion of NSSet objects to Applescript lists

It’s time for me to get up with the times. I dread the tweaks that will be necessary in the 100+ utility handlers that are at the core of my automations. But when it’s over, I’m sure it will be delightful.

From an ASObjC point of view 10.11 or later is more reliable, and the bridging of dates is a great leap forward.

I can’t test this at the moment, but I wonder if something else is involved. Let me explain…

In your previous example:

set {theResult, isFile} to (anItem's getResourceValue:(reference) forKey:theKey |error|:(missing value))

the call to getResourceValue:forKey: returns an AppleScript list like this:

{true, (NSNumber) YES}

That’s because the AppleScript bridge has sees the method returns a boolean, and therefore converts theResult to an AppleScript boolean, but it has no way of telling what class the key should be, so it passes the Objective-C value unconverted as an NSNumber.

However, here:

the bridge knows that the isKindOfClass: method returns a boolean, so it again does the conversion. (Early versions of ASObjC didn’t always do this conversion of booleans, so a coercion was a safe fallback.)

That suggest to me that you’re forcing an unnecessary conversion of AsObjCTrue each time. I’m surprised that that would be quicker than using “as boolean”, given you already have a boolean.

In any event, it should be quickest to do neither, but change:

if (its isKindOfClass:(my ||'s class "NSArray")) is AsObjCTrue then

to:

if (its isKindOfClass:(my ||'s class "NSArray")) then

How did I not pay attention to the fact that the returned value is already boolean !!!

My script just returned faster an erroneous result. :frowning: So, I just removed it.

I was making efforts to improve the speed of using data retrieved from an API using Applescript objC and tried the handler above and had 2 issues I need some advice with:

  1. When adding
property || : current application

to the beginning of the code and testing in Script Debugger it ended up making the run time many times slower. Is there any issue with leaving it out?

  1. The data I am trying to convert to AppleScript record is a NSDictionary containing NSArrays.

if I try:


set resultAPI to my cocoaToASValue(aRESTres)

the handler reports an error “No result was returned from some part of this expression.”

if I try:


set resultAPI to my cocoaToASValue(aRESTres) as record
set resultData to my cocoaToASValue(resultAPI)

the coercion takes forever and is pointless since it is already an AppleScript value.

Is there a faster method someone could recommend for converting NSDictionary containing NSArrays?

This is making me feel very stupid.

Trying to gain an understanding of NSSet and testing with the code as suggested:-

set || to current application
set mySet to ||'s NSSet's setWithArray:{1, "two", 3.3}
set myArray to mySet's allObjects()
set myList to myArray as list

But even the very simple:-

set mySet to current application's NSSet's setWithArray:{1, "two", 3.3}

just returns an error in Script Editor or Script Debugger:-

“NSSet doesn’t understand the “setWithArray_” message.” number -1708 from NSSet

I must be missing something very obvious as it all looks right to me and was copied directly from above.

So, a bit stumped. Anyone?

Hi UKenGB.

Most of the examples above are lazy excerpts from other scripts. To work, scripts using ASObjC must begin with declaration(s) of the system framework(s) which provide their classes and methods.

use framework "Foundation"

set || to current application
set mySet to ||'s NSSet's setWithArray:{1, "two", 3.3}
set myArray to mySet's allObjects()
set myList to myArray as list

Doh! Thanks. Got it working now.

wrong thread…