Differences between revisions 4 and 5
Revision 4 as of 2007-03-23 12:55:14
Size: 4464
Comment:
Revision 5 as of 2007-05-03 14:56:58
Size: 4641
Editor: client-82-2-143-188
Comment: updated OSA component info
Deletions are marked like this. Additions are marked like this.
Line 23: Line 23:
There have been several attempts to develop a Python OSA language component, though to date none of them provide a complete replacement for !AppleScript. There have been several attempts to develop a Python OSA language component. The two options currently available are:

 * [http://appscript.sourceforge.net/pyosa.html PyOSA] implements the majority of the OSA interface, including support for sending and receiving Apple events. The Python interpreter runs within the host process, providing good performance at the cost of limited insulation between client scripts. PyOSA is currently under development so some features may be unfinished or subject to change.
Line 25: Line 27:
 * OSAPython implements much of the OSA interface, but is unfinished and is no longer being developed.
 
 * MacPythonOSA is another attempt to implement the full OSA interface, but is also unfinished and currently inactive.
 
 * PythonOSA provides a working implementation of the core OSA interface, allowing OSA scripts written in Python to be loaded, stored, compiled and executed. More advanced OSA features, such as the ability to send and receive !AppleEvents to and from the host process are not currently available.
 * [http://homepage.mac.com/philip_aker/osa/osa.html PythonOSA] allows OSA scripts written in Python to be loaded, stored, compiled and executed as separate subprocesses. Only the core OSA interface is implemented; more advanced OSA features, such as the ability to send and receive !AppleEvents to and from the host process are not available.
Line 41: Line 39:
 * PyObjC-based applications can leverage the /AppKit framework's built-in Cocoa Scripting support to implement a full !AppleEventObjectModel.  * PyObjC-based applications can leverage the ../AppKit framework's built-in Cocoa Scripting support to implement a full !AppleEventObjectModel.

What is AppleScript?

AppleScript is a scripting language developed by Apple, included as standard in Mac OS (System 7 Pro and later) and Mac OS X. It has two important technical features:

  • It can communicate with local and remote processes via ../AppleEvents; commonly known as "application scripting".

  • It is an ["../OSA"] language component, allowing client applications to load and run scripts via the language agnostic OSA API. Client applications can use the OSA API to implement features such as Folder Actions (System Events), Mail rules, iCal alarm scripts, OSA script editing (Script Editor, Script Debugger), etc.

Equivalent Python Features

Python provides extensive third-party application scripting support and limited OSA language component support. It also supports AppleEvent handling and can load and use other OSA language components.

Application scripting

Python has long supported sending AppleEvents via the high-level aetools and gensuitemodule modules in its standard library. However, these modules have always had a number of shortcomings and have grown increasingly troublesome in recent Mac OS X releases; in particular, they are completely broken on Intel-based Macs. As a result, these and other AE/OSA-related modules will be removed in a future Python release and their use should be avoided.

There is also a low-level extension, Carbon.AE, that can be used to construct and send AppleEvents. Using this API requires detailed knowledge of the Apple Event Manager, however, so is rarely used directly.

A modern replacement to aetools and gensuitemodule, the ../AppscriptModule, has been available since late 2003. (A second project, aeve, has since been discontinued.)

Python OSA language components

There have been several attempts to develop a Python OSA language component. The two options currently available are:

  • [http://appscript.sourceforge.net/pyosa.html PyOSA] implements the majority of the OSA interface, including support for sending and receiving Apple events. The Python interpreter runs within the host process, providing good performance at the cost of limited insulation between client scripts. PyOSA is currently under development so some features may be unfinished or subject to change.

  • [http://homepage.mac.com/philip_aker/osa/osa.html PythonOSA] allows OSA scripts written in Python to be loaded, stored, compiled and executed as separate subprocesses. Only the core OSA interface is implemented; more advanced OSA features, such as the ability to send and receive AppleEvents to and from the host process are not available.

Apple event handling

The Python standard library has long provided a basic AppleEvent handing framework, MiniAEFrame, but as with aetools and gensuitemodule this module is unsupported on Intel Macs and will be removed in future, and its use should be avoided.

The low-level Carbon.AE extension can be used to install Apple event handlers, though lacks the ability to install coercion handlers and requires detailed knowledge of the ../AppleEventManager to use, so is rarely used directly.

There are two modern, high-level options for implementing AppleEvent handling in Python-based applications:

  • aemreceive (bundled with the AppscriptModule) can be used to install AppleEvent handlers although it provides no assistance for resolving object references so is best suited for use in applications that don't implement an ../AppleEventObjectModel.

  • PyObjC-based applications can leverage the ../AppKit framework's built-in Cocoa Scripting support to implement a full AppleEventObjectModel.

OSA API access

There are currently two ways to access the OSA API in Python:

  • The CarbonX.OSA extension (bundled with the AppscriptModule) provides a low-level wrapper around the OSA API. (There is also a Carbon.OSA extension included in Python 2.4 and later, but it provides only a partial implementation and contains several bugs so is best avoided.) A high-level wrapper for CarbonX.OSA is under development.

  • PyObjC includes a wrapper for the (currently undocumented) OSAKit API on OS 10.4 and later.

See also

MacPython/AppleScript (last edited 2011-03-08 10:14:53 by 82)

Unable to edit the page? See the FrontPage for instructions.