openfile(): Go back to opening the files in text mode. This undoes
[python/dscho.git] / Mac / Contrib / osam / ScriptRunner.c
blobbf77d6bfd4a4d644e82264ca99d2bd7376a7bfb7
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);
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;
148 OSAError
149 CompileAndExecute (const char *text,
150 AEDesc * result,
151 OSAActiveUPP proc)
153 OSAError err2 = 0;
154 AEDesc theScript;
155 OSAID compiledScriptID = 0;
156 ComponentInstance scriptingComponent;
159 /* initialize theScript here because it is a struct */
160 theScript.dataHandle = NULL;
162 /* Open the component manager */
163 err2 = LoadScriptingComponent (&scriptingComponent);
164 if (err2)
165 return err2; /* <<< Fail quietly?? */
168 /* construct the AppleEvent Descriptor to contain the text of script */
169 AECreateDesc ('TEXT', text, strlen (text), &theScript);
172 err2 = OSASetActiveProc (scriptingComponent, proc, NULL);
173 if (err2)
174 goto CleanUp;
177 err2 = OSADoScript (scriptingComponent, &theScript, kOSANullScript, 'TEXT', 0, result);
178 if (err2)
180 OSAScriptError (scriptingComponent, kOSAErrorMessage, 'TEXT', result);
181 goto CleanUp;
185 CleanUp:
187 if (theScript.dataHandle)
188 AEDisposeDesc (&theScript);
190 if (scriptingComponent != 0)
191 CloseComponent (scriptingComponent);
194 return err2;
199 * This routine reads in a saved script file and executes
200 * the script contained within (from a 'scpt' resource.)
202 OSAError
203 ExecuteScriptFile (const char *theFilePath,
204 OSAActiveUPP proc,
205 AEDesc * result)
207 OSAError err2;
208 short resRefCon;
209 AEDesc theScript;
210 OSAID compiledScriptID, scriptResultID;
211 ComponentInstance scriptingComponent;
212 FSSpec theFile;
215 c2pstr (theFilePath);
216 FSMakeFSSpec (0, 0, (StringPtr) theFilePath, &theFile);
217 p2cstr ((StringPtr) theFilePath);
220 /* open a connection to the OSA */
221 err2 = LoadScriptingComponent (&scriptingComponent);
222 if (err2)
223 return err2; /* <<< Fail quietly?? */
226 err2 = OSASetActiveProc (scriptingComponent, proc, NULL);
227 if (err2)
228 goto error;
231 /* now, try and read in the script
232 * Open the script file and get the resource
234 resRefCon = FSpOpenResFile (&theFile, fsRdPerm);
235 if (resRefCon == -1)
236 return ResError ();
238 theScript.dataHandle = Get1IndResource (typeOSAGenericStorage, 1);
240 if ((err2 = ResError ()) || (err2 = resNotFound, theScript.dataHandle == NULL))
242 CloseResFile (resRefCon);
243 return err2;
246 theScript.descriptorType = typeOSAGenericStorage;
247 DetachResource (theScript.dataHandle);
248 CloseResFile (resRefCon);
249 err2 = noErr;
252 /* give a copy of the script to AppleScript */
253 err2 = OSALoad (scriptingComponent,
254 &theScript,
255 0L,
256 &compiledScriptID);
257 if (err2)
258 goto error;
260 AEDisposeDesc (&theScript);
261 theScript.dataHandle = NULL;
264 err2 = OSAExecute (scriptingComponent,
265 compiledScriptID,
266 kOSANullScript,
268 &scriptResultID);
270 if (compiledScriptID)
271 OSAScriptError (scriptingComponent, kOSAErrorMessage, 'TEXT', result);
273 if (err2)
274 goto error;
276 /* If there was an error, return it. If there was a result, return it. */
277 (void) OSADispose (scriptingComponent, compiledScriptID);
279 if (err2)
280 goto error;
281 else
282 goto done;
284 error:
285 if (theScript.dataHandle)
286 AEDisposeDesc (&theScript);
289 done:
292 return err2;
296 OSAError
297 LoadScriptingComponent (ComponentInstance * scriptingComponent)
299 OSAError err2;
301 /* Open a connection to the Open Scripting Architecture */
302 *scriptingComponent = OpenDefaultComponent (kOSAComponentType,
303 kOSAGenericScriptingComponentSubtype);
305 err2 = GetComponentInstanceError (*scriptingComponent);
307 return err2;