[Pythonmac-SIG] Applescript

has hengist.podd at virgin.net
Wed Jan 7 11:26:14 EST 2004


Alex Martelli wrote:

>>>Can anybody suggests a good tutorial/with/examples
>>>for me to learn to use Python for OSA/AppleScript tasks?
>>
>>I understand your apprehension about learning AppleScript, and maybe as
>>a Python luminary that makes sense for you.  But for me, learning at
>
>Well, it's not so much "apprehension", but rather a case of there 
>being SO many important things to learn, that one englishy-syntax, 
>proprietary, non-cross-platform, apparently-feeblish scripting 
>language, per se, wasn't high on my to-do list.  However, studying 
>it may still well be the best approach, of course.

The big problems with learning AppleScript are 1. its incredibly 
muddled semantics and syntax, 2. the difficulty of finding really 
well-written example code, 3. the drudgery of learning its myriad 
bugs and the various workarounds for them.

The first is a fundamental fault of AppleScript's design and 
implementation. The second a natural consequence of having a non-CS 
literate userbase. While waiting for Apple to rectify the third is 
like watching paint drying... only over years, not months.;p [1]


Now, I'm optimistic that sometime this year MacPython will become a 
viable - if not compelling(!) - alternative to AppleScript for the 
majority of IAC tasks. The work is already underway, and roughly 
breaks down as: 1. implementing a high-level interface to Apple Event 
Manager; 2. writing a guide to application scripting (introduction, 
tutorials, examples) that's geared specifically towards Python users; 
3. implementing decent OSA support[2].

As far as doing IAC today goes, none of the Python IAC solutions 
quite match AppleScript's level of support yet; for example, you 
can't currently control remote apps or apps without creator codes 
(e.g. Address Book). And right now the only literature available on 
application scripting is written around the AppleScript language. So 
if you're brand new to application scripting then you'll probably 
fare better if you grit your teeth, haul out the crusty old 
AppleScript Language Guide [3]:

<file://localhost/Developer/Documentation/Carbon/interapplicationcomm/AppleScript/AppleScriptLangGuide/index.html>

and start off with the simple stuff in AppleScript for now. Finder 
scripting, while it has some bugs, is reasonably good. Tex-Edit 
Plus's scripting support is very good indeed and about as close to 
canonical as they get. OTOH, Apple's iApps are pretty notorious; if 
you want to script these, learn how good scripting should work first 
before attempting to grasp the ugly realities of controlling apps 
whose designers don't seem to understand IAC either.


BTW, one other thing: it'll save you a LOT of mental anguish if you 
realise from the start that application objects are referred to using 
basic relational queries [4]. Most folk don't realise this, 
especially since AppleScript uses syntax that looks much like 
standard OO [5]. I posted a message to the MACSCRPT list in the last 
couple of days where I've talked about this issue; I do recommend you 
check it out. Here's the archives; look for the 'Perl (and Python)' 
thread:

<http://listserv.dartmouth.edu/scripts/wa.exe?A0=macscrpt&D=1&H=0&O=D&T=1>

HTH

has


[1] The AppleScript engineers _are_ working on this... tho' some of 
these bugs are so old they'll soon be getting telegrams from the 
Queen...;)

[2] Most of #3 should be straightforward, but I think attachability 
will present a significant problem as the Cocoa API only provides 
support for the AppleScript component. (Which biases attachable Cocoa 
apps against any language whose names does not start with "Apple".) 
But this can be addressed at the time.

[3] For the basics of IAC, the chapters on Commands and Objects and 
References are the ones you probably want.

[4] e.g. From 
<file://localhost/Developer/Documentation/Cocoa/TasksAndConcepts/ProgrammingTopics/Scriptability/Concepts/ScriptabilityTerms.html>:

"An attribute of an object is a one-to-one relationship of a certain 
type. It is synonymous with the AppleScript concept of a property of 
an object. For example, color is an attribute of a graphic shape, but 
a document is not an attribute of a window, even though the window 
has only one associated document. This use of the term attribute is 
not related to the Apple event attribute defined above.

"The term relationship is used to describe an object's one-to-many 
relationships or its non-attribute one-to-one relationships. For 
example, a paragraph has a one-to-many relationship with its words, 
but a a one-to-one relationship with its document (though the 
document is not an attribute of the paragraph). Traditional 
AppleScript terminology refers to each object contained in another 
object as an element (words are elements of a paragraph)."

Also see the ASLG for discussion of the different reference forms 
that are supported.


[5] Including some application designers. So don't be surprised when 
some apps refuse to handle commands and references as expected; often 
as not is cos their scripting support just sucks. e.g.:

    tell application "iTunes"
       delete (every file track of every playlist of every source 
whose location is missing value)
    end tell

ought to delete all tracks whose original audio file is missing. 
Alas, in practice it doesn't even come close to working correctly, 
and you'll have to use a workaround like the one Bob provides as one 
of his aeve examples.
-- 
http://freespace.virgin.net/hamish.sanderson/



More information about the Pythonmac-SIG mailing list