/Doc/library/macosa.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 92 lines · 69 code · 23 blank · 0 comment · 0 complexity · dd4b3c4ff44344e949043b26d9dcd58c MD5 · raw file

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