blob: f0fb092ef6034eff08899ef98e943d1b166b7d3f [file] [log] [blame]
Jack Jansenbae5c962003-04-11 15:35:28 +00001\chapter{MacPython OSA Modules \label{scripting}}
2
3Python has a fairly complete implementation of the Open Scripting
4Architecure (OSA, also commonly referred to as AppleScript), allowing
5you to control scriptable applications from your Python program,
6and with a fairly pythonic interface.
7
8For a description of the various components of AppleScript and OSA, and
9to get an understanding of the architecture and terminology, you should
10read Apple's documentation. The "Applescript Language Guide" explains
11the conceptual model and the terminology, and documents the standard
12suite. The "Open Scripting Architecture" document explains how to use
13OSA from an application programmers point of view. In the Apple Help
14Viewer these book sare located in the Developer Documentation, Core
15Technologies section.
16
17
18As an example of scripting an application, the following piece of
19AppleScript will get the name of the frontmost \program{Finder} window
20and print it:
21
22\begin{verbatim}
23tell application "Finder"
24 get name of window 1
25end tell
26\end{verbatim}
27
28In Python, the following code fragment will do the same:
29
30\begin{verbatim}
31import Finder
32
33f = Finder.Finder()
Jack Jansen097da0d2003-06-13 14:59:26 +000034print f.get(f.window(1).name)
Jack Jansenbae5c962003-04-11 15:35:28 +000035\end{verbatim}
36
37As distributed the Python library includes packages that implement the
38standard suites, plus packages that interface to a small number of
39common applications.
40
41To send AppleEvents to an application you must first create the Python
42package interfacing to the terminology of the application (what
43\program{Script Editor} calls the "Dictionary"). This can be done from
44within the \program{PythonIDE} or by running the
45\file{gensuitemodule.py} module as a standalone program from the command
46line.
47
48The generated output is a package with a number of modules, one for
49every suite used in the program plus an \module{__init__} module to glue
50it all together. The Python inheritance graph follows the AppleScript
51inheritance graph, so if a programs dictionary specifies that it
52includes support for the Standard Suite, but extends one or two verbs
53with extra arguments then the output suite will contain a module
54\module{Standard_Suite} that imports and re-exports everything from
55\module{StdSuites.Standard_Suite} but overrides the methods that have
56extra functionality. The output of \module{gensuitemodule} is pretty
57readable, and contains the documentation that was in the original
58AppleScript dictionary in Python docstrings, so reading it is a good
59source of documentation.
60
61The output package implements a main class with the same name as the
62package which contains all the AppleScript verbs as methods, with the
63direct object as the first argument and all optional parameters as
64keyword arguments. AppleScript classes are also implemented as Python
65classes, as are comparisons and all the other thingies.
66
Jack Jansen097da0d2003-06-13 14:59:26 +000067The main
68Python class implementing the verbs also allows access to the properties
69and elements declared in the AppleScript class "application". In the
70current release that is as far as the object orientation goes, so
71in the example above we need to use
Neal Norwitz1ecbd672003-12-14 15:02:54 +000072\code{f.get(f.window(1).name)} instead of the more Pythonic
Jack Jansenbae5c962003-04-11 15:35:28 +000073\code{f.window(1).name.get()}.
74
75
76If an AppleScript identifier is not a Python identifier the name is
77mangled according to a small number of rules:
78\begin{itemize}
79 \item spaces are replaced with underscores
80 \item other non-alphanumeric characters are replaced with
81 \code{_xx_} where \code{xx} is the hexadecimal character value
82 \item any Python reserved word gets an underscore appended
83\end{itemize}
84
85Python also has support for creating scriptable applications
86in Python, but
87The following modules are relevant to MacPython AppleScript support:
88
89\localmoduletable
90
91In addition, support modules have been pre-generated for
92\module{Finder}, \module{Terminal}, \module{Explorer},
93\module{Netscape}, \module{CodeWarrior}, \module{SystemEvents} and
94\module{StdSuites}.
95
96\input{libgensuitemodule}
97\input{libaetools}
98\input{libaepack}
99\input{libaetypes}
100\input{libminiae}