Jack Jansen | 7cc5735 | 1998-08-18 14:54:11 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 24 | static PyObject *ErrorObject; |
| 25 | static char OSAm_DoCommand__doc__[] = ""; |
| 26 | |
| 27 | |
| 28 | |
| 29 | static PyObject * |
| 30 | OSAm_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 Jansen | 3e82872 | 2003-01-08 16:27:44 +0000 | [diff] [blame] | 52 | OSErr err; |
Jack Jansen | 7cc5735 | 1998-08-18 14:54:11 +0000 | [diff] [blame] | 53 | |
Jack Jansen | 3e82872 | 2003-01-08 16:27:44 +0000 | [diff] [blame] | 54 | dataSize = AEGetDescDataSize (&temp); |
Jack Jansen | 7cc5735 | 1998-08-18 14:54:11 +0000 | [diff] [blame] | 55 | |
| 56 | if (dataSize > 0) |
| 57 | { |
Jack Jansen | 3e82872 | 2003-01-08 16:27:44 +0000 | [diff] [blame] | 58 | PyObject *result = PyString_FromStringAndSize (NULL, |
Jack Jansen | 7cc5735 | 1998-08-18 14:54:11 +0000 | [diff] [blame] | 59 | dataSize); |
Jack Jansen | 3e82872 | 2003-01-08 16:27:44 +0000 | [diff] [blame] | 60 | |
Jack Jansen | 7cc5735 | 1998-08-18 14:54:11 +0000 | [diff] [blame] | 61 | |
| 62 | if (!result) |
| 63 | { |
| 64 | printf ("OSAm.error Out of memory.\n"); |
| 65 | Py_INCREF (Py_None); |
Jack Jansen | 3e82872 | 2003-01-08 16:27:44 +0000 | [diff] [blame] | 66 | AEDisposeDesc (&temp); |
Jack Jansen | 7cc5735 | 1998-08-18 14:54:11 +0000 | [diff] [blame] | 67 | return Py_None; |
| 68 | } |
Jack Jansen | 3e82872 | 2003-01-08 16:27:44 +0000 | [diff] [blame] | 69 | if ( (err=AEGetDescData(&temp, PyString_AS_STRING(result), dataSize)) < 0 ) |
| 70 | { |
| 71 | AEDisposeDesc(&temp); |
| 72 | return PyMac_Error(err); |
| 73 | } |
Jack Jansen | 7cc5735 | 1998-08-18 14:54:11 +0000 | [diff] [blame] | 74 | |
Jack Jansen | 3e82872 | 2003-01-08 16:27:44 +0000 | [diff] [blame] | 75 | AEDisposeDesc(&temp); |
Jack Jansen | 7cc5735 | 1998-08-18 14:54:11 +0000 | [diff] [blame] | 76 | 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 | |
| 94 | static PyObject * |
| 95 | OSAm_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 Jansen | 3e82872 | 2003-01-08 16:27:44 +0000 | [diff] [blame] | 118 | OSErr err; |
Jack Jansen | 7cc5735 | 1998-08-18 14:54:11 +0000 | [diff] [blame] | 119 | |
Jack Jansen | 3e82872 | 2003-01-08 16:27:44 +0000 | [diff] [blame] | 120 | dataSize = AEGetDescDataSize (&temp); |
Jack Jansen | 7cc5735 | 1998-08-18 14:54:11 +0000 | [diff] [blame] | 121 | |
| 122 | if (dataSize > 0) |
| 123 | { |
Jack Jansen | 3e82872 | 2003-01-08 16:27:44 +0000 | [diff] [blame] | 124 | PyObject *result = PyString_FromStringAndSize (NULL, |
Jack Jansen | 7cc5735 | 1998-08-18 14:54:11 +0000 | [diff] [blame] | 125 | dataSize); |
Jack Jansen | 3e82872 | 2003-01-08 16:27:44 +0000 | [diff] [blame] | 126 | |
Jack Jansen | 7cc5735 | 1998-08-18 14:54:11 +0000 | [diff] [blame] | 127 | |
| 128 | if (!result) |
| 129 | { |
| 130 | printf ("OSAm.error Out of memory.\n"); |
| 131 | Py_INCREF (Py_None); |
Jack Jansen | 3e82872 | 2003-01-08 16:27:44 +0000 | [diff] [blame] | 132 | AEDisposeDesc (&temp); |
Jack Jansen | 7cc5735 | 1998-08-18 14:54:11 +0000 | [diff] [blame] | 133 | return Py_None; |
| 134 | } |
Jack Jansen | 3e82872 | 2003-01-08 16:27:44 +0000 | [diff] [blame] | 135 | if ( (err=AEGetDescData(&temp, PyString_AS_STRING(result), dataSize)) < 0 ) |
| 136 | { |
| 137 | AEDisposeDesc(&temp); |
| 138 | return PyMac_Error(err); |
| 139 | } |
Jack Jansen | 7cc5735 | 1998-08-18 14:54:11 +0000 | [diff] [blame] | 140 | |
Jack Jansen | 3e82872 | 2003-01-08 16:27:44 +0000 | [diff] [blame] | 141 | AEDisposeDesc(&temp); |
Jack Jansen | 7cc5735 | 1998-08-18 14:54:11 +0000 | [diff] [blame] | 142 | return result; |
| 143 | } |
Jack Jansen | 7cc5735 | 1998-08-18 14:54:11 +0000 | [diff] [blame] | 144 | } |
| 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 | |
| 160 | static PyObject * |
| 161 | OSAm_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 Jansen | 3e82872 | 2003-01-08 16:27:44 +0000 | [diff] [blame] | 182 | OSErr err; |
Jack Jansen | 7cc5735 | 1998-08-18 14:54:11 +0000 | [diff] [blame] | 183 | |
Jack Jansen | 3e82872 | 2003-01-08 16:27:44 +0000 | [diff] [blame] | 184 | dataSize = AEGetDescDataSize (&temp); |
Jack Jansen | 7cc5735 | 1998-08-18 14:54:11 +0000 | [diff] [blame] | 185 | |
| 186 | if (dataSize > 0) |
| 187 | { |
Jack Jansen | 3e82872 | 2003-01-08 16:27:44 +0000 | [diff] [blame] | 188 | PyObject *result = PyString_FromStringAndSize (NULL, |
Jack Jansen | 7cc5735 | 1998-08-18 14:54:11 +0000 | [diff] [blame] | 189 | dataSize); |
Jack Jansen | 3e82872 | 2003-01-08 16:27:44 +0000 | [diff] [blame] | 190 | |
Jack Jansen | 7cc5735 | 1998-08-18 14:54:11 +0000 | [diff] [blame] | 191 | |
| 192 | if (!result) |
| 193 | { |
| 194 | printf ("OSAm.error Out of memory.\n"); |
| 195 | Py_INCREF (Py_None); |
Jack Jansen | 3e82872 | 2003-01-08 16:27:44 +0000 | [diff] [blame] | 196 | AEDisposeDesc (&temp); |
Jack Jansen | 7cc5735 | 1998-08-18 14:54:11 +0000 | [diff] [blame] | 197 | return Py_None; |
| 198 | } |
Jack Jansen | 3e82872 | 2003-01-08 16:27:44 +0000 | [diff] [blame] | 199 | if ( (err=AEGetDescData(&temp, PyString_AS_STRING(result), dataSize)) < 0 ) |
| 200 | { |
| 201 | AEDisposeDesc(&temp); |
| 202 | return PyMac_Error(err); |
| 203 | } |
Jack Jansen | 7cc5735 | 1998-08-18 14:54:11 +0000 | [diff] [blame] | 204 | |
Jack Jansen | 3e82872 | 2003-01-08 16:27:44 +0000 | [diff] [blame] | 205 | AEDisposeDesc(&temp); |
Jack Jansen | 7cc5735 | 1998-08-18 14:54:11 +0000 | [diff] [blame] | 206 | 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 | */ |
| 227 | static struct PyMethodDef OSAm_methods[] = |
| 228 | { |
| 229 | {"CompileAndExecute", |
| 230 | (PyCFunction) OSAm_CompileAndExecute, |
| 231 | METH_VARARGS, |
| 232 | OSAm_DoCommand__doc__}, |
Jack Jansen | 3e82872 | 2003-01-08 16:27:44 +0000 | [diff] [blame] | 233 | #if 0 |
Jack Jansen | 7cc5735 | 1998-08-18 14:54:11 +0000 | [diff] [blame] | 234 | {"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 Jansen | 3e82872 | 2003-01-08 16:27:44 +0000 | [diff] [blame] | 243 | #endif |
Jack Jansen | 7cc5735 | 1998-08-18 14:54:11 +0000 | [diff] [blame] | 244 | |
| 245 | {NULL, (PyCFunction) NULL, 0, NULL} |
| 246 | }; |
| 247 | |
| 248 | |
| 249 | |
| 250 | static char OSAm_module_documentation[] = ""; |
| 251 | |
| 252 | |
| 253 | /* |
| 254 | * PYTHON Module Initalization |
| 255 | */ |
| 256 | void |
| 257 | initOSAm () |
| 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 | } |