Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / html / python / mac / scripting.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="mac.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="mac.html" title='Macintosh Library Modules' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="toolbox.html" />
<link rel="prev" href="macpython-modules.html" />
<link rel="parent" href="mac.html" />
<link rel="next" href="module-gensuitemodule.html" />
<meta name='aesop' content='information' />
<title>3. MacPython OSA Modules </title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="2.9 autoGIL "
href="module-autoGIL.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="Macintosh Library Modules"
href="mac.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="3.1 gensuitemodule "
href="module-gensuitemodule.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Macintosh Library Modules</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-autoGIL.html">2.9 autoGIL </A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="mac.html">Macintosh Library Modules</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-gensuitemodule.html">3.1 gensuitemodule </A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION005000000000000000000"></A><A NAME="scripting"></A>
<BR>
3. MacPython OSA Modules
</H1>
<P>
This chapter describes the current implementation of the Open Scripting
Architecure (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, and a replacement is expected for Python 2.5.
<P>
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 book sare located in the Developer Documentation, Core
Technologies section.
<P>
As an example of scripting an application, the following piece of
AppleScript will get the name of the frontmost <b class="program">Finder</b> window
and print it:
<P>
<div class="verbatim"><pre>
tell application "Finder"
get name of window 1
end tell
</pre></div>
<P>
In Python, the following code fragment will do the same:
<P>
<div class="verbatim"><pre>
import Finder
f = Finder.Finder()
print f.get(f.window(1).name)
</pre></div>
<P>
As distributed the Python library includes packages that implement the
standard suites, plus packages that interface to a small number of
common applications.
<P>
To send AppleEvents to an application you must first create the Python
package interfacing to the terminology of the application (what
<b class="program">Script Editor</b> calls the "Dictionary"). This can be done from
within the <b class="program">PythonIDE</b> or by running the
<span class="file">gensuitemodule.py</span> module as a standalone program from the command
line.
<P>
The generated output is a package with a number of modules, one for
every suite used in the program plus an <tt class="module">__init__</tt> module to glue
it all together. The Python inheritance graph follows the AppleScript
inheritance graph, so if a programs 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
<tt class="module">Standard_Suite</tt> that imports and re-exports everything from
<tt class="module">StdSuites.Standard_Suite</tt> but overrides the methods that have
extra functionality. The output of <tt class="module">gensuitemodule</tt> 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.
<P>
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.
<P>
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
<code>f.get(f.window(1).name)</code> instead of the more Pythonic
<code>f.window(1).name.get()</code>.
<P>
If an AppleScript identifier is not a Python identifier the name is
mangled according to a small number of rules:
<UL>
<LI>spaces are replaced with underscores
</LI>
<LI>other non-alphanumeric characters are replaced with
<code>_xx_</code> where <code>xx</code> is the hexadecimal character value
</LI>
<LI>any Python reserved word gets an underscore appended
</LI>
</UL>
<P>
Python also has support for creating scriptable applications
in Python, but
The following modules are relevant to MacPython AppleScript support:
<P>
<table class='synopsistable' valign='baseline'>
<tr class='oddrow'>
<td><b><tt class='module'><a href='module-gensuitemodule.html'>gensuitemodule</a></tt></b></td>
<td>&nbsp;</td>
<td class='synopsis'>Create a stub package from an OSA dictionary</td></tr>
<tr><td><b><tt class='module'><a href='module-aetools.html'>aetools</a></tt></b></td>
<td>&nbsp;</td>
<td class='synopsis'>Basic support for sending Apple Events</td></tr>
<tr class='oddrow'>
<td><b><tt class='module'><a href='module-aepack.html'>aepack</a></tt></b></td>
<td>&nbsp;</td>
<td class='synopsis'>Conversion between Python variables and AppleEvent
data containers.</td></tr>
<tr><td><b><tt class='module'><a href='module-aetypes.html'>aetypes</a></tt></b></td>
<td>&nbsp;</td>
<td class='synopsis'>Python representation of the Apple Event Object Model.</td></tr>
<tr class='oddrow'>
<td><b><tt class='module'><a href='module-MiniAEFrame.html'>MiniAEFrame</a></tt></b></td>
<td>&nbsp;</td>
<td class='synopsis'>Support to act as an Open Scripting Architecture (OSA) server
(``Apple Events'').</td></tr>
</table>
<BR>
<P>
In addition, support modules have been pre-generated for
<tt class="module">Finder</tt>, <tt class="module">Terminal</tt>, <tt class="module">Explorer</tt>,
<tt class="module">Netscape</tt>, <tt class="module">CodeWarrior</tt>, <tt class="module">SystemEvents</tt> and
<tt class="module">StdSuites</tt>.
<P>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="2.9 autoGIL "
href="module-autoGIL.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="Macintosh Library Modules"
href="mac.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="3.1 gensuitemodule "
href="module-gensuitemodule.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Macintosh Library Modules</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-autoGIL.html">2.9 autoGIL </A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="mac.html">Macintosh Library Modules</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-gensuitemodule.html">3.1 gensuitemodule </A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>