/* LowRunAppleScript compiles and runs an AppleScript provided as text in the buffer pointed to by text. textLength bytes will be compiled from this buffer and run as an AppleScript using all of the default environment and execution settings. resultData must be non-NULL, and should have been previously initialised, for example with _hs_initNull. The result returned by the execution command will be returned as typeUTF8Text in this descriptor record (or typeNull if there is no result information). If the function returns errOSAScriptError, then resultData will be set to a descriptive error message describing the error (if one is available). */ OSStatus LowRunAppleScript(const void* text, long textLength, AEDesc *resultData) { ComponentInstance theComponent; AEDesc scriptTextDesc; OSStatus err; OSAID scriptID, resultID; /* set up locals to a known state */ theComponent = NULL; AECreateDesc(typeNull, NULL, 0, &scriptTextDesc); scriptID = kOSANullScript; resultID = kOSANullScript; /* open the scripting component */ theComponent = OpenDefaultComponent(kOSAComponentType, typeAppleScript); if (theComponent == NULL) { err = paramErr; goto bail; } /* put the script text into an aedesc */ err = AECreateDesc(typeUTF8Text, text, textLength, &scriptTextDesc); if (err != noErr) goto bail; /* compile the script */ err = OSACompile(theComponent, &scriptTextDesc, kOSAModeNull, &scriptID); if (err != noErr) goto bail; /* run the script/get the result */ err = OSAExecute(theComponent, scriptID, kOSANullScript, kOSAModeNull, &resultID); if (resultData != NULL) { if (err == noErr && resultID != kOSANullScript) { OSADisplay(theComponent, resultID, typeUTF8Text, kOSAModeNull, resultData); } } bail: if (err == errOSAScriptError) { AECreateDesc(typeNull, NULL, 0, resultData); OSAScriptError(theComponent, kOSAErrorMessage, typeUTF8Text, resultData); } AEDisposeDesc(&scriptTextDesc); if (scriptID != kOSANullScript) OSADispose(theComponent, scriptID); if (resultID != kOSANullScript) OSADispose(theComponent, resultID); if (theComponent != NULL) CloseComponent(theComponent); return err; }
static PyObject *OSAObj_OSADispose(OSAComponentInstanceObject *_self, PyObject *_args) { PyObject *_res = NULL; OSAError _err; OSAID scriptID; #ifndef OSADispose PyMac_PRECHECK(OSADispose); #endif if (!PyArg_ParseTuple(_args, "l", &scriptID)) return NULL; _err = OSADispose(_self->ob_itself, scriptID); if (_err != noErr) return PyMac_Error(_err); Py_INCREF(Py_None); _res = Py_None; return _res; }
OSErr sourceStringOfAEDesc(ComponentInstance component, AEDesc* inDesc, AEDesc *outDesc) { OSErr err = noErr; OSAID script_id = kOSANullScript; CFAttributedStringRef source_text = NULL; err = OSACoerceFromDesc(component, inDesc, kOSAModeNull, &script_id); if (noErr != err) goto bail; err = OSACopySourceString(component, script_id, kOSAModeNull, &source_text); if (noErr != err) goto bail; CFStringRef plain_text = CFAttributedStringGetString(source_text); #if useLog CFShow(plain_text); #endif err = AEDescCreateWithCFString(plain_text, kCFStringEncodingUTF8, outDesc); bail: OSADispose(component, script_id); safeRelease(source_text); return err; }