Bump version to 0.9.1.
[python/dscho.git] / Mac / Contrib / osam / ScriptRunner.c
blob7fd68f64737e2d18c5b2ca0ce42617ae70cd8097
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>
26 OSAError LoadScriptingComponent (ComponentInstance * scriptingComponent);
30 * store the script as a compile script so that OSA
31 * components may load and execute the script easily
33 OSAError
34 CompileAndSave (const char *text,
35 const char *outfile,
36 OSAActiveUPP proc,
37 AEDesc * result)
40 OSAError err2 = 0;
41 AEDesc theScript;
42 OSAID compiledScriptID = 0;
43 ComponentInstance scriptingComponent;
44 FSSpec outfilespec;
45 AEDesc theCompiledScript;
46 OSAID scriptid = kOSANullScript;
47 short saveres = 0;
51 /* Initialize theScript here because it is a struct */
52 theScript.dataHandle = NULL;
53 theCompiledScript.dataHandle = NULL;
56 /* open the component manager */
57 err2 = LoadScriptingComponent (&scriptingComponent);
58 if (err2)
59 return err2; /* <<< Fail quietly?? */
62 /* construct the AppleEvent Descriptor to contain the text of script */
63 AECreateDesc ('TEXT', text, strlen (text), &theScript);
65 err2 = OSACompile (scriptingComponent,
66 &theScript,
67 kOSAModeCompileIntoContext,
68 &scriptid);
69 if (err2)
71 OSAScriptError (scriptingComponent, kOSAErrorMessage, 'TEXT', result);
72 goto CleanUp;
76 err2 = OSAStore (scriptingComponent,
77 scriptid,
78 typeOSAGenericStorage,
79 kOSAModeCompileIntoContext,
80 &theCompiledScript);
81 if (err2)
83 OSAScriptError (scriptingComponent, kOSAErrorMessage, 'TEXT', result);
84 goto CleanUp;
88 c2pstr (outfile);
89 FSMakeFSSpec (0, 0, (StringPtr) outfile, &outfilespec);
90 p2cstr ((StringPtr) outfile);
92 FSpDelete (&outfilespec);
94 FSpCreateResFile (&outfilespec, 'ToyS', 'osas', smRoman);
96 saveres = CurResFile ();
98 if (saveres)
100 short myres = 0;
101 myres = FSpOpenResFile (&outfilespec, fsWrPerm);
103 UseResFile (myres);
104 AddResource (theCompiledScript.dataHandle, 'scpt', 128, "\p");
105 CloseResFile (myres);
106 UseResFile (saveres);
110 CleanUp:
112 if (theScript.dataHandle)
113 AEDisposeDesc (&theScript);
115 if (theCompiledScript.dataHandle)
116 AEDisposeDesc (&theCompiledScript);
118 if (scriptid)
119 OSADispose (scriptingComponent, scriptid);
121 if (scriptingComponent != 0)
122 CloseComponent (scriptingComponent);
125 return err2;
129 OSAError
130 CompileAndExecute (const char *text,
131 AEDesc * result,
132 OSAActiveUPP proc)
134 OSAError err2 = 0;
135 AEDesc theScript;
136 OSAID compiledScriptID = 0;
137 ComponentInstance scriptingComponent;
140 /* initialize theScript here because it is a struct */
141 theScript.dataHandle = NULL;
143 /* Open the component manager */
144 err2 = LoadScriptingComponent (&scriptingComponent);
145 if (err2)
146 return err2; /* <<< Fail quietly?? */
149 /* construct the AppleEvent Descriptor to contain the text of script */
150 AECreateDesc ('TEXT', text, strlen (text), &theScript);
153 err2 = OSASetActiveProc (scriptingComponent, proc, NULL);
154 if (err2)
155 goto CleanUp;
158 err2 = OSADoScript (scriptingComponent, &theScript, kOSANullScript, 'TEXT', 0, result);
159 if (err2)
161 OSAScriptError (scriptingComponent, kOSAErrorMessage, 'TEXT', result);
162 goto CleanUp;
166 CleanUp:
168 if (theScript.dataHandle)
169 AEDisposeDesc (&theScript);
171 if (scriptingComponent != 0)
172 CloseComponent (scriptingComponent);
175 return err2;
180 * This routine reads in a saved script file and executes
181 * the script contained within (from a 'scpt' resource.)
183 OSAError
184 ExecuteScriptFile (const char *theFilePath,
185 OSAActiveUPP proc,
186 AEDesc * result)
188 OSAError err2;
189 short resRefCon;
190 AEDesc theScript;
191 OSAID compiledScriptID, scriptResultID;
192 ComponentInstance scriptingComponent;
193 FSSpec theFile;
196 c2pstr (theFilePath);
197 FSMakeFSSpec (0, 0, (StringPtr) theFilePath, &theFile);
198 p2cstr ((StringPtr) theFilePath);
201 /* open a connection to the OSA */
202 err2 = LoadScriptingComponent (&scriptingComponent);
203 if (err2)
204 return err2; /* <<< Fail quietly?? */
207 err2 = OSASetActiveProc (scriptingComponent, proc, NULL);
208 if (err2)
209 goto error;
212 /* now, try and read in the script
213 * Open the script file and get the resource
215 resRefCon = FSpOpenResFile (&theFile, fsRdPerm);
216 if (resRefCon == -1)
217 return ResError ();
219 theScript.dataHandle = Get1IndResource (typeOSAGenericStorage, 1);
221 if ((err2 = ResError ()) || (err2 = resNotFound, theScript.dataHandle == NULL))
223 CloseResFile (resRefCon);
224 return err2;
227 theScript.descriptorType = typeOSAGenericStorage;
228 DetachResource (theScript.dataHandle);
229 CloseResFile (resRefCon);
230 err2 = noErr;
233 /* give a copy of the script to AppleScript */
234 err2 = OSALoad (scriptingComponent,
235 &theScript,
236 0L,
237 &compiledScriptID);
238 if (err2)
239 goto error;
241 AEDisposeDesc (&theScript);
242 theScript.dataHandle = NULL;
245 err2 = OSAExecute (scriptingComponent,
246 compiledScriptID,
247 kOSANullScript,
249 &scriptResultID);
251 if (compiledScriptID)
252 OSAScriptError (scriptingComponent, kOSAErrorMessage, 'TEXT', result);
254 if (err2)
255 goto error;
257 /* If there was an error, return it. If there was a result, return it. */
258 (void) OSADispose (scriptingComponent, compiledScriptID);
260 if (err2)
261 goto error;
262 else
263 goto done;
265 error:
266 if (theScript.dataHandle)
267 AEDisposeDesc (&theScript);
270 done:
273 return err2;
277 OSAError
278 LoadScriptingComponent (ComponentInstance * scriptingComponent)
280 OSAError err2;
282 /* Open a connection to the Open Scripting Architecture */
283 *scriptingComponent = OpenDefaultComponent (kOSAComponentType,
284 kOSAGenericScriptingComponentSubtype);
286 err2 = GetComponentInstanceError (*scriptingComponent);
288 return err2;