[Pythonmac-SIG] hi, everybody!

Bob Ippolito bob at redivi.com
Mon Jan 5 07:58:12 EST 2004


On Jan 5, 2004, at 6:49 AM, Alex Martelli wrote:

> Hi there, folks -- I just joined this mailing list.  I'm quite 
> experienced
> with Python (and other programming languages and technologies, and
> Unix-y systems in general, including BSD dialects), but a total newbie
> to the Mac, having just bought my very first ever for Christmas (12" 
> iBook
> G4 -- I think I've installed all current SW and upgrades, incl. OSX 
> 10.3.2,
> the latest Apple developer tools, fink, Apple's X11 1.0, TclTkAqua,
> MacPython and everything in the standard package db, etc, etc).  I'm
> slowly starting to learn about the dazzling arrays of new-to-me
> technologies.  Can anybody suggests a good tutorial/with/examples
> for me to learn to use Python for OSA/AppleScript tasks?  I'd rather
> learn as little as possible about AppleScript itself as a language, but
> I assume I'll have to learn & understand at least something about
> "AppleEvents" and the like.  Thanks!

Welcome to the community, Alex.  It's good to see that yet another 
large contributor to the Python (and PyPy) community has been tempted 
;)

There's (no less than) four ways to do OSA/AppleScript tasks via Python
	- low level and raw (this would mostly be in the Carbon.AE / 
Carbon.AppleEvents pair, it's abstracted by the other three)
	- via the standard library gensuitemodule stuff (to be deprecated 
soonish in favor of AppScripting and/or aeve)
	- AppScripting : 
http://freespace.virgin.net/hamish.sanderson/appscripting.html
	- aeve : http://undefined.org/python/#aeve

The current version of AppScripting probably supports more applications 
and has better documentation than the current version of aeve, however 
I have a new version of aeve to be released Very Soon Now (this month, 
probably 1-2 weeks) that's significantly different (at least 
internally) from what's out there now.  Your best bet is to get 
AppScripting and/or aeve and plug away.  If you run into any problems 
with any approach, it is appropriate to ask this list or the module 
author, but any python-on-the-mac question is welcome here.

You will need a familiarity with Script Editor 
(/Applications/AppleScript/Script Editor), particularly the "Open 
Dictionary" command, and some of the AppleScript syntax.  The biggest 
thing is that AppleScript "evaluates" from right to left (the name of 
the first playlist of the first library) rather than Python's left to 
right (library.first.playlist.first.name).  AppleScript is also 
extremely lazy about evaluation and does a lot of type adaptation 
behind the scenes, which can cause some impedance mismatch with Python.

Apple Events are a kind of RPC used to pass around "serialized 
objects".  Typically Apple Events are passed around between processes 
on the same machine, but the mechanisms exist to do it over a network 
(but are turned off by default in OS X for security reasons).  These 
serialized objects have a target (a process id, basically), a command 
(get, set, play track, quit, open, new, etc), and arguments to a 
command.  The target (or runtime) will pass back a serialized object 
(an error, list, nothing, integer, string, reference, etc) as a result. 
  All of this is strongly typed and carries around a simple form of 
RTTI.  In the Apple world, the run time typing information associated 
with this serialization in the form of a four character code such as 
'8BIM' or 'hook'.  They are literally 
four-chars-as-a-little-endian-32bit-integer, and generally stick to the 
ASCII range (but sometimes have a \x00 or two).  The kind of objects 
you can pass around are pretty rich, and are a mix of value objects 
(strings, integers, lists, etc) and reference objects ("the desktop" -- 
a property reference of the Application subclass, if you're talking to 
Finder).

Apple Events are not particularly human readable, but AppleScript is.  
It is highly likely that an application that has worthwhile scripting 
support has a "terminology" resource, which is sort of like a C header 
file full of #define's.  They map human-readable names and 
documentation (including additional type information) to four character 
codes, but there is no guarantee that the terminology is actually 
correct (though they often are).  These terminologies and an extremely 
simple runtime are basically what AppleScript is, and the three high 
level Apple Event wrappers for Python (gensuitemodule, AppScripting, 
aeve) consist primarily of code that tries to locate, decode, and 
translate these terminologies into something that can be easily used 
from Python.  Fortunately, all this said, you probably don't need to 
know much of anything about Apple Events to take advantage of them as a 
client of scriptable applications.

In addition to the standard PackageManager database (maintained by 
Jack), there is a separately maintained (by me) "experimental" package 
database at http://undefined.org/python/pimp/ -- which has some 
software that is not in the standard database or has non-standard 
compilation options (such as Numeric compiled against Apple's vecLib, 
which means Altivec acceleration for some operations on that shiny new 
G4).

You should also take a good look at PyObjC ( http://pyobjc.sf.net/ ), 
which is the runtime bridge between Objective C and Python, that lets 
you rather seamlessly take advantage of just about any Objective C 
framework with the convenience of Python.  This means that you can 
write Python applications using Apple's flagship Cocoa UI framework, 
and even use Interface Builder to build the GUIs.

-bob
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2357 bytes
Desc: not available
Url : http://mail.python.org/pipermail/pythonmac-sig/attachments/20040105/3af74cf0/smime-0001.bin


More information about the Pythonmac-SIG mailing list