append(): Fixing the test for convertability after consultation with
[python/dscho.git] / Mac / Contrib / osam / OSAm.c
bloba9c34454f284c6d6bafe79c42f275a585f1c4acd
1 /*
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.
7 * Sean Hummel <seanh@prognet.com>
8 * 1/20/98
9 * RealNetworks
11 * Jay Painter <jpaint@serv.net> <jpaint@gimp.org> <jpaint@real.com>
15 #include "OSAm.h"
16 #include "ScriptRunner.h"
17 #include <AppleEvents.h>
22 * Boiler plate generated from "genmodule.py"
24 static PyObject *ErrorObject;
25 static char OSAm_DoCommand__doc__[] = "";
29 static PyObject *
30 OSAm_RunCompiledScript (self, args)
31 PyObject *self;
32 PyObject *args;
34 char *commandStr = NULL;
35 char *outpath = NULL;
36 OSErr myErr;
37 AEDesc temp;
38 EventRecord event;
40 temp.dataHandle = NULL;
42 if (!PyArg_ParseTuple (args, "s", &commandStr))
43 return NULL;
45 myErr = ExecuteScriptFile (commandStr, NULL, &temp);
47 if (temp.dataHandle != NULL && temp.descriptorType == 'TEXT')
49 char *line;
50 DescType typeCode;
51 long dataSize = 0;
53 HLock (temp.dataHandle);
55 dataSize = GetHandleSize (temp.dataHandle);
57 if (dataSize > 0)
59 PyObject *result = PyString_FromStringAndSize ((*temp.dataHandle),
60 dataSize);
62 AEDisposeDesc (&temp);
64 if (!result)
66 printf ("OSAm.error Out of memory.\n");
67 Py_INCREF (Py_None);
68 return Py_None;
71 return result;
75 if (myErr != noErr)
77 PyErr_Mac (ErrorObject, myErr);
78 return NULL;
82 Py_INCREF (Py_None);
83 return Py_None;
89 static PyObject *
90 OSAm_CompileAndSave (self, args)
91 PyObject *self;
92 PyObject *args;
94 char *commandStr = NULL;
95 char *outpath = NULL;
96 OSErr myErr;
97 AEDesc temp;
98 EventRecord event;
100 temp.dataHandle = NULL;
102 if (!PyArg_ParseTuple (args, "ss", &commandStr, &outpath))
103 return NULL;
105 myErr = CompileAndSave (commandStr, outpath, NULL, &temp);
108 if (temp.dataHandle != NULL && temp.descriptorType == 'TEXT')
110 char *line;
111 DescType typeCode;
112 long dataSize = 0;
114 HLock (temp.dataHandle);
116 dataSize = GetHandleSize (temp.dataHandle);
118 if (dataSize > 0)
120 PyObject *result = PyString_FromStringAndSize ((*temp.dataHandle),
121 dataSize);
123 AEDisposeDesc (&temp);
125 if (!result)
127 printf ("OSAm.error Out of memory.\n");
128 Py_INCREF (Py_None);
129 return Py_None;
132 return result;
137 if (myErr != noErr)
140 PyErr_Mac (ErrorObject, myErr);
141 return NULL;
145 Py_INCREF (Py_None);
146 return Py_None;
151 static PyObject *
152 OSAm_CompileAndExecute (self, args)
153 PyObject *self;
154 PyObject *args;
156 char *commandStr;
157 OSErr myErr;
158 AEDesc temp;
159 EventRecord event;
161 temp.dataHandle = NULL;
163 if (!PyArg_ParseTuple (args, "s", &commandStr))
164 return NULL;
166 myErr = CompileAndExecute (commandStr, &temp, NULL);
168 if (temp.dataHandle != NULL && temp.descriptorType == 'TEXT')
170 char *line;
171 DescType typeCode;
172 long dataSize = 0;
174 HLock (temp.dataHandle);
176 dataSize = GetHandleSize (temp.dataHandle);
178 if (dataSize > 0)
180 PyObject *result = PyString_FromStringAndSize ((*temp.dataHandle),
181 dataSize);
183 AEDisposeDesc (&temp);
185 if (!result)
187 printf ("OSAm.error Out of memory.\n");
188 Py_INCREF (Py_None);
189 return Py_None;
192 return result;
196 if (myErr != noErr)
199 PyErr_Mac (ErrorObject, myErr);
200 return NULL;
204 Py_INCREF (Py_None);
205 return Py_None;
211 * List of methods defined in the module
213 static struct PyMethodDef OSAm_methods[] =
215 {"CompileAndExecute",
216 (PyCFunction) OSAm_CompileAndExecute,
217 METH_VARARGS,
218 OSAm_DoCommand__doc__},
220 {"CompileAndSave",
221 (PyCFunction) OSAm_CompileAndSave,
222 METH_VARARGS,
223 OSAm_DoCommand__doc__},
225 {"RunCompiledScript",
226 (PyCFunction) OSAm_RunCompiledScript,
227 METH_VARARGS,
228 OSAm_DoCommand__doc__},
230 {NULL, (PyCFunction) NULL, 0, NULL}
235 static char OSAm_module_documentation[] = "";
239 * PYTHON Module Initalization
241 void
242 initOSAm ()
244 PyObject *m, *d;
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);
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);
259 /* Check for errors */
260 if (PyErr_Occurred ())
261 Py_FatalError ("can't initialize module OSAm");