blob: a9c34454f284c6d6bafe79c42f275a585f1c4acd [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;
52
53 HLock (temp.dataHandle);
54
55 dataSize = GetHandleSize (temp.dataHandle);
56
57 if (dataSize > 0)
58 {
59 PyObject *result = PyString_FromStringAndSize ((*temp.dataHandle),
60 dataSize);
61
62 AEDisposeDesc (&temp);
63
64 if (!result)
65 {
66 printf ("OSAm.error Out of memory.\n");
67 Py_INCREF (Py_None);
68 return Py_None;
69 }
70
71 return result;
72 }
73 }
74
75 if (myErr != noErr)
76 {
77 PyErr_Mac (ErrorObject, myErr);
78 return NULL;
79 }
80
81
82 Py_INCREF (Py_None);
83 return Py_None;
84}
85
86
87
88
89static PyObject *
90OSAm_CompileAndSave (self, args)
91 PyObject *self;
92 PyObject *args;
93{
94 char *commandStr = NULL;
95 char *outpath = NULL;
96 OSErr myErr;
97 AEDesc temp;
98 EventRecord event;
99
100 temp.dataHandle = NULL;
101
102 if (!PyArg_ParseTuple (args, "ss", &commandStr, &outpath))
103 return NULL;
104
105 myErr = CompileAndSave (commandStr, outpath, NULL, &temp);
106
107
108 if (temp.dataHandle != NULL && temp.descriptorType == 'TEXT')
109 {
110 char *line;
111 DescType typeCode;
112 long dataSize = 0;
113
114 HLock (temp.dataHandle);
115
116 dataSize = GetHandleSize (temp.dataHandle);
117
118 if (dataSize > 0)
119 {
120 PyObject *result = PyString_FromStringAndSize ((*temp.dataHandle),
121 dataSize);
122
123 AEDisposeDesc (&temp);
124
125 if (!result)
126 {
127 printf ("OSAm.error Out of memory.\n");
128 Py_INCREF (Py_None);
129 return Py_None;
130 }
131
132 return result;
133 }
134
135 }
136
137 if (myErr != noErr)
138 {
139
140 PyErr_Mac (ErrorObject, myErr);
141 return NULL;
142 }
143
144
145 Py_INCREF (Py_None);
146 return Py_None;
147}
148
149
150
151static PyObject *
152OSAm_CompileAndExecute (self, args)
153 PyObject *self;
154 PyObject *args;
155{
156 char *commandStr;
157 OSErr myErr;
158 AEDesc temp;
159 EventRecord event;
160
161 temp.dataHandle = NULL;
162
163 if (!PyArg_ParseTuple (args, "s", &commandStr))
164 return NULL;
165
166 myErr = CompileAndExecute (commandStr, &temp, NULL);
167
168 if (temp.dataHandle != NULL && temp.descriptorType == 'TEXT')
169 {
170 char *line;
171 DescType typeCode;
172 long dataSize = 0;
173
174 HLock (temp.dataHandle);
175
176 dataSize = GetHandleSize (temp.dataHandle);
177
178 if (dataSize > 0)
179 {
180 PyObject *result = PyString_FromStringAndSize ((*temp.dataHandle),
181 dataSize);
182
183 AEDisposeDesc (&temp);
184
185 if (!result)
186 {
187 printf ("OSAm.error Out of memory.\n");
188 Py_INCREF (Py_None);
189 return Py_None;
190 }
191
192 return result;
193 }
194 }
195
196 if (myErr != noErr)
197 {
198
199 PyErr_Mac (ErrorObject, myErr);
200 return NULL;
201 }
202
203
204 Py_INCREF (Py_None);
205 return Py_None;
206}
207
208
209
210/*
211 * List of methods defined in the module
212 */
213static struct PyMethodDef OSAm_methods[] =
214{
215 {"CompileAndExecute",
216 (PyCFunction) OSAm_CompileAndExecute,
217 METH_VARARGS,
218 OSAm_DoCommand__doc__},
219
220 {"CompileAndSave",
221 (PyCFunction) OSAm_CompileAndSave,
222 METH_VARARGS,
223 OSAm_DoCommand__doc__},
224
225 {"RunCompiledScript",
226 (PyCFunction) OSAm_RunCompiledScript,
227 METH_VARARGS,
228 OSAm_DoCommand__doc__},
229
230 {NULL, (PyCFunction) NULL, 0, NULL}
231};
232
233
234
235static char OSAm_module_documentation[] = "";
236
237
238/*
239 * PYTHON Module Initalization
240 */
241void
242initOSAm ()
243{
244 PyObject *m, *d;
245
246 /* Create the module and add the functions */
247 m = Py_InitModule4 ("OSAm",
248 OSAm_methods,
249 OSAm_module_documentation,
250 (PyObject *) NULL, PYTHON_API_VERSION);
251
252
253 /* Add some symbolic constants to the module */
254 d = PyModule_GetDict (m);
255 ErrorObject = PyString_FromString ("OSAm.error");
256 PyDict_SetItemString (d, "error", ErrorObject);
257
258
259 /* Check for errors */
260 if (PyErr_Occurred ())
261 Py_FatalError ("can't initialize module OSAm");
262}