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