Thursday, October 10, 2013

AppleScript

I find myself often developing on Mac OS X. Given all the new technologies and programming languages available, it is sometimes easy to forget about AppleScript, a scripting language that has been available for over twenty years, and reads and writes a bit like "plain English". AppleScript can be run from the command-line using the osascript tool, and edited and run using the "AppleScript Editor" application.

Wouldn't it be cool if we could run AppleScripts from Factor?

Using the NSAppleScript Class Reference, we can see that, basically, we need to create an NSAppleScript initialized from a source string, and then execute the script:

USING: cocoa cocoa.application cocoa.classes kernel ;

: run-apple-script ( str -- )
    [ NSAppleScript -> alloc ] dip
    <NSString> -> initWithSource: -> autorelease
    f -> executeAndReturnError: drop ;

Right now we are ignoring the return values, but supposedly we could figure out how to interpret the return value (which is a NSAppleEventDescriptor) and pass values back from the script to Factor.

You can see that it works by trying to display a simple dialog:

If you're curious to see what else you can do with AppleScript, check out Doug's AppleScripts for iTunes or the MacScripter.net forum.

This is available in the development version of Factor.

No comments:

Post a Comment