Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 1 | <!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> |
| 2 | <html><head><title>Using the Open Scripting Architecture from Python</title></head> |
| 3 | <body> |
| 4 | <h1>Using the Open Scripting Architecture from Python</h1> |
| 5 | <hr> |
Jack Jansen | a630813 | 1996-03-18 13:38:52 +0000 | [diff] [blame] | 6 | |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 7 | <p>OSA support in Python is still not 100% complete, but |
Jack Jansen | a630813 | 1996-03-18 13:38:52 +0000 | [diff] [blame] | 8 | there is already enough in place to allow you to do some nifty things |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 9 | with other programs from your python program. </p> |
Jack Jansen | a630813 | 1996-03-18 13:38:52 +0000 | [diff] [blame] | 10 | |
Jack Jansen | a630813 | 1996-03-18 13:38:52 +0000 | [diff] [blame] | 11 | |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 12 | <p> |
Jack Jansen | a630813 | 1996-03-18 13:38:52 +0000 | [diff] [blame] | 13 | In this example, we will look at a scriptable application, extract its |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 14 | “AppleScript Dictionary,” generate a Python interface package from |
| 15 | the dictionary, and use that package to control the application. |
Jack Jansen | c15ab03 | 2000-08-20 21:57:38 +0000 | [diff] [blame] | 16 | The application we are going to script is Disk Copy, Apple's standard |
| 17 | utility for making copies of floppies, creating files that are mountable |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 18 | as disk images, etc. |
| 19 | Because we want |
| 20 | to concentrate on the OSA details, we won’t bother with a real |
| 21 | user-interface for our application. </p> |
Jack Jansen | c15ab03 | 2000-08-20 21:57:38 +0000 | [diff] [blame] | 22 | |
Jack Jansen | c15ab03 | 2000-08-20 21:57:38 +0000 | [diff] [blame] | 23 | |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 24 | <p> |
| 25 | <em>When we say “AppleScript” in this document we actually mean |
| 26 | “the Open Scripting Architecture.” There is nothing |
| 27 | AppleScript-specific in the Python implementation. Most of this document |
| 28 | focuses on the classic Mac OS; <a href="#osx">Mac OS X</a> users have some |
| 29 | additional tools.</em> |
| 30 | </p> |
| 31 | |
| 32 | <h2>Python OSA architecture</h2> |
| 33 | |
| 34 | <p>Open Scripting suites and inheritance can be modelled rather nicely |
| 35 | with Python packages, so we generate |
| 36 | a package for each application we want to script. Each suite defined in |
| 37 | the application becomes a module in the |
Jack Jansen | c15ab03 | 2000-08-20 21:57:38 +0000 | [diff] [blame] | 38 | package, and the package main module imports everything from all the |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 39 | submodules and glues together all the classes (in Python terminology— |
| 40 | events in OSA terminology or verbs in AppleScript terminology). </p> |
Jack Jansen | c15ab03 | 2000-08-20 21:57:38 +0000 | [diff] [blame] | 41 | |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 42 | <p> |
Jack Jansen | c15ab03 | 2000-08-20 21:57:38 +0000 | [diff] [blame] | 43 | A suite in an OSA application can extend the functionality of a standard |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 44 | suite. This is implemented in Python by importing everything from the |
| 45 | module that implements the standard suites and overriding anything that has |
| 46 | been extended. The standard suites live in the StdSuite package. </p> |
Jack Jansen | c15ab03 | 2000-08-20 21:57:38 +0000 | [diff] [blame] | 47 | |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 48 | <p> |
| 49 | This all sounds complicated, but the good news is that basic |
| 50 | scripting is actually pretty simple. You can do strange and wondrous things |
| 51 | with OSA scripting once you fully understand it. </p> |
Jack Jansen | a630813 | 1996-03-18 13:38:52 +0000 | [diff] [blame] | 52 | |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 53 | <h2>Creating the Python interface package</h2> |
Jack Jansen | a630813 | 1996-03-18 13:38:52 +0000 | [diff] [blame] | 54 | |
Jack Jansen | c15ab03 | 2000-08-20 21:57:38 +0000 | [diff] [blame] | 55 | |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 56 | <p>There is a tool in the standard distribution that can automatically |
| 57 | generate the interface packages. This tool is called |
| 58 | <code>gensuitemodule.py</code>, and lives in <code>Mac:scripts</code>. |
| 59 | It looks through a file |
| 60 | for an ‘AETE’ or ‘AEUT’ resource, |
| 61 | the internal representation of the |
| 62 | AppleScript dictionary, and parses the resource to generate the suite |
| 63 | modules. |
| 64 | When we start <code>gensuitemodule</code>, it asks us for an input file; |
| 65 | for our example, |
| 66 | we point it to the Disk Copy executable. </p> |
| 67 | |
| 68 | <p> |
| 69 | Next, <code>gensuitemodule</code> wants a folder where it will store the |
| 70 | package it is going to generate. |
Jack Jansen | c15ab03 | 2000-08-20 21:57:38 +0000 | [diff] [blame] | 71 | Note that this is the package folder, not the parent folder, so we |
| 72 | navigate to <code>Python:Mac:Demo:applescript</code>, create a folder |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 73 | <code>Disk_Copy</code>, and select that. </p> |
Jack Jansen | c15ab03 | 2000-08-20 21:57:38 +0000 | [diff] [blame] | 74 | |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 75 | <p> |
| 76 | We next specify the folder from which <code>gensuitemodule</code> |
| 77 | should import the standard suites. Here, |
| 78 | we always select <code>Python:Mac:Lib:lib-scriptpackages:StdSuites</code>. (There is |
Jack Jansen | c15ab03 | 2000-08-20 21:57:38 +0000 | [diff] [blame] | 79 | one exception to this rule: when you are generating <code>StdSuites</code> itself |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 80 | you select <code>_builtinSuites</code>.) |
| 81 | </p> |
Jack Jansen | c15ab03 | 2000-08-20 21:57:38 +0000 | [diff] [blame] | 82 | |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 83 | <p> |
Jack Jansen | c15ab03 | 2000-08-20 21:57:38 +0000 | [diff] [blame] | 84 | It starts parsing the AETE resource, and for |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 85 | each AppleEvent suite it finds, <code>gensuitemodule.py</code> |
| 86 | prompts us for the filename of the |
Jack Jansen | f10786b | 1997-08-19 14:00:56 +0000 | [diff] [blame] | 87 | resulting python module. Remember to change folders for the first |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 88 | module—you don't want to clutter up, say, the |
Jack Jansen | 9051ad1 | 2002-01-25 15:28:39 +0000 | [diff] [blame^] | 89 | Disk Copy folder |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 90 | with your python |
| 91 | interfaces. If you want to skip a suite, press <code>cancel</code> and the process |
| 92 | continues with the next suite. </p> |
Jack Jansen | a630813 | 1996-03-18 13:38:52 +0000 | [diff] [blame] | 93 | |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 94 | <h3>Summary</h3> |
| 95 | |
| 96 | <ol> |
| 97 | |
| 98 | <li>Run <code>gensuitemodule</code>.</li> |
| 99 | |
| 100 | <li>Select the application (or OSAX) for which you would like a Python interface.</li> |
| 101 | |
| 102 | <li>Select the package folder where the interface modules should be |
| 103 | stored.</li> |
| 104 | |
| 105 | <li>Specify the folder <code>Python:Mac:Lib:lib-scriptpackages:StdSuites</code> |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 106 | to import the standard suites (or <code>_builtinSuites</code> if you are |
| 107 | generating <code>StdSuites</code> itself). </li> |
| 108 | |
| 109 | <li>Save the generated suites (use <code>cancel</code> to skip a suite).</li> |
| 110 | |
| 111 | |
| 112 | </ol> |
| 113 | |
| 114 | |
| 115 | <h3>Notes</h3> |
| 116 | |
| 117 | |
| 118 | <ul> |
| 119 | |
Jack Jansen | 9051ad1 | 2002-01-25 15:28:39 +0000 | [diff] [blame^] | 120 | <li>The interface package may occasionally need some editing by hand. For example, |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 121 | <code>gensuitemodule</code> does not handle all Python reserved words, so |
| 122 | if |
| 123 | one of the AppleScript verbs is a Python reserved word, a <code>SyntaxError</code> |
| 124 | may be raised when the package is imported. |
| 125 | Simply rename the class into something acceptable, if this happens; |
| 126 | take a look at how the |
| 127 | <code>print</code> verb is handled (automatically by <code>gensuitemodule</code>) |
Jack Jansen | 9051ad1 | 2002-01-25 15:28:39 +0000 | [diff] [blame^] | 128 | in the standard suites. But: f you need to edit your package this should be considered a |
| 129 | bug in gensuitemodule, so please report it so it can be fixed in future releases. |
| 130 | </li> |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 131 | |
| 132 | |
| 133 | <li>If you want to re-create the StdSuite modules, |
| 134 | you should look in one of two places. With versions of AppleScript older than 1.4.0 |
| 135 | (which first shipped with OS 9.0), you will find the |
| 136 | AEUT resources in <code>System Folder:Extensions:Scripting |
| 137 | Additions:Dialects:English Dialect</code>. For newer versions, you will |
| 138 | find them in <code>System Folder:Extensions:Applescript</code>. |
| 139 | </li> |
| 140 | |
| 141 | <li>Since MacPython 2.0, this new structure, with packages |
| 142 | per application and submodules per suite, is used. Older MacPythons had a |
| 143 | single level of modules, with uncertain semantics. With the new structure, |
| 144 | it is possible for programs to override standard suites, as programs often do. |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 145 | |
| 146 | </li> |
| 147 | |
| 148 | <li><code>Gensuitemodule.py</code> may ask you questions |
| 149 | like “Where is enum 'xyz ' declared?”. |
| 150 | This is either due to a misunderstanding on my part or (rather too commonly) |
Jack Jansen | c15ab03 | 2000-08-20 21:57:38 +0000 | [diff] [blame] | 151 | bugs in the AETE resources. Pressing <code>cancel</code> is usually the |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 152 | right choice: it will cause the specific enum not to be treated as an enum |
| 153 | but as a “normal” type. As things like fsspecs and TEXT strings clearly are |
| 154 | not enumerators, this is correct. If someone understands what is really going on |
| 155 | here, please let me know.</li> |
Jack Jansen | f10786b | 1997-08-19 14:00:56 +0000 | [diff] [blame] | 156 | |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 157 | </ul> |
Jack Jansen | a630813 | 1996-03-18 13:38:52 +0000 | [diff] [blame] | 158 | |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 159 | |
| 160 | |
| 161 | <h2>The Python interface package contents</h2> |
| 162 | |
| 163 | <p> |
| 164 | Let’s glance at the |
| 165 | <a href="applescript/Disk_Copy">Disk_Copy</a> package just created. You |
| 166 | may want to open Script Editor alongside to see how it |
| 167 | interprets the dictionary. |
| 168 | </p> |
| 169 | |
| 170 | |
| 171 | <p> |
| 172 | The main package module is in <code>__init__.py</code>. |
| 173 | The only interesting bit is the <code>Disk_Copy</code> class, which |
Jack Jansen | c15ab03 | 2000-08-20 21:57:38 +0000 | [diff] [blame] | 174 | includes the event handling classes from the individual suites. It also |
| 175 | inherits <code>aetools.TalkTo</code>, which is a base class that handles all |
| 176 | details on how to start the program and talk to it, and a class variable |
| 177 | <code>_signature</code> which is the default application this class will talk |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 178 | to (you can override this in various ways when you instantiate your class, see |
Jack Jansen | c15ab03 | 2000-08-20 21:57:38 +0000 | [diff] [blame] | 179 | <code>aetools.py</code> for details). |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 180 | </p> |
Jack Jansen | a630813 | 1996-03-18 13:38:52 +0000 | [diff] [blame] | 181 | |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 182 | <p> |
Jack Jansen | c15ab03 | 2000-08-20 21:57:38 +0000 | [diff] [blame] | 183 | The <a href="applescript/Disk_Copy/Special_Events.py">Special_Events</a> |
| 184 | module is a nice example of a suite module. |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 185 | The <code>Special_Events_Events</code> class is the bulk of the code |
| 186 | generated. For each verb, it contains a method. Each method knows what |
| 187 | arguments the verb expects, and it makes use of keyword |
Jack Jansen | c15ab03 | 2000-08-20 21:57:38 +0000 | [diff] [blame] | 188 | arguments to present a palatable |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 189 | interface to the python programmer. |
Jack Jansen | a630813 | 1996-03-18 13:38:52 +0000 | [diff] [blame] | 190 | |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 191 | Notice that each method |
| 192 | calls some routines from <code>aetools</code>, an auxiliary module |
| 193 | living in <code>Mac:Lib</code>. |
| 194 | The other thing to notice is that each method calls |
| 195 | <code>self.send</code>. This comes from the <code>aetools.TalkTo</code> |
| 196 | baseclass. </p> |
Jack Jansen | a630813 | 1996-03-18 13:38:52 +0000 | [diff] [blame] | 197 | |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 198 | |
| 199 | <p> |
| 200 | After the big class, there are a number of little class declarations. These |
| 201 | declarations are for the (AppleEvent) classes and properties in the suite. |
Jack Jansen | f10786b | 1997-08-19 14:00:56 +0000 | [diff] [blame] | 202 | They allow you to create object IDs, which can then be passed to the verbs. |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 203 | For instance, |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 204 | when scripting the popular email program Eudora, |
| 205 | you would use <code>mailbox("inbox").message(1).sender</code> |
| 206 | to get the name of the sender of the first message in mailbox |
| 207 | inbox. It is |
Jack Jansen | f10786b | 1997-08-19 14:00:56 +0000 | [diff] [blame] | 208 | also possible to specify this as <code>sender(message(1, mailbox("inbox")))</code>, |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 209 | which is sometimes needed because these classes don’t always inherit correctly |
| 210 | from baseclasses, so you may have to use a class or property from another |
| 211 | suite. </p> |
Jack Jansen | f10786b | 1997-08-19 14:00:56 +0000 | [diff] [blame] | 212 | |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 213 | <p> |
Jack Jansen | f10786b | 1997-08-19 14:00:56 +0000 | [diff] [blame] | 214 | Next we get the enumeration dictionaries, which allow you to pass |
| 215 | english names as arguments to verbs, so you don't have to bother with the 4-letter |
| 216 | type code. So, you can say |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 217 | <code> |
Jack Jansen | c15ab03 | 2000-08-20 21:57:38 +0000 | [diff] [blame] | 218 | diskcopy.create(..., filesystem="Mac OS Standard") |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 219 | </code> |
| 220 | as it is called in Script Editor, instead of the cryptic lowlevel |
| 221 | <code> |
Jack Jansen | c15ab03 | 2000-08-20 21:57:38 +0000 | [diff] [blame] | 222 | diskcopy.create(..., filesystem="Fhfs") |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 223 | </code></p> |
Jack Jansen | f10786b | 1997-08-19 14:00:56 +0000 | [diff] [blame] | 224 | |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 225 | <p> |
| 226 | Finally, we get the “table of contents” of the module, listing all |
| 227 | classes and such |
Jack Jansen | 9051ad1 | 2002-01-25 15:28:39 +0000 | [diff] [blame^] | 228 | by code, which is used by <code>gensuitemodule</code> itself: if you use this |
| 229 | suite as a base package in a later run this is how it knows what is defined in this |
| 230 | suite, and what the Python names are. |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 231 | </p> |
Jack Jansen | a630813 | 1996-03-18 13:38:52 +0000 | [diff] [blame] | 232 | |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 233 | <h3>Notes</h3> |
Jack Jansen | a630813 | 1996-03-18 13:38:52 +0000 | [diff] [blame] | 234 | |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 235 | <ul> |
| 236 | |
| 237 | <li>The <code>aetools</code> module contains some other nifty |
| 238 | AppleEvent tools as well. Have a look at it sometime, there is (of |
| 239 | course) no documentation yet. |
| 240 | </li> |
| 241 | |
| 242 | <li>There are also some older object specifiers for standard objects in aetools. |
| 243 | You use these in the form <code>aetools.Word(10, |
| 244 | aetools.Document(1))</code>, where the corresponding AppleScript |
| 245 | terminology would be <code>word 10 of the first |
| 246 | document</code>. Examine |
| 247 | <code>aetools</code> and <code>aetools.TalkTo</code> |
| 248 | along with |
| 249 | the comments at the end of your suite module if you need to create |
| 250 | more than the standard object specifiers. |
| 251 | </li> |
| 252 | |
| 253 | </ul> |
Jack Jansen | a630813 | 1996-03-18 13:38:52 +0000 | [diff] [blame] | 254 | |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 255 | |
| 256 | |
| 257 | |
| 258 | <h2>Using a Python suite module</h2> |
| 259 | |
| 260 | <p> |
| 261 | Now that we have created the suite module, we can use it in a Python script. |
Jack Jansen | c15ab03 | 2000-08-20 21:57:38 +0000 | [diff] [blame] | 262 | In older MacPython distributions this used to be a rather |
| 263 | complicated affair, but with the package scheme and with the application signature |
| 264 | known by the package it is very simple: you import the package and instantiate |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 265 | the class, e.g. |
| 266 | <code> |
Jack Jansen | c15ab03 | 2000-08-20 21:57:38 +0000 | [diff] [blame] | 267 | talker = Disk_Copy.Disk_Copy(start=1) |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 268 | </code> |
| 269 | You will usually specify the <code>start=1</code>: it will run the application if it is |
| 270 | not already running. |
| 271 | You may want to omit it if you want to talk to the application |
| 272 | only if it is already running, or if the application is something like the Finder. |
Jack Jansen | 9051ad1 | 2002-01-25 15:28:39 +0000 | [diff] [blame^] | 273 | Another way to ensure that the application is running is to call <code>talker._start()</code>. |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 274 | </p> |
Jack Jansen | a630813 | 1996-03-18 13:38:52 +0000 | [diff] [blame] | 275 | |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 276 | <p> |
| 277 | Looking at the sourcefile <a |
| 278 | href="applescript/makedisk.py">makedisk.py</a>, we see that it starts |
| 279 | with some imports. Naturally, one of these is the Python interface to Disk |
| 280 | Copy.</p> |
Jack Jansen | bdf03a0 | 1996-09-20 15:22:47 +0000 | [diff] [blame] | 281 | |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 282 | <p> |
| 283 | The main program itself is a wonder of simplicity: we create the |
| 284 | object (<code>talker</code>) that talks to Disk Copy, |
| 285 | create a disk, and mount it. The bulk of |
| 286 | the work is done by <code>talker</code> and the Python interface package we |
| 287 | just created.</p> |
Jack Jansen | a630813 | 1996-03-18 13:38:52 +0000 | [diff] [blame] | 288 | |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 289 | <p> |
| 290 | The exception handling does warrant a few comments, though. Since |
| 291 | AppleScript is basically a connectionless RPC protocol, |
| 292 | nothing happens |
| 293 | when we create the <code>talker</code> object. Hence, if the destination application |
| 294 | is not running, we will not notice until we send our first |
| 295 | command (avoid this as described above). There is another thing to note about errors returned by |
| 296 | AppleScript calls: <code>MacOS.Error</code> is raised for |
| 297 | all of the errors that are known to be <code>OSErr</code>-type errors, |
| 298 | while |
| 299 | server generated errors raise <code>aetools.Error</code>. </p> |
Jack Jansen | a630813 | 1996-03-18 13:38:52 +0000 | [diff] [blame] | 300 | |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 301 | <h2>Scripting Additions</h2> |
Jack Jansen | a742d11 | 1996-12-23 17:28:53 +0000 | [diff] [blame] | 302 | |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 303 | <p> |
Jack Jansen | a742d11 | 1996-12-23 17:28:53 +0000 | [diff] [blame] | 304 | If you want to use any of the scripting additions (or OSAXen, in |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 305 | everyday speech) from a Python program, you can use the same method |
| 306 | as for applications, i.e. run <code>gensuitemodule</code> on the |
| 307 | OSAX (commonly found in <code>System Folder:Scripting Additions</code> |
Jack Jansen | c15ab03 | 2000-08-20 21:57:38 +0000 | [diff] [blame] | 308 | or something similar). There is one minor gotcha: the application |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 309 | signature to use is <code>MACS</code>. You will need to edit the main class |
| 310 | in the <code>__init__.py</code> file of the created package and change the value |
Jack Jansen | 9051ad1 | 2002-01-25 15:28:39 +0000 | [diff] [blame^] | 311 | of <code>_signature</code> to <code>MACS</code>, or use a subclass to the |
| 312 | same effect. |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 313 | </p> |
Jack Jansen | a742d11 | 1996-12-23 17:28:53 +0000 | [diff] [blame] | 314 | |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 315 | <p> |
| 316 | There are two minor points to watch out for when using <code>gensuitemodule</code> |
| 317 | on OSAXen: they appear all to define the class <code>System_Object_Suite</code>, |
Jack Jansen | a742d11 | 1996-12-23 17:28:53 +0000 | [diff] [blame] | 318 | and a lot of them have the command set in multiple dialects. You have to |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 319 | watch out for name conflicts and make sure you select a reasonable dialect |
| 320 | (some of the non-English dialects cause <code>gensuitemodule</code> to generate incorrect |
| 321 | Python code). </p> |
Jack Jansen | a742d11 | 1996-12-23 17:28:53 +0000 | [diff] [blame] | 322 | |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 323 | Despite these difficulties, OSAXen offer a lot of possibilities. Take a |
| 324 | look at some of the OSAXen in the Scripting Additions folder, or |
| 325 | <A HREF="http://www.osaxen.com/index.php">download</A> some from the net. |
Jack Jansen | c15ab03 | 2000-08-20 21:57:38 +0000 | [diff] [blame] | 326 | |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 327 | <h2>Further Reading</h2> |
| 328 | |
| 329 | <p> |
| 330 | If you want to look at more involved examples of applescripting, look at the standard |
Jack Jansen | c15ab03 | 2000-08-20 21:57:38 +0000 | [diff] [blame] | 331 | modules <code>findertools</code> and <code>nsremote</code>, or (possibly better, as it |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 332 | is more involved) <code>fullbuild</code> from the <code>Mac:scripts</code> folder. |
| 333 | </p> |
| 334 | |
| 335 | <h2><a name="alternatives">Alternatives</a></h2> |
| 336 | |
| 337 | <h3><a name="osx">Mac OS X</a></h3> |
| 338 | |
| 339 | <p> |
| 340 | Under Mac OS X, the above still works, but with some new difficulties. |
Jack Jansen | 9051ad1 | 2002-01-25 15:28:39 +0000 | [diff] [blame^] | 341 | The application package structure can hide the ‘AETE’ or |
| 342 | ‘AEUT’ resource from <code>gensuitemodule</code>, so that, |
| 343 | for example, it cannot generate an OSA interface to iTunes. Script |
| 344 | Editor gets at the dictionary of such programs using a ‘Get |
| 345 | AETE’ AppleEvent, if someone wants to donate code to use the same |
| 346 | method for gensuitemodule: by all means! |
Jack Jansen | c7a7d2d | 2002-01-25 15:06:19 +0000 | [diff] [blame] | 347 | </p> |
| 348 | |
| 349 | <p> |
| 350 | One alternative is available through the Unix command line version of python. |
| 351 | Apple has provided the <code>osacompile</code> and <code>osascript</code> tools, |
| 352 | which can be used to compile and execute scripts written in OSA languages. See the |
| 353 | man pages for more details. |
| 354 | </p> |
| 355 | |
| 356 | |
| 357 | </body> |
| 358 | </html> |