blob: 54e62f2f55e99565df58eb09a3948514d4b94e15 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2.. _mac-scripting:
3
4*********************
5MacPython OSA Modules
6*********************
7
8This chapter describes the current implementation of the Open Scripting
Benjamin Peterson90f36732008-07-12 20:16:19 +00009Architecture (OSA, also commonly referred to as AppleScript) for Python,
10allowing you to control scriptable applications from your Python program,
11and with a fairly pythonic interface. Development on this set of modules has
Ronald Oussorencd22a582010-09-28 15:46:28 +000012stopped. For more up-to-date implementation of AppleScript support for Python,
13see the third-party py-appscript project: <http://pypi.python.org/pypi/appscript/>.
Georg Brandl8ec7f652007-08-15 14:28:01 +000014
15For a description of the various components of AppleScript and OSA, and to get
16an understanding of the architecture and terminology, you should read Apple's
17documentation. The "Applescript Language Guide" explains the conceptual model
18and the terminology, and documents the standard suite. The "Open Scripting
19Architecture" document explains how to use OSA from an application programmers
20point of view. In the Apple Help Viewer these books are located in the Developer
21Documentation, Core Technologies section.
22
23As an example of scripting an application, the following piece of AppleScript
24will get the name of the frontmost :program:`Finder` window and print it::
25
26 tell application "Finder"
27 get name of window 1
28 end tell
29
30In Python, the following code fragment will do the same::
31
32 import Finder
33
34 f = Finder.Finder()
35 print f.get(f.window(1).name)
36
37As distributed the Python library includes packages that implement the standard
38suites, plus packages that interface to a small number of common applications.
39
40To send AppleEvents to an application you must first create the Python package
41interfacing to the terminology of the application (what :program:`Script Editor`
42calls the "Dictionary"). This can be done from within the :program:`PythonIDE`
43or by running the :file:`gensuitemodule.py` module as a standalone program from
44the command line.
45
46The generated output is a package with a number of modules, one for every suite
47used in the program plus an :mod:`__init__` module to glue it all together. The
48Python inheritance graph follows the AppleScript inheritance graph, so if a
49program's dictionary specifies that it includes support for the Standard Suite,
50but extends one or two verbs with extra arguments then the output suite will
51contain a module :mod:`Standard_Suite` that imports and re-exports everything
52from :mod:`StdSuites.Standard_Suite` but overrides the methods that have extra
53functionality. The output of :mod:`gensuitemodule` is pretty readable, and
54contains the documentation that was in the original AppleScript dictionary in
55Python docstrings, so reading it is a good source of documentation.
56
57The output package implements a main class with the same name as the package
58which contains all the AppleScript verbs as methods, with the direct object as
59the first argument and all optional parameters as keyword arguments. AppleScript
60classes are also implemented as Python classes, as are comparisons and all the
61other thingies.
62
63The main Python class implementing the verbs also allows access to the
64properties and elements declared in the AppleScript class "application". In the
65current release that is as far as the object orientation goes, so in the example
66above we need to use ``f.get(f.window(1).name)`` instead of the more Pythonic
67``f.window(1).name.get()``.
68
69If an AppleScript identifier is not a Python identifier the name is mangled
70according to a small number of rules:
71
72* spaces are replaced with underscores
73
74* other non-alphanumeric characters are replaced with ``_xx_`` where ``xx`` is
75 the hexadecimal character value
76
77* any Python reserved word gets an underscore appended
78
79Python also has support for creating scriptable applications in Python, but The
80following modules are relevant to MacPython AppleScript support:
81
82.. toctree::
83
84 gensuitemodule.rst
85 aetools.rst
86 aepack.rst
87 aetypes.rst
88 miniaeframe.rst
89
90
91In addition, support modules have been pre-generated for :mod:`Finder`,
92:mod:`Terminal`, :mod:`Explorer`, :mod:`Netscape`, :mod:`CodeWarrior`,
93:mod:`SystemEvents` and :mod:`StdSuites`.