Section B.5. Python


B.5. Python

Hamish Sanderson's Appscript brings AppleScript terminology to Python. It comes with a standard installer package; double-click and run the installer and you're good to go. Scripts must be run using pythonw, not plain python. Here's the code for generating our model events:

 from appscript import * bb = app('BBEdit') bb.make(new=k.document) bb.documents[1].text.set('Hello, world!')

No glue is necessary; like JavaScript OSA, Appscript reads the dictionary and constitutes an appropriate application object behind the scenes. It does this very quickly, and it's even faster if you run the supplied background application AppscriptTerminologyServer. The Python code is structured in a compact, intuitive, highly legible manner, incorporating all the best features of UserTalk and JavaScript; this is partly due to Python itself and partly due to Appscript's clean and thoughtful implementation. Appscript is also easy to learn. An included tool, htmldoc.py, generates an HTML-formatted display of an application's dictionary with the Python version of its terminology. (Alternatively, you can use the HTMLDictionary standalone application, included with the installer, which does the same thing.) And a particularly nice extra is an interactive help command for exploring either the terminology or the live attributes of an application's objects. So, for example, within the interactive pythonw interpreter:

 >>> bb.documents[1].help('-s') ================================================================================ Appscript Help (-s)   Reference: app(u'/Volumes/gromit/Users/matt2/extra/BBEdit 8/BBEdit.app').documents[1]   -------------------------------------------------------------------------------- Current state of referenced object(s)   --- Get reference ---   app(u'/Volumes/gromit/Users/matt2/extra/BBEdit 8/BBEdit.app').text_documents.first   ---- Properties ----   properties: {k.contents: u'Hello, world!',  k.ID_: 7038242,  k.file: k.MissingValue,  k.modifiable: True,  k.state_modified: False,  k.name: u'untitled text 3',  k.modification_date: datetime.datetime(2005, 9, 22, 12, 51, 51),  k.source_language: '',  k.FTP_info: k.MissingValue,  k.line_breaks: k.Unix,  k.container: None,  k.text: u'Hello, world!', ...

And so on. This is like a command-line version of Script Debugger, and greatly eases the trial and error required to develop a script. Appscript clearly attempts to equal and even surpass AppleScript as an environment for creating and sending Apple events. In my view, it succeeds admirably; it is extremely complete and consistent, and takes care of all the heavy lifting for you.




AppleScript. The Definitive Guide
AppleScript: The Definitive Guide, 2nd Edition
ISBN: 0596102119
EAN: 2147483647
Year: 2006
Pages: 267
Authors: Matt Neuburg

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net