blob: aa9a5da33516dedf3f37652ed3e8cdd8fb79088d [file] [log] [blame]
Jack Jansen7cc57351998-08-18 14:54:11 +00001/*
2 *
3 * This is a simple module to allow the
4 * user to compile and execute an applescript
5 * which is passed in as a text item.
6 *
7 * Sean Hummel <seanh@prognet.com>
8 * 1/20/98
9 * RealNetworks
10 *
11 * Jay Painter <jpaint@serv.net> <jpaint@gimp.org> <jpaint@real.com>
12 *
13 *
14 */
15#include "OSAm.h"
16#include "ScriptRunner.h"
17#include <AppleEvents.h>
18
19
20
21/*
22 * Boiler plate generated from "genmodule.py"
23 */
24static PyObject *ErrorObject;
25static char OSAm_DoCommand__doc__[] = "";
26
27
28
29static PyObject *
30OSAm_RunCompiledScript (self, args)
31 PyObject *self;
32 PyObject *args;
33{
34 char *commandStr = NULL;
35 char *outpath = NULL;
36 OSErr myErr;
37 AEDesc temp;
38 EventRecord event;
39
40 temp.dataHandle = NULL;
41
42 if (!PyArg_ParseTuple (args, "s", &commandStr))
43 return NULL;
44
45 myErr = ExecuteScriptFile (commandStr, NULL, &temp);
46
47 if (temp.dataHandle != NULL && temp.descriptorType == 'TEXT')
48 {
49 char *line;
50 DescType typeCode;
51 long dataSize = 0;
Jack Jansen3e828722003-01-08 16:27:44 +000052 OSErr err;
Jack Jansen7cc57351998-08-18 14:54:11 +000053
Jack Jansen3e828722003-01-08 16:27:44 +000054 dataSize = AEGetDescDataSize (&temp);
Jack Jansen7cc57351998-08-18 14:54:11 +000055
56 if (dataSize > 0)
57 {
Jack Jansen3e828722003-01-08 16:27:44 +000058 PyObject *result = PyString_FromStringAndSize (NULL,
Jack Jansen7cc57351998-08-18 14:54:11 +000059 dataSize);
Jack Jansen3e828722003-01-08 16:27:44 +000060
Jack Jansen7cc57351998-08-18 14:54:11 +000061
62 if (!result)
63 {
64 printf ("OSAm.error Out of memory.\n");
65 Py_INCREF (Py_None);
Jack Jansen3e828722003-01-08 16:27:44 +000066 AEDisposeDesc (&temp);
Jack Jansen7cc57351998-08-18 14:54:11 +000067 return Py_None;
68 }
Jack Jansen3e828722003-01-08 16:27:44 +000069 if ( (err=AEGetDescData(&temp, PyString_AS_STRING(result), dataSize)) < 0 )
70 {
71 AEDisposeDesc(&temp);
72 return PyMac_Error(err);
73 }
Jack Jansen7cc57351998-08-18 14:54:11 +000074
Jack Jansen3e828722003-01-08 16:27:44 +000075 AEDisposeDesc(&temp);
Jack Jansen7cc57351998-08-18 14:54:11 +000076 return result;
77 }
78 }
79
80 if (myErr != noErr)
81 {
82 PyErr_Mac (ErrorObject, myErr);
83 return NULL;
84 }
85
86
87 Py_INCREF (Py_None);
88 return Py_None;
89}
90
91
92
93
94static PyObject *
95OSAm_CompileAndSave (self, args)
96 PyObject *self;
97 PyObject *args;
98{
99 char *commandStr = NULL;
100 char *outpath = NULL;
101 OSErr myErr;
102 AEDesc temp;
103 EventRecord event;
104
105 temp.dataHandle = NULL;
106
107 if (!PyArg_ParseTuple (args, "ss", &commandStr, &outpath))
108 return NULL;
109
110 myErr = CompileAndSave (commandStr, outpath, NULL, &temp);
111
112
113 if (temp.dataHandle != NULL && temp.descriptorType == 'TEXT')
114 {
115 char *line;
116 DescType typeCode;
117 long dataSize = 0;
Jack Jansen3e828722003-01-08 16:27:44 +0000118 OSErr err;
Jack Jansen7cc57351998-08-18 14:54:11 +0000119
Jack Jansen3e828722003-01-08 16:27:44 +0000120 dataSize = AEGetDescDataSize (&temp);
Jack Jansen7cc57351998-08-18 14:54:11 +0000121
122 if (dataSize > 0)
123 {
Jack Jansen3e828722003-01-08 16:27:44 +0000124 PyObject *result = PyString_FromStringAndSize (NULL,
Jack Jansen7cc57351998-08-18 14:54:11 +0000125 dataSize);
Jack Jansen3e828722003-01-08 16:27:44 +0000126
Jack Jansen7cc57351998-08-18 14:54:11 +0000127
128 if (!result)
129 {
130 printf ("OSAm.error Out of memory.\n");
131 Py_INCREF (Py_None);
Jack Jansen3e828722003-01-08 16:27:44 +0000132 AEDisposeDesc (&temp);
Jack Jansen7cc57351998-08-18 14:54:11 +0000133 return Py_None;
134 }
Jack Jansen3e828722003-01-08 16:27:44 +0000135 if ( (err=AEGetDescData(&temp, PyString_AS_STRING(result), dataSize)) < 0 )
136 {
137 AEDisposeDesc(&temp);
138 return PyMac_Error(err);
139 }
Jack Jansen7cc57351998-08-18 14:54:11 +0000140
Jack Jansen3e828722003-01-08 16:27:44 +0000141 AEDisposeDesc(&temp);
Jack Jansen7cc57351998-08-18 14:54:11 +0000142 return result;
143 }
Jack Jansen7cc57351998-08-18 14:54:11 +0000144 }
145
146 if (myErr != noErr)
147 {
148
149 PyErr_Mac (ErrorObject, myErr);
150 return NULL;
151 }
152
153
154 Py_INCREF (Py_None);
155 return Py_None;
156}
157
158
159
160static PyObject *
161OSAm_CompileAndExecute (self, args)
162 PyObject *self;
163 PyObject *args;
164{
165 char *commandStr;
166 OSErr myErr;
167 AEDesc temp;
168 EventRecord event;
169
170 temp.dataHandle = NULL;
171
172 if (!PyArg_ParseTuple (args, "s", &commandStr))
173 return NULL;
174
175 myErr = CompileAndExecute (commandStr, &temp, NULL);
176
177 if (temp.dataHandle != NULL && temp.descriptorType == 'TEXT')
178 {
179 char *line;
180 DescType typeCode;
181 long dataSize = 0;
Jack Jansen3e828722003-01-08 16:27:44 +0000182 OSErr err;
Jack Jansen7cc57351998-08-18 14:54:11 +0000183
Jack Jansen3e828722003-01-08 16:27:44 +0000184 dataSize = AEGetDescDataSize (&temp);
Jack Jansen7cc57351998-08-18 14:54:11 +0000185
186 if (dataSize > 0)
187 {
Jack Jansen3e828722003-01-08 16:27:44 +0000188 PyObject *result = PyString_FromStringAndSize (NULL,
Jack Jansen7cc57351998-08-18 14:54:11 +0000189 dataSize);
Jack Jansen3e828722003-01-08 16:27:44 +0000190
Jack Jansen7cc57351998-08-18 14:54:11 +0000191
192 if (!result)
193 {
194 printf ("OSAm.error Out of memory.\n");
195 Py_INCREF (Py_None);
Jack Jansen3e828722003-01-08 16:27:44 +0000196 AEDisposeDesc (&temp);
Jack Jansen7cc57351998-08-18 14:54:11 +0000197 return Py_None;
198 }
Jack Jansen3e828722003-01-08 16:27:44 +0000199 if ( (err=AEGetDescData(&temp, PyString_AS_STRING(result), dataSize)) < 0 )
200 {
201 AEDisposeDesc(&temp);
202 return PyMac_Error(err);
203 }
Jack Jansen7cc57351998-08-18 14:54:11 +0000204
Jack Jansen3e828722003-01-08 16:27:44 +0000205 AEDisposeDesc(&temp);
Jack Jansen7cc57351998-08-18 14:54:11 +0000206 return result;
207 }
208 }
209
210 if (myErr != noErr)
211 {
212
213 PyErr_Mac (ErrorObject, myErr);
214 return NULL;
215 }
216
217
218 Py_INCREF (Py_None);
219 return Py_None;
220}
221
222
223
224/*
225 * List of methods defined in the module
226 */
227static struct PyMethodDef OSAm_methods[] =
228{
229 {"CompileAndExecute",
230 (PyCFunction) OSAm_CompileAndExecute,
231 METH_VARARGS,
232 OSAm_DoCommand__doc__},
Jack Jansen3e828722003-01-08 16:27:44 +0000233#if 0
Jack Jansen7cc57351998-08-18 14:54:11 +0000234 {"CompileAndSave",
235 (PyCFunction) OSAm_CompileAndSave,
236 METH_VARARGS,
237 OSAm_DoCommand__doc__},
238
239 {"RunCompiledScript",
240 (PyCFunction) OSAm_RunCompiledScript,
241 METH_VARARGS,
242 OSAm_DoCommand__doc__},
Jack Jansen3e828722003-01-08 16:27:44 +0000243#endif
Jack Jansen7cc57351998-08-18 14:54:11 +0000244
245 {NULL, (PyCFunction) NULL, 0, NULL}
246};
247
248
249
250static char OSAm_module_documentation[] = "";
251
252
253/*
254 * PYTHON Module Initalization
255 */
256void
257initOSAm ()
258{
259 PyObject *m, *d;
260
261 /* Create the module and add the functions */
262 m = Py_InitModule4 ("OSAm",
263 OSAm_methods,
264 OSAm_module_documentation,
265 (PyObject *) NULL, PYTHON_API_VERSION);
266
267
268 /* Add some symbolic constants to the module */
269 d = PyModule_GetDict (m);
270 ErrorObject = PyString_FromString ("OSAm.error");
271 PyDict_SetItemString (d, "error", ErrorObject);
272
273
274 /* Check for errors */
275 if (PyErr_Occurred ())
276 Py_FatalError ("can't initialize module OSAm");
277}