38. MacPython OSA Modules - Python 2.7.18 documentation 编辑

This chapter describes the current implementation of the Open Scripting Architecture (OSA, also commonly referred to as AppleScript) for Python, allowing you to control scriptable applications from your Python program, and with a fairly pythonic interface. Development on this set of modules has stopped.

For a description of the various components of AppleScript and OSA, and to get an understanding of the architecture and terminology, you should read Apple’s documentation. The “Applescript Language Guide” explains the conceptual model and the terminology, and documents the standard suite. The “Open Scripting Architecture” document explains how to use OSA from an application programmers point of view. In the Apple Help Viewer these books are located in the Developer Documentation, Core Technologies section.

As an example of scripting an application, the following piece of AppleScript will get the name of the frontmost Finder window and print it:

tell application "Finder"
    get name of window 1
end tell

In Python, the following code fragment will do the same:

import Finder

f = Finder.Finder()
print f.get(f.window(1).name)

As distributed the Python library includes packages that implement the standard suites, plus packages that interface to a small number of common applications.

To send AppleEvents to an application you must first create the Python package interfacing to the terminology of the application (what Script Editor calls the “Dictionary”). This can be done from within the PythonIDE or by running the gensuitemodule.py module as a standalone program from the command line.

The generated output is a package with a number of modules, one for every suite used in the program plus an __init__ module to glue it all together. The Python inheritance graph follows the AppleScript inheritance graph, so if a program’s dictionary specifies that it includes support for the Standard Suite, but extends one or two verbs with extra arguments then the output suite will contain a module Standard_Suite that imports and re-exports everything from StdSuites.Standard_Suite but overrides the methods that have extra functionality. The output of gensuitemodule is pretty readable, and contains the documentation that was in the original AppleScript dictionary in Python docstrings, so reading it is a good source of documentation.

The output package implements a main class with the same name as the package which contains all the AppleScript verbs as methods, with the direct object as the first argument and all optional parameters as keyword arguments. AppleScript classes are also implemented as Python classes, as are comparisons and all the other thingies.

The main Python class implementing the verbs also allows access to the properties and elements declared in the AppleScript class “application”. In the current release that is as far as the object orientation goes, so in the example above we need to use f.get(f.window(1).name) instead of the more Pythonic f.window(1).name.get().

If an AppleScript identifier is not a Python identifier the name is mangled according to a small number of rules:

  • spaces are replaced with underscores

  • other non-alphanumeric characters are replaced with _xx_ where xx is the hexadecimal character value

  • any Python reserved word gets an underscore appended

Python also has support for creating scriptable applications in Python, but The following modules are relevant to MacPython AppleScript support:

In addition, support modules have been pre-generated for Finder, Terminal, Explorer, Netscape, CodeWarrior, SystemEvents and StdSuites.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:47 次

字数:4939

最后编辑:6年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文