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