scripting os x with applescript, without applescript

23
Reprocessed.org » Scripting OS X with Applescript, without Applescript.

Upload: matt-patterson

Post on 26-May-2015

2.527 views

Category:

Technology


3 download

DESCRIPTION

A brief tour around Applescript and the Ruby-OSA bridge rb-appscript

TRANSCRIPT

Page 1: Scripting OS X with Applescript, without Applescript

Reprocessed.org »

Scripting OS X with Applescript, without Applescript.

Page 2: Scripting OS X with Applescript, without Applescript

Reprocessed.org »

With Ruby instead

Page 3: Scripting OS X with Applescript, without Applescript

Reprocessed.org »

Hello, my name is Matt

Page 4: Scripting OS X with Applescript, without Applescript

Reprocessed.org »

Why Applescript isn’t Applescript

Page 5: Scripting OS X with Applescript, without Applescript

Reprocessed.org »

• Applescript is Apple’s system-level inter-process scripting system

• It’s actually made of two parts:

• Applescript, the language

• OSA – Open Scripting Architecture – which is the plumbing

• Generally, when people ask, ‘is it Applescriptable?’, they actually mean, ‘does it accept Apple Events and expose a terminology dictionary for OSA clients to use?’ Clearly, the first is shorter.

Page 6: Scripting OS X with Applescript, without Applescript

Reprocessed.org »

• Applescript looks a bit like this:

tell application "Finder" empty the trashend tell

• All pretty simple, right? Not quite. Check these two radically different lines out:

get words 1 thru 4 of "now is the winter of our discontent"

get text from word 1 to 4 of "now is the winter of our discontent"

Page 7: Scripting OS X with Applescript, without Applescript

Reprocessed.org »

Sort of like an API

Page 8: Scripting OS X with Applescript, without Applescript

Reprocessed.org »

• You get a way to stitch all sorts of things and apps on your Mac together in all sorts of interesting ways

• This is so useful that the publishing industry relies on Applescript to tie together apps and processes into the editorial workflow stuff needed to get magazines and newspapers out the door.

• You can do the stitching together of lots of separate things in a single script

Page 9: Scripting OS X with Applescript, without Applescript

Reprocessed.org »

Actually, mostly like IPC

Page 10: Scripting OS X with Applescript, without Applescript

Reprocessed.org »

• Under the hood, its OSA which makes all this possible. OSA allows apps to send Apple Events to each other, and defines standard ways to interpret the results, so you can send data types around the place

• When you iterate across things in a script, you’re not really iterating across things in memory: Script Editor (or whatever script running OSA runtime you’re using) is firing Apple Events around the place and interpreting the results

Page 11: Scripting OS X with Applescript, without Applescript

Reprocessed.org »

It’s a query

Page 12: Scripting OS X with Applescript, without Applescript

Reprocessed.org »

• What’s actually going on is more like SQL...

• So, lets have a look...

Page 13: Scripting OS X with Applescript, without Applescript

Reprocessed.org »

Demo time...

Page 14: Scripting OS X with Applescript, without Applescript

Reprocessed.org »

The english-likeness monster

Page 15: Scripting OS X with Applescript, without Applescript

Reprocessed.org »

• As we’ve already seen, the english-likeness of Applescript the language gets complex. Particularly for programmers.

• Multi-word tokens are just plain odd, and allowing apps and scripting additions to essentially redefine parts of the language syntax doesn’t help much

• John Gruber goes into this at great length here: http://daringfireball.net/2005/09/englishlikeness_monster

• So, is there a way to access the good stuff without the gut-wrenching terror?

Page 16: Scripting OS X with Applescript, without Applescript

Reprocessed.org »

Enter rb-appscript / Watch OSA surface from the depths

Page 17: Scripting OS X with Applescript, without Applescript

Reprocessed.org »

• Since Applescript is layered on top of OSA, in theory you could just use a different OSA runtime.

• For once, theory ≈ practice...

• There are low-level Apple Event modules for most dynamic languages, and for Python and Ruby, Hamish Sanderson has given us Appscript, which gives us a nice abstraction layer over the guts which actually makes use of those terminology dictionaries...

• See http://appscript.sourceforge.net/ for the full story

Page 18: Scripting OS X with Applescript, without Applescript

Reprocessed.org »

• Appscript takes away the pain of english-likeness by giving us a consistent interface and making a lot of the voodoo implicit stuff explicit

• Believe me, you’ll appreciate this.

• Appscript code looks a bit like this:

app('TextEdit').documents['Read Me']. paragraphs[1].get()

• As opposed to:

tell application "TextEdit" get paragraph 1 of document "Read Me"end tell

Page 19: Scripting OS X with Applescript, without Applescript

Reprocessed.org »

Munging phone numbers in Address Book

Page 20: Scripting OS X with Applescript, without Applescript

Reprocessed.org »

• The critical bits:

Appscript::app('Address Book'). app.people.phones.get()=> [a, bunch, of, number, objects]

number = phone.value.get()

phone.value.set(number)

Page 21: Scripting OS X with Applescript, without Applescript

Reprocessed.org »

Demo time...

Page 22: Scripting OS X with Applescript, without Applescript

Reprocessed.org »

Questions?

Page 23: Scripting OS X with Applescript, without Applescript

Reprocessed.org »

Thank you

[email protected]://reprocessed.org/

Some code from Matt Neuberg’s excellent Applescript: The Definitive Guide, published by O’Reilly