Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | |
| 2 | .. _mac-scripting: |
| 3 | |
| 4 | ********************* |
| 5 | MacPython OSA Modules |
| 6 | ********************* |
| 7 | |
| 8 | This chapter describes the current implementation of the Open Scripting |
Benjamin Peterson | 90f3673 | 2008-07-12 20:16:19 +0000 | [diff] [blame] | 9 | Architecture (OSA, also commonly referred to as AppleScript) for Python, |
| 10 | allowing you to control scriptable applications from your Python program, |
| 11 | and with a fairly pythonic interface. Development on this set of modules has |
Ronald Oussoren | cd22a58 | 2010-09-28 15:46:28 +0000 | [diff] [blame] | 12 | stopped. For more up-to-date implementation of AppleScript support for Python, |
| 13 | see the third-party py-appscript project: <http://pypi.python.org/pypi/appscript/>. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 14 | |
| 15 | For a description of the various components of AppleScript and OSA, and to get |
| 16 | an understanding of the architecture and terminology, you should read Apple's |
| 17 | documentation. The "Applescript Language Guide" explains the conceptual model |
| 18 | and the terminology, and documents the standard suite. The "Open Scripting |
| 19 | Architecture" document explains how to use OSA from an application programmers |
| 20 | point of view. In the Apple Help Viewer these books are located in the Developer |
| 21 | Documentation, Core Technologies section. |
| 22 | |
| 23 | As an example of scripting an application, the following piece of AppleScript |
| 24 | will 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 | |
| 30 | In 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 | |
| 37 | As distributed the Python library includes packages that implement the standard |
| 38 | suites, plus packages that interface to a small number of common applications. |
| 39 | |
| 40 | To send AppleEvents to an application you must first create the Python package |
| 41 | interfacing to the terminology of the application (what :program:`Script Editor` |
| 42 | calls the "Dictionary"). This can be done from within the :program:`PythonIDE` |
| 43 | or by running the :file:`gensuitemodule.py` module as a standalone program from |
| 44 | the command line. |
| 45 | |
| 46 | The generated output is a package with a number of modules, one for every suite |
| 47 | used in the program plus an :mod:`__init__` module to glue it all together. The |
| 48 | Python inheritance graph follows the AppleScript inheritance graph, so if a |
| 49 | program's dictionary specifies that it includes support for the Standard Suite, |
| 50 | but extends one or two verbs with extra arguments then the output suite will |
| 51 | contain a module :mod:`Standard_Suite` that imports and re-exports everything |
| 52 | from :mod:`StdSuites.Standard_Suite` but overrides the methods that have extra |
| 53 | functionality. The output of :mod:`gensuitemodule` is pretty readable, and |
| 54 | contains the documentation that was in the original AppleScript dictionary in |
| 55 | Python docstrings, so reading it is a good source of documentation. |
| 56 | |
| 57 | The output package implements a main class with the same name as the package |
| 58 | which contains all the AppleScript verbs as methods, with the direct object as |
| 59 | the first argument and all optional parameters as keyword arguments. AppleScript |
| 60 | classes are also implemented as Python classes, as are comparisons and all the |
| 61 | other thingies. |
| 62 | |
| 63 | The main Python class implementing the verbs also allows access to the |
| 64 | properties and elements declared in the AppleScript class "application". In the |
| 65 | current release that is as far as the object orientation goes, so in the example |
| 66 | above we need to use ``f.get(f.window(1).name)`` instead of the more Pythonic |
| 67 | ``f.window(1).name.get()``. |
| 68 | |
| 69 | If an AppleScript identifier is not a Python identifier the name is mangled |
| 70 | according 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 | |
| 79 | Python also has support for creating scriptable applications in Python, but The |
| 80 | following 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 | |
| 91 | In 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`. |