-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpython_applescript.c
More file actions
executable file
·124 lines (106 loc) · 4.26 KB
/
python_applescript.c
File metadata and controls
executable file
·124 lines (106 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* Copyright (c) 2009 Olivier Hervieu <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
**/
#include "python_applescript.h"
#define ADD_METHOD(method_name)\
{#method_name,applescript_##method_name, METH_VARARGS, applescript_##method_name##_doc}
static PyObject * applescriptException = NULL;
static PyMethodDef applescriptMethods[] =
{
ADD_METHOD(launch_script),
{NULL, NULL} /* Sentinel */
};
char applescript_launch_script_doc[] = "launch_script(script)";
/* {{{ applescript_launch_script
*/
PyObject *
applescript_launch_script(PyObject *obj, PyObject *args) {
ComponentInstance theComponent = NULL;
AEDesc scriptTextDesc;
AEDesc resultData;
OSStatus err = noErr;
OSAID scriptID = kOSANullScript;
OSAID resultID;
int isError = 0;
char *text = NULL;
int returnSize = 0;
PyObject *buffer = NULL;
/* Grep python args */
if (PyArg_ParseTuple(args, "s", &text)){
/* String convertion */
err = AECreateDesc(typeUTF8Text, text, strlen(text), & scriptTextDesc);
/* Open the scripting component */
theComponent = OpenDefaultComponent(kOSAComponentType, typeAppleScript);
/* Compile the script */
err = OSACompile(theComponent, &scriptTextDesc, kOSAModeNull, &scriptID);
if ( err == noErr) {
/* Launch the script */
err = OSAExecute(theComponent, scriptID, kOSANullScript, kOSAModeNull, &resultID);
if (err != noErr) {
isError = 1;
AECreateDesc (typeNull, NULL, 0, &resultData);
if (err == errOSAScriptError) {
OSAScriptError( theComponent, kOSAErrorMessage, typeUTF8Text, &resultData);
}
}
else {
AECreateDesc (typeNull, NULL, 0, &resultData);
if (err ==noErr && resultID != kOSANullScript) {
OSADisplay (theComponent, resultID, typeUTF8Text, kOSAModeNull, &resultData);
}
};
/* Yourey! There's something in resultData */
returnSize = AEGetDescDataSize(&resultData);
buffer = PyString_FromStringAndSize(NULL, returnSize);
err = AEGetDescData(&resultData, PyString_AsString(buffer), returnSize);
AEDisposeDesc(&resultData);
if (isError) {
PyErr_SetString(applescriptException, PyString_AsString(buffer));
buffer = NULL;
}
}
else {
PyErr_SetString(applescriptException, "Failed to Compile given script");
return NULL;
}
}
else {
PyErr_SetString(applescriptException, "Error Parsing args");
}
return buffer;
}
/* }}}*/
/* {{{ initapplescript
*/
void initapplescript(void) {
PyObject *dict = NULL;
PyObject *mod = NULL;
applescriptException = PyErr_NewException("applescript.Error", NULL, NULL);
mod = Py_InitModule3(
"applescript", /* name of the module */
applescriptMethods, /* name of the method table */
"Python extension to execute applescript commands on OS X"
);
dict = PyModule_GetDict(mod);
PyDict_SetItemString(dict,"AppleScriptError",applescriptException);
return;
}
/* }}} */