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>
24 #ifdef TARGET_API_MAC_CARBON
30 strncpy((char *)c
+1, (char *)c
, len
);
34 static c2pstr(const char *cc
)
36 char *c
= (char *)cc
; /* Ouch */
39 if ( len
> 255 ) len
= 255;
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
53 CompileAndSave (const char *text
,
61 OSAID compiledScriptID
= 0;
62 ComponentInstance scriptingComponent
;
64 AEDesc theCompiledScript
;
65 OSAID scriptid
= kOSANullScript
;
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
);
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
,
86 kOSAModeCompileIntoContext
,
90 OSAScriptError (scriptingComponent
, kOSAErrorMessage
, 'TEXT', result
);
95 err2
= OSAStore (scriptingComponent
,
97 typeOSAGenericStorage
,
98 kOSAModeCompileIntoContext
,
102 OSAScriptError (scriptingComponent
, kOSAErrorMessage
, 'TEXT', result
);
108 FSMakeFSSpec (0, 0, (StringPtr
) outfile
, &outfilespec
);
109 p2cstr ((StringPtr
) outfile
);
111 FSpDelete (&outfilespec
);
113 FSpCreateResFile (&outfilespec
, 'ToyS', 'osas', smRoman
);
115 saveres
= CurResFile ();
120 myres
= FSpOpenResFile (&outfilespec
, fsWrPerm
);
123 AddResource (theCompiledScript
.dataHandle
, 'scpt', 128, "\p");
124 CloseResFile (myres
);
125 UseResFile (saveres
);
131 if (theScript
.dataHandle
)
132 AEDisposeDesc (&theScript
);
134 if (theCompiledScript
.dataHandle
)
135 AEDisposeDesc (&theCompiledScript
);
138 OSADispose (scriptingComponent
, scriptid
);
140 if (scriptingComponent
!= 0)
141 CloseComponent (scriptingComponent
);
149 CompileAndExecute (const char *text
,
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
);
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
);
177 err2
= OSADoScript (scriptingComponent
, &theScript
, kOSANullScript
, 'TEXT', 0, result
);
180 OSAScriptError (scriptingComponent
, kOSAErrorMessage
, 'TEXT', result
);
187 if (theScript
.dataHandle
)
188 AEDisposeDesc (&theScript
);
190 if (scriptingComponent
!= 0)
191 CloseComponent (scriptingComponent
);
199 * This routine reads in a saved script file and executes
200 * the script contained within (from a 'scpt' resource.)
203 ExecuteScriptFile (const char *theFilePath
,
210 OSAID compiledScriptID
, scriptResultID
;
211 ComponentInstance scriptingComponent
;
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
);
223 return err2
; /* <<< Fail quietly?? */
226 err2
= OSASetActiveProc (scriptingComponent
, proc
, NULL
);
231 /* now, try and read in the script
232 * Open the script file and get the resource
234 resRefCon
= FSpOpenResFile (&theFile
, fsRdPerm
);
238 theScript
.dataHandle
= Get1IndResource (typeOSAGenericStorage
, 1);
240 if ((err2
= ResError ()) || (err2
= resNotFound
, theScript
.dataHandle
== NULL
))
242 CloseResFile (resRefCon
);
246 theScript
.descriptorType
= typeOSAGenericStorage
;
247 DetachResource (theScript
.dataHandle
);
248 CloseResFile (resRefCon
);
252 /* give a copy of the script to AppleScript */
253 err2
= OSALoad (scriptingComponent
,
260 AEDisposeDesc (&theScript
);
261 theScript
.dataHandle
= NULL
;
264 err2
= OSAExecute (scriptingComponent
,
270 if (compiledScriptID
)
271 OSAScriptError (scriptingComponent
, kOSAErrorMessage
, 'TEXT', result
);
276 /* If there was an error, return it. If there was a result, return it. */
277 (void) OSADispose (scriptingComponent
, compiledScriptID
);
285 if (theScript
.dataHandle
)
286 AEDisposeDesc (&theScript
);
297 LoadScriptingComponent (ComponentInstance
* scriptingComponent
)
301 /* Open a connection to the Open Scripting Architecture */
302 *scriptingComponent
= OpenDefaultComponent (kOSAComponentType
,
303 kOSAGenericScriptingComponentSubtype
);
305 err2
= GetComponentInstanceError (*scriptingComponent
);