February 24, 2008

appscript’s get syntax (advanced concepts)

Filed under: Uncategorized — Ryan Wilcox @ 12:22 am

So if you read my previous article on appscript’s get syntax, you might now be wondering how to do more advanced things with getters. For example, in the Finder with Applescript you can say:

tell app "Finder" to get selection

if you want the alias (and not a reference to the finder item you’re dealing with):

tell app "Finder" to get selection as alias

So how do you do this in appscript? Read on…


Technically what I describe below works for any command you issue via appscript, not just get(), but for simplicity of example I’ll just keep using get().

To get an alias from the Finder’s selection:


fndr = app("Finder")
sel = fndr.selection() #get the selection (a list, technically)
item1Alias = sel[0].get(resulttype=k.alias) #get item 1 as alias
print item1Alias

The line item1Alias = sel[0].get(resulttype=k.alias) is the main point, so it requires further examination.

Firstly, item1Alias = sel[0] gets a reference to the first item of the selection list. Even at this point, however, the first item of the selection is still a reference. (In Applescript, differentiating between references and the values of those references is taken care of automatically. Not so in appscript, which is why there’s get() and its kin in appscript in the first place).

Secondly, .get(resulttype=. get() (and all three variations) can take some specific “labeled parameters” (more technically called keyword arguments in Python, which is what I’ll use from now on)

  • resulttype: the type of data you want returned from the expression: (synonymous to Applescript’s “as ….” expression)
  • timeout: the number of seconds to wait until the command times out (synonymous to Applescript’s “with timeout of … seconds clause.)
  • waitreply: a boolean if appscript should wait around for a result, or just return right after a command is issued to the app (synonymous to Applescript’s “ignoring application responses” clause)

If you wanted to go crazy, you could:
item1Alias = sel[0].get(resulttype=k.alias, timeout=1) #get item 1 as alias in 1 second or timeout

Thirdly, and lastly: k.alias). The k is appscript’s “keyword” namespace… where all the “keywords” live. (I disagree with this description a little: really it contains syntax for all the possible types you could need, not keywords, but it’s named how it’s named.)

If you’ve installed appscript on your machine, description of this namespace (and some common types) is found in Chapter 6 of Appscript’s documentation. The rule with the more recent versions of appscript is that it’s of the form k.type, instead of older versions’ k.Type.

Disclaimer: I’m reasonably sure the syntax (like the names of the keyword arguments for get(), and “k.type vs k.Type”) is still correct with the most recent versions of appscript. This is one part of appscript’s syntax that has seen a lot of change in the past. I’m using appscript 0.18.1. Your version of appscript can be easily found out by opening an interactive Python session (aka: typing python on the command line) and going:

Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
....
>>> import appscript
>>> print appscript.__version__
'0.18.1'

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment