AddressList.__str__(): Get rid of useless, and broken method. Closes
[python/dscho.git] / Mac / Contrib / osam / ScriptRunner.c
blob6f7d9ce4b908e3f4f7bc1cc89bcfd20b8303bdb6
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>
16 #include <Resources.h>
17 #include <Files.h>
18 #include <OSA.h>
19 #include <string.h>
20 #include "ScriptRunner.h"
21 #include <script.h>
22 #include <resources.h>
24 #ifdef TARGET_API_MAC_CARBON
25 static
26 p2cstr(StringPtr p)
28 unsigned char *c = p;
29 int len = c[0];
30 strncpy((char *)c+1, (char *)c, len);
31 c[len] = 0;
34 static c2pstr(const char *cc)
36 char *c = (char *)cc; /* Ouch */
37 int len = strlen(c);
39 if ( len > 255 ) len = 255;
40 strncpy(c, c+1, len);
41 c[0] = len;
43 #endif
45 OSAError LoadScriptingComponent (ComponentInstance * scriptingComponent);
47 #if 0
49 * store the script as a compile script so that OSA
50 * components may load and execute the script easily
52 OSAError
53 CompileAndSave (const char *text,
54 const char *outfile,
55 OSAActiveUPP proc,
56 AEDesc * result)
59 OSAError err2 = 0;
60 AEDesc theScript;
61 OSAID compiledScriptID = 0;
62 ComponentInstance scriptingComponent;
63 FSSpec outfilespec;
64 AEDesc theCompiledScript;
65 OSAID scriptid = kOSANullScript;
66 short saveres = 0;
70 /* Initialize theScript here because it is a struct */
71 theScript.dataHandle = NULL;
72 theCompiledScript.dataHandle = NULL;
75 /* open the component manager */
76 err2 = LoadScriptingComponent (&scriptingComponent);
77 if (err2)
78 return err2; /* <<< Fail quietly?? */
81 /* construct the AppleEvent Descriptor to contain the text of script */
82 AECreateDesc ('TEXT', text, strlen (text), &theScript);
84 err2 = OSACompile (scriptingComponent,
85 &theScript,
86 kOSAModeCompileIntoContext,
87 &scriptid);
88 if (err2)
90 OSAScriptError (scriptingComponent, kOSAErrorMessage, 'TEXT', result);
91 goto CleanUp;
95 err2 = OSAStore (scriptingComponent,
96 scriptid,
97 typeOSAGenericStorage,
98 kOSAModeCompileIntoContext,
99 &theCompiledScript);
100 if (err2)
102 OSAScriptError (scriptingComponent, kOSAErrorMessage, 'TEXT', result);
103 goto CleanUp;
107 c2pstr (outfile);
108 FSMakeFSSpec (0, 0, (StringPtr) outfile, &outfilespec);
109 p2cstr ((StringPtr) outfile);
111 FSpDelete (&outfilespec);
113 FSpCreateResFile (&outfilespec, 'ToyS', 'osas', smRoman);
115 saveres = CurResFile ();
117 if (saveres)
119 short myres = 0;
120 myres = FSpOpenResFile (&outfilespec, fsWrPerm);
122 UseResFile (myres);
123 AddResource (theCompiledScript.dataHandle, 'scpt', 128, "\p");
124 CloseResFile (myres);
125 UseResFile (saveres);
129 CleanUp:
131 if (theScript.dataHandle)
132 AEDisposeDesc (&theScript);
134 if (theCompiledScript.dataHandle)
135 AEDisposeDesc (&theCompiledScript);
137 if (scriptid)
138 OSADispose (scriptingComponent, scriptid);
140 if (scriptingComponent != 0)
141 CloseComponent (scriptingComponent);
144 return err2;
146 #endif
149 OSAError
150 CompileAndExecute (const char *text,
151 AEDesc * result,
152 OSAActiveUPP proc)
154 OSAError err2 = 0;
155 AEDesc theScript;
156 OSAID compiledScriptID = 0;
157 ComponentInstance scriptingComponent;
160 /* initialize theScript here because it is a struct */
161 theScript.dataHandle = NULL;
163 /* Open the component manager */
164 err2 = LoadScriptingComponent (&scriptingComponent);
165 if (err2)
166 return err2; /* <<< Fail quietly?? */
169 /* construct the AppleEvent Descriptor to contain the text of script */
170 AECreateDesc ('TEXT', text, strlen (text), &theScript);
173 err2 = OSASetActiveProc (scriptingComponent, proc, NULL);
174 if (err2)
175 goto CleanUp;
178 err2 = OSADoScript (scriptingComponent, &theScript, kOSANullScript, 'TEXT', 0, result);
179 if (err2)
181 OSAScriptError (scriptingComponent, kOSAErrorMessage, 'TEXT', result);
182 goto CleanUp;
186 CleanUp:
188 if (theScript.dataHandle)
189 AEDisposeDesc (&theScript);
191 if (scriptingComponent != 0)
192 CloseComponent (scriptingComponent);
195 return err2;
198 #if 0
200 * This routine reads in a saved script file and executes
201 * the script contained within (from a 'scpt' resource.)
203 OSAError
204 ExecuteScriptFile (const char *theFilePath,
205 OSAActiveUPP proc,
206 AEDesc * result)
208 OSAError err2;
209 short resRefCon;
210 AEDesc theScript;
211 OSAID compiledScriptID, scriptResultID;
212 ComponentInstance scriptingComponent;
213 FSSpec theFile;
216 c2pstr (theFilePath);
217 FSMakeFSSpec (0, 0, (StringPtr) theFilePath, &theFile);
218 p2cstr ((StringPtr) theFilePath);
221 /* open a connection to the OSA */
222 err2 = LoadScriptingComponent (&scriptingComponent);
223 if (err2)
224 return err2; /* <<< Fail quietly?? */
227 err2 = OSASetActiveProc (scriptingComponent, proc, NULL);
228 if (err2)
229 goto error;
232 /* now, try and read in the script
233 * Open the script file and get the resource
235 resRefCon = FSpOpenResFile (&theFile, fsRdPerm);
236 if (resRefCon == -1)
237 return ResError ();
239 theScript.dataHandle = Get1IndResource (typeOSAGenericStorage, 1);
241 if ((err2 = ResError ()) || (err2 = resNotFound, theScript.dataHandle == NULL))
243 CloseResFile (resRefCon);
244 return err2;
247 theScript.descriptorType = typeOSAGenericStorage;
248 DetachResource (theScript.dataHandle);
249 CloseResFile (resRefCon);
250 err2 = noErr;
253 /* give a copy of the script to AppleScript */
254 err2 = OSALoad (scriptingComponent,
255 &theScript,
256 0L,
257 &compiledScriptID);
258 if (err2)
259 goto error;
261 AEDisposeDesc (&theScript);
262 theScript.dataHandle = NULL;
265 err2 = OSAExecute (scriptingComponent,
266 compiledScriptID,
267 kOSANullScript,
269 &scriptResultID);
271 if (compiledScriptID)
272 OSAScriptError (scriptingComponent, kOSAErrorMessage, 'TEXT', result);
274 if (err2)
275 goto error;
277 /* If there was an error, return it. If there was a result, return it. */
278 (void) OSADispose (scriptingComponent, compiledScriptID);
280 if (err2)
281 goto error;
282 else
283 goto done;
285 error:
286 if (theScript.dataHandle)
287 AEDisposeDesc (&theScript);
290 done:
293 return err2;
295 #endif
298 OSAError
299 LoadScriptingComponent (ComponentInstance * scriptingComponent)
301 OSAError err2;
303 /* Open a connection to the Open Scripting Architecture */
304 *scriptingComponent = OpenDefaultComponent (kOSAComponentType,
305 kOSAGenericScriptingComponentSubtype);
307 err2 = GetComponentInstanceError (*scriptingComponent);
309 return err2;