blob: 7fd68f64737e2d18c5b2ca0ce42617ae70cd8097 [file] [log] [blame]
Jack Jansen7cc57351998-08-18 14:54:11 +00001/*
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
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>
23
24
25
26OSAError LoadScriptingComponent (ComponentInstance * scriptingComponent);
27
28
29/*
30 * store the script as a compile script so that OSA
31 * components may load and execute the script easily
32 */
33OSAError
34CompileAndSave (const char *text,
35 const char *outfile,
36 OSAActiveUPP proc,
37 AEDesc * result)
38{
39
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;
48
49
50
51 /* Initialize theScript here because it is a struct */
52 theScript.dataHandle = NULL;
53 theCompiledScript.dataHandle = NULL;
54
55
56 /* open the component manager */
57 err2 = LoadScriptingComponent (&scriptingComponent);
58 if (err2)
59 return err2; /* <<< Fail quietly?? */
60
61
62 /* construct the AppleEvent Descriptor to contain the text of script */
63 AECreateDesc ('TEXT', text, strlen (text), &theScript);
64
65 err2 = OSACompile (scriptingComponent,
66 &theScript,
67 kOSAModeCompileIntoContext,
68 &scriptid);
69 if (err2)
70 {
71 OSAScriptError (scriptingComponent, kOSAErrorMessage, 'TEXT', result);
72 goto CleanUp;
73 }
74
75
76 err2 = OSAStore (scriptingComponent,
77 scriptid,
78 typeOSAGenericStorage,
79 kOSAModeCompileIntoContext,
80 &theCompiledScript);
81 if (err2)
82 {
83 OSAScriptError (scriptingComponent, kOSAErrorMessage, 'TEXT', result);
84 goto CleanUp;
85 }
86
87
88 c2pstr (outfile);
89 FSMakeFSSpec (0, 0, (StringPtr) outfile, &outfilespec);
90 p2cstr ((StringPtr) outfile);
91
92 FSpDelete (&outfilespec);
93
94 FSpCreateResFile (&outfilespec, 'ToyS', 'osas', smRoman);
95
96 saveres = CurResFile ();
97
98 if (saveres)
99 {
100 short myres = 0;
101 myres = FSpOpenResFile (&outfilespec, fsWrPerm);
102
103 UseResFile (myres);
104 AddResource (theCompiledScript.dataHandle, 'scpt', 128, "\p");
105 CloseResFile (myres);
106 UseResFile (saveres);
107 }
108
109
110CleanUp:
111
112 if (theScript.dataHandle)
113 AEDisposeDesc (&theScript);
114
115 if (theCompiledScript.dataHandle)
116 AEDisposeDesc (&theCompiledScript);
117
118 if (scriptid)
119 OSADispose (scriptingComponent, scriptid);
120
121 if (scriptingComponent != 0)
122 CloseComponent (scriptingComponent);
123
124
125 return err2;
126}
127
128
129OSAError
130CompileAndExecute (const char *text,
131 AEDesc * result,
132 OSAActiveUPP proc)
133{
134 OSAError err2 = 0;
135 AEDesc theScript;
136 OSAID compiledScriptID = 0;
137 ComponentInstance scriptingComponent;
138
139
140 /* initialize theScript here because it is a struct */
141 theScript.dataHandle = NULL;
142
143 /* Open the component manager */
144 err2 = LoadScriptingComponent (&scriptingComponent);
145 if (err2)
146 return err2; /* <<< Fail quietly?? */
147
148
149 /* construct the AppleEvent Descriptor to contain the text of script */
150 AECreateDesc ('TEXT', text, strlen (text), &theScript);
151
152
153 err2 = OSASetActiveProc (scriptingComponent, proc, NULL);
154 if (err2)
155 goto CleanUp;
156
157
158 err2 = OSADoScript (scriptingComponent, &theScript, kOSANullScript, 'TEXT', 0, result);
159 if (err2)
160 {
161 OSAScriptError (scriptingComponent, kOSAErrorMessage, 'TEXT', result);
162 goto CleanUp;
163 }
164
165
166CleanUp:
167
168 if (theScript.dataHandle)
169 AEDisposeDesc (&theScript);
170
171 if (scriptingComponent != 0)
172 CloseComponent (scriptingComponent);
173
174
175 return err2;
176}
177
178
179/*
180 * This routine reads in a saved script file and executes
181 * the script contained within (from a 'scpt' resource.)
182 */
183OSAError
184ExecuteScriptFile (const char *theFilePath,
185 OSAActiveUPP proc,
186 AEDesc * result)
187{
188 OSAError err2;
189 short resRefCon;
190 AEDesc theScript;
191 OSAID compiledScriptID, scriptResultID;
192 ComponentInstance scriptingComponent;
193 FSSpec theFile;
194
195
196 c2pstr (theFilePath);
197 FSMakeFSSpec (0, 0, (StringPtr) theFilePath, &theFile);
198 p2cstr ((StringPtr) theFilePath);
199
200
201 /* open a connection to the OSA */
202 err2 = LoadScriptingComponent (&scriptingComponent);
203 if (err2)
204 return err2; /* <<< Fail quietly?? */
205
206
207 err2 = OSASetActiveProc (scriptingComponent, proc, NULL);
208 if (err2)
209 goto error;
210
211
212 /* now, try and read in the script
213 * Open the script file and get the resource
214 */
215 resRefCon = FSpOpenResFile (&theFile, fsRdPerm);
216 if (resRefCon == -1)
217 return ResError ();
218
219 theScript.dataHandle = Get1IndResource (typeOSAGenericStorage, 1);
220
221 if ((err2 = ResError ()) || (err2 = resNotFound, theScript.dataHandle == NULL))
222 {
223 CloseResFile (resRefCon);
224 return err2;
225 }
226
227 theScript.descriptorType = typeOSAGenericStorage;
228 DetachResource (theScript.dataHandle);
229 CloseResFile (resRefCon);
230 err2 = noErr;
231
232
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;
240
241 AEDisposeDesc (&theScript);
242 theScript.dataHandle = NULL;
243
244
245 err2 = OSAExecute (scriptingComponent,
246 compiledScriptID,
247 kOSANullScript,
248 0,
249 &scriptResultID);
250
251 if (compiledScriptID)
252 OSAScriptError (scriptingComponent, kOSAErrorMessage, 'TEXT', result);
253
254 if (err2)
255 goto error;
256
257 /* If there was an error, return it. If there was a result, return it. */
258 (void) OSADispose (scriptingComponent, compiledScriptID);
259
260 if (err2)
261 goto error;
262 else
263 goto done;
264
265error:
266 if (theScript.dataHandle)
267 AEDisposeDesc (&theScript);
268
269
270done:
271
272
273 return err2;
274}
275
276
277OSAError
278LoadScriptingComponent (ComponentInstance * scriptingComponent)
279{
280 OSAError err2;
281
282 /* Open a connection to the Open Scripting Architecture */
283 *scriptingComponent = OpenDefaultComponent (kOSAComponentType,
284 kOSAGenericScriptingComponentSubtype);
285
286 err2 = GetComponentInstanceError (*scriptingComponent);
287
288 return err2;
289}