static OSErr ScriptHandler( const AppleEvent *event, AppleEvent *reply, long handlerRefcon) { OSStatus theErr; AEDescList theDesc; Size size; int tclErr = -1; Tcl_Interp *interp = (Tcl_Interp *) handlerRefcon; char errString[128]; /* * The do script event receives one parameter that should be data or a * file. */ theErr = AEGetParamDesc(event, keyDirectObject, typeWildCard, &theDesc); if (theErr != noErr) { sprintf(errString, "AEDoScriptHandler: GetParamDesc error %d", (int)theErr); theErr = AEPutParamPtr(reply, keyErrorString, typeChar, errString, strlen(errString)); } else if (MissedAnyParameters(event)) { /* * Return error if parameter is missing. */ sprintf(errString, "AEDoScriptHandler: extra parameters"); AEPutParamPtr(reply, keyErrorString, typeChar, errString, strlen(errString)); theErr = -1771; } else if (theDesc.descriptorType == (DescType) typeAlias && AEGetParamPtr(event, keyDirectObject, typeFSRef, NULL, NULL, 0, &size) == noErr && size == sizeof(FSRef)) { /* * We've had a file sent to us. Source it. */ FSRef file; theErr = AEGetParamPtr(event, keyDirectObject, typeFSRef, NULL, &file, size, NULL); if (theErr == noErr) { Tcl_DString scriptName; theErr = FSRefToDString(&file, &scriptName); if (theErr == noErr) { tclErr = Tcl_EvalFile(interp, Tcl_DStringValue(&scriptName)); Tcl_DStringFree(&scriptName); } else { sprintf(errString, "AEDoScriptHandler: file not found"); AEPutParamPtr(reply, keyErrorString, typeChar, errString, strlen(errString)); } } } else if (AEGetParamPtr(event, keyDirectObject, typeUTF8Text, NULL, NULL, 0, &size) == noErr && size) { /* * We've had some data sent to us. Evaluate it. */ char *data = ckalloc(size + 1); theErr = AEGetParamPtr(event, keyDirectObject, typeUTF8Text, NULL, data, size, NULL); if (theErr == noErr) { tclErr = Tcl_EvalEx(interp, data, size, TCL_EVAL_GLOBAL); } } else { /* * Umm, don't recognize what we've got... */ sprintf(errString, "AEDoScriptHandler: invalid script type '%-4.4s', " "must be 'alis' or coercable to 'utf8'", (char*) &theDesc.descriptorType); AEPutParamPtr(reply, keyErrorString, typeChar, errString, strlen(errString)); theErr = -1770; } /* * If we actually go to run Tcl code - put the result in the reply. */ if (tclErr >= 0) { int reslen; const char *result = Tcl_GetStringFromObj(Tcl_GetObjResult(interp), &reslen); if (tclErr == TCL_OK) { AEPutParamPtr(reply, keyDirectObject, typeChar, result, reslen); } else { AEPutParamPtr(reply, keyErrorString, typeChar, result, reslen); AEPutParamPtr(reply, keyErrorNumber, typeSInt32, (Ptr) &tclErr, sizeof(int)); } } AEDisposeDesc(&theDesc); return theErr; }
static OSErr ScriptHandler( const AppleEvent *event, AppleEvent *reply, SRefCon handlerRefcon) { OSStatus theErr; AEDescList theDesc; int tclErr = -1; Tcl_Interp *interp = (Tcl_Interp *) handlerRefcon; char errString[128]; /* * The do script event receives one parameter that should be data or a * file. */ theErr = AEGetParamDesc(event, keyDirectObject, typeWildCard, &theDesc); if (theErr != noErr) { sprintf(errString, "AEDoScriptHandler: GetParamDesc error %d", (int)theErr); theErr = AEPutParamPtr(reply, keyErrorString, typeChar, errString, strlen(errString)); } else if (MissedAnyParameters(event)) { /* * Return error if parameter is missing. */ sprintf(errString, "AEDoScriptHandler: extra parameters"); AEPutParamPtr(reply, keyErrorString, typeChar, errString, strlen(errString)); theErr = -1771; } else if (theDesc.descriptorType == (DescType) typeChar) { /* * We've had some data sent to us. Evaluate it. */ Tcl_DString encodedText; short i; Size size = AEGetDescDataSize(&theDesc); char *data = ckalloc(size + 1); AEGetDescData(&theDesc, data, size); data[size] = 0; for (i = 0; i < size; i++) { if (data[i] == '\r') { data[i] = '\n'; } } AEReplaceDescData(theDesc.descriptorType, data, size + 1, &theDesc); Tcl_ExternalToUtfDString(NULL, data, size, &encodedText); tclErr = Tcl_EvalEx(interp, Tcl_DStringValue(&encodedText), Tcl_DStringLength(&encodedText), TCL_EVAL_GLOBAL); Tcl_DStringFree(&encodedText); } else if (theDesc.descriptorType == (DescType) typeAlias) { /* * We've had a file sent to us. Source it. */ Boolean dummy; FSRef file; Size theSize = AEGetDescDataSize(&theDesc); AliasPtr alias = (AliasPtr) ckalloc(theSize); if (alias) { AEGetDescData(&theDesc, alias, theSize); theErr = FSResolveAlias(NULL, &alias, &file, &dummy); ckfree((char*)alias); } else { theErr = memFullErr; } if (theErr == noErr) { Tcl_DString scriptName; theErr = FSRefToDString(&file, &scriptName); if (theErr == noErr) { Tcl_EvalFile(interp, Tcl_DStringValue(&scriptName)); Tcl_DStringFree(&scriptName); } } else { sprintf(errString, "AEDoScriptHandler: file not found"); AEPutParamPtr(reply, keyErrorString, typeChar, errString, strlen(errString)); } } else { /* * Umm, don't recognize what we've got... */ sprintf(errString, "AEDoScriptHandler: invalid script type '%-4.4s'," " must be 'alis' or 'TEXT'", (char*) &theDesc.descriptorType); AEPutParamPtr(reply, keyErrorString, typeChar, errString, strlen(errString)); theErr = -1770; } /* * If we actually go to run Tcl code - put the result in the reply. */ if (tclErr >= 0) { int reslen; const char *result = Tcl_GetStringFromObj(Tcl_GetObjResult(interp), &reslen); if (tclErr == TCL_OK) { AEPutParamPtr(reply, keyDirectObject, typeChar, result, reslen); } else { AEPutParamPtr(reply, keyErrorString, typeChar, result, reslen); AEPutParamPtr(reply, keyErrorNumber, typeSInt32, (Ptr) &tclErr, sizeof(int)); } } AEDisposeDesc(&theDesc); return theErr; }