October 31, 2007

appscript’s get syntax

Filed under: General Information — Ryan Wilcox @ 11:26 am

I use appscript – a Python module letting me talk to scriptable applications on the Mac – everywhere I can. It means I can write less in the verbose, cranky, sometimes read-only AppleScript.

Appscript is nice, but in earlier versions seemed very… verbose. Where AppleScript did all kinds of magic behind the scenes, appscript made you do a lot of that stuff yourself.

Today I was working on getting an object’s reference’s value from appscript. In Applescript this is easy:


tell app "Microsoft Word" to get selection's selection start

(Maybe Word isn’t the best example of this, but it’s what I’m working in now…)

In appscript I’ve always struggled with how to get and set values in appscript. Not today, since I’ve discovered there are three ways of doing it (shown here from the Python Interpreter, one of the great tools for appscript: piece together things line by line interactively!)


>>> msw = app("Microsoft Word")
>>> msw.selection.selection_start.get()
48
>>> msw.selection.selection_start()
48
>>> msw.get( msw.selection.selection_start )
48

The first get() call is the one I’ve been using since early in appscript’s development. Entire reference (the subject), than the verb. It’s very OOP smelling, OBJECT then VERB. You see this everywhere, including Python.

The second get() call is one I didn’t know about until I read the appscript manual just today: once you have a reference, call it like a function and you’ll get its value. That’s a great shortcut, and feels somewhat OOP too.

The third get() call is one I just discovered, and probably the most familiar to AppleScript programmers. Much of Applescript works on VERB then OBJECT, because it could be considered not a class based object oriented programming language. So here we are: GET the selection object’s selection start).

It’ll be a toss-up for me whither I’ll use the second or the third form of the “get idiom”, but I’ve always found the first to be too literal, personally. I’m glad there are other forms.

Oh, and PS: appscript has had addressing by creator code, bundle identifier, path, as well as name and eppc URL since very early in its development. Thanks for catching up, Applescript. <nudge />

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment