Question about something in ASObj-C database #1


I copied the text below from the new ASObj-C database. It is an “Extra info” topic I added to the database. It lists all of the script bridging that goes on in ASObj-C that I know about. Do you see anything incorrect or missing from the list?

Bridging in ASObj-C

ASObj-C was released in Mac OS X Snow Leopard (10.6), which came out in 2009.

ASObj-C was released with some support for bridging ASObj-C and AppleScript data types. Bridging refers to process where AppleScript data types and ASObj-C data types are automatically changed from one type to the other automatically. The actual bridging process occurs at runtime when the script is being run.

AppleScript bridged classes, and AppleScript records that are equivalent to the output of ASObj-C objects, can be used for both “input” or “output” that is “given to” or “returned from” the ASObj-C object. For example:

There are ASObj-C classes that can be converted to equivalent AppleScript records.
The ASObj-C operation NSMakeRect(x, y, width, height) will produce the AppleScript record {origin:{x:real, y:real}, |size|:{width:real, height:real}}

The following AppleScript:
set TheRect to current application’s NSRect’s NSMakeRect(10, 20, 100, 200)
will produce the the same result as:
NSIsEmptyRect({origin:{x:10.0, y:20.0}, |size|:{width:100.0, height:200.0}})

There are other data types where a single value in an AppleScript class is replaced by a data type in an ASObj-C class, and vice-versa. The notation “<—>” will be used to indicate this automatic conversion that goes in both directions.

OS X 10.6
NSRange, NSSize, NSPoint, and NSRect were bridged to the AppleScript records:
ASObj-C NSRect <—> {origin:{x:real, y:real}, |size|:{width:real, height:real}}
ASObj-C NSSize <—> {width:real, height:real}
ASObj-C NSPoint <—> {x:real, y:real}
ASObj-C NSRange <—> {location:real, |length|:real

AppleScript string <—> ASObj-C NSString
AppleScript list <—> ASObj-C NSArray
AppleScript record <—> ASObj-C NSDictionary
AppleScript integer <—> ASObj-C int/unsigned int/long/unsigned long/NSInteger/NSUInteger
AppleScript real <—> ASObj-C float/double
AppleScript boolean <—> ASObj-C BOOL

OS X 10.11
Most ASObj-C struct types are bridged to AppleScript records and vice versa.

Lists are also bridged to structs if the elements are in the correct order. Struct types with bitfield members are still not supported.

AppleScript file —> ASObj-C NSURL
AppleScript alias —> ASObj-C NSURL
AppleScript POSIX file —> ASObj-C NSURL
AppleScript alias <— ASObj-C NSURL objects with a scheme of file

AppleScript date <—> ASObj-C NSDate
NOTE: Converting between date and NSDate is inherently imprecise: date has a resolution of exactly one second over its entire range, and is relative to the current time zone; NSDate has a variable resolution (for practical purposes, one to ten microseconds), and is a specific point in time.

Bill Kopp

1 Like

Conversion is automatic in only one direction — you need to use “as” to convert to AppleScript classes.

I think terms like “ASObj-C NSString” confuse things — I think you should just refer to the class name, NSString. You also don’t mention NSNumber as all.

And NSRect conversion to/from a record is broken in 10.13 — you have to use a list of lists.

That should be:

set TheRect to current application's NSMakeRect(10, 20, 100, 200)

But NSMakeRect is rather pointless especially in 10.13. You might just as well say:

set theRect to {{0, 20}, {100, 200}}

They actually convert to «class furl».