cinap_lenrekadd hg and python4bb9e2215 years agoHistory
101 lines · 4.0 KB · TeX
RawDownload
\chapter{MacPython OSA Modules \label{scripting}} This chapter describes the current implementation of the Open ScriptingArchitecure (OSA, also commonly referred to as AppleScript) for Python, allowingyou to control scriptable applications from your Python program,and with a fairly pythonic interface. Development on this set of moduleshas stopped, and a replacement is expected for Python 2.5. For a description of the various components of AppleScript and OSA, andto get an understanding of the architecture and terminology, you shouldread Apple's documentation. The "Applescript Language Guide" explainsthe conceptual model and the terminology, and documents the standardsuite. The "Open Scripting Architecture" document explains how to useOSA from an application programmers point of view. In the Apple HelpViewer these books are located in the Developer Documentation, CoreTechnologies section.  As an example of scripting an application, the following piece ofAppleScript will get the name of the frontmost \program{Finder} windowand print it:	\begin{verbatim}tell application "Finder"    get name of window 1end tell\end{verbatim} In Python, the following code fragment will do the same: \begin{verbatim}import Finder f = Finder.Finder()print f.get(f.window(1).name)\end{verbatim} As distributed the Python library includes packages that implement thestandard suites, plus packages that interface to a small number ofcommon applications. To send AppleEvents to an application you must first create the Pythonpackage interfacing to the terminology of the application (what\program{Script Editor} calls the "Dictionary"). This can be done fromwithin the \program{PythonIDE} or by running the\file{gensuitemodule.py} module as a standalone program from the commandline. The generated output is a package with a number of modules, one forevery suite used in the program plus an \module{__init__} module to glueit all together. The Python inheritance graph follows the AppleScriptinheritance graph, so if a program's dictionary specifies that itincludes support for the Standard Suite, but extends one or two verbswith extra arguments then the output suite will contain a module\module{Standard_Suite} that imports and re-exports everything from\module{StdSuites.Standard_Suite} but overrides the methods that haveextra functionality. The output of \module{gensuitemodule} is prettyreadable, and contains the documentation that was in the originalAppleScript dictionary in Python docstrings, so reading it is a goodsource of documentation. The output package implements a main class with the same name as thepackage which contains all the AppleScript verbs as methods, with thedirect object as the first argument and all optional parameters askeyword arguments. AppleScript classes are also implemented as Pythonclasses, as are comparisons and all the other thingies. The mainPython class implementing the verbs also allows access to the propertiesand elements declared in the AppleScript class "application". In thecurrent release that is as far as the object orientation goes, soin the example above we need to use\code{f.get(f.window(1).name)} instead of the more Pythonic\code{f.window(1).name.get()}.  If an AppleScript identifier is not a Python identifier the name ismangled according to a small number of rules:\begin{itemize}    \item spaces are replaced with underscores    \item other non-alphanumeric characters are replaced with    \code{_xx_} where \code{xx} is the hexadecimal character value    \item any Python reserved word gets an underscore appended\end{itemize} Python also has support for creating scriptable applicationsin Python, butThe following modules are relevant to MacPython AppleScript support: \localmoduletable In addition, support modules have been pre-generated for\module{Finder}, \module{Terminal}, \module{Explorer},\module{Netscape}, \module{CodeWarrior}, \module{SystemEvents} and\module{StdSuites}. \input{libgensuitemodule}\input{libaetools}\input{libaepack}\input{libaetypes}\input{libminiae}