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>
11 * Jay Painter <jpaint@serv.net> <jpaint@gimp.org> <jpaint@real.com>
16 #include <Resources.h>
20 #include "ScriptRunner.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
34 CompileAndSave (const char *text
,
42 OSAID compiledScriptID
= 0;
43 ComponentInstance scriptingComponent
;
45 AEDesc theCompiledScript
;
46 OSAID scriptid
= kOSANullScript
;
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
);
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
,
67 kOSAModeCompileIntoContext
,
71 OSAScriptError (scriptingComponent
, kOSAErrorMessage
, 'TEXT', result
);
76 err2
= OSAStore (scriptingComponent
,
78 typeOSAGenericStorage
,
79 kOSAModeCompileIntoContext
,
83 OSAScriptError (scriptingComponent
, kOSAErrorMessage
, 'TEXT', result
);
89 FSMakeFSSpec (0, 0, (StringPtr
) outfile
, &outfilespec
);
90 p2cstr ((StringPtr
) outfile
);
92 FSpDelete (&outfilespec
);
94 FSpCreateResFile (&outfilespec
, 'ToyS', 'osas', smRoman
);
96 saveres
= CurResFile ();
101 myres
= FSpOpenResFile (&outfilespec
, fsWrPerm
);
104 AddResource (theCompiledScript
.dataHandle
, 'scpt', 128, "\p");
105 CloseResFile (myres
);
106 UseResFile (saveres
);
112 if (theScript
.dataHandle
)
113 AEDisposeDesc (&theScript
);
115 if (theCompiledScript
.dataHandle
)
116 AEDisposeDesc (&theCompiledScript
);
119 OSADispose (scriptingComponent
, scriptid
);
121 if (scriptingComponent
!= 0)
122 CloseComponent (scriptingComponent
);
130 CompileAndExecute (const char *text
,
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
);
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
);
158 err2
= OSADoScript (scriptingComponent
, &theScript
, kOSANullScript
, 'TEXT', 0, result
);
161 OSAScriptError (scriptingComponent
, kOSAErrorMessage
, 'TEXT', result
);
168 if (theScript
.dataHandle
)
169 AEDisposeDesc (&theScript
);
171 if (scriptingComponent
!= 0)
172 CloseComponent (scriptingComponent
);
180 * This routine reads in a saved script file and executes
181 * the script contained within (from a 'scpt' resource.)
184 ExecuteScriptFile (const char *theFilePath
,
191 OSAID compiledScriptID
, scriptResultID
;
192 ComponentInstance scriptingComponent
;
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
);
204 return err2
; /* <<< Fail quietly?? */
207 err2
= OSASetActiveProc (scriptingComponent
, proc
, NULL
);
212 /* now, try and read in the script
213 * Open the script file and get the resource
215 resRefCon
= FSpOpenResFile (&theFile
, fsRdPerm
);
219 theScript
.dataHandle
= Get1IndResource (typeOSAGenericStorage
, 1);
221 if ((err2
= ResError ()) || (err2
= resNotFound
, theScript
.dataHandle
== NULL
))
223 CloseResFile (resRefCon
);
227 theScript
.descriptorType
= typeOSAGenericStorage
;
228 DetachResource (theScript
.dataHandle
);
229 CloseResFile (resRefCon
);
233 /* give a copy of the script to AppleScript */
234 err2
= OSALoad (scriptingComponent
,
241 AEDisposeDesc (&theScript
);
242 theScript
.dataHandle
= NULL
;
245 err2
= OSAExecute (scriptingComponent
,
251 if (compiledScriptID
)
252 OSAScriptError (scriptingComponent
, kOSAErrorMessage
, 'TEXT', result
);
257 /* If there was an error, return it. If there was a result, return it. */
258 (void) OSADispose (scriptingComponent
, compiledScriptID
);
266 if (theScript
.dataHandle
)
267 AEDisposeDesc (&theScript
);
278 LoadScriptingComponent (ComponentInstance
* scriptingComponent
)
282 /* Open a connection to the Open Scripting Architecture */
283 *scriptingComponent
= OpenDefaultComponent (kOSAComponentType
,
284 kOSAGenericScriptingComponentSubtype
);
286 err2
= GetComponentInstanceError (*scriptingComponent
);