home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************
- *
- * OSASupport.c
- *
- ****************************************************************************/
-
- #include <stdio.h>
- #include <AppleScript.h>
- #include <Components.h>
-
- #include "OSASupport.h"
- #include "Assertion.h"
- #include "StringUtils.h"
- #include "OSLHelpers.h"
-
- static ComponentInstance fgScriptingComponentInstance;
- static ComponentInstance fgGenericScriptingComponent;
-
- /*****************************************************************************
- *
- * InitializeOSASupport
- *
- *****************************************************************************/
-
- Boolean InitializeOSASupport(void)
- {
- OSErr error = noErr;
-
- if (fgScriptingComponentInstance == 0)
- error = ConnectToScriptingComponent();
-
- return (error == noErr && fgScriptingComponentInstance != 0);
- }
-
- /*****************************************************************************
- *
- * HasOSASupport
- *
- *****************************************************************************/
-
- Boolean HasOSASupport(void)
- {
- return (fgScriptingComponentInstance != 0);
- }
-
- /*****************************************************************************
- *
- * GetScriptingComponentInstance
- *
- *****************************************************************************/
-
- ComponentInstance
- GetScriptingComponentInstance(void)
- {
- return fgScriptingComponentInstance;
- }
-
- /*****************************************************************************
- *
- * ConnectToScriptingComponent
- *
- *****************************************************************************/
-
- #define kMRGMinStackSize 65536L
- #define kMRGPreferredStackSize 65536L
- #define kMRGMaxStackSize 65536L
-
- OSErr
- ConnectToScriptingComponent(void)
- {
- OSErr error = noErr;
- OSErr ignoreError = noErr;
- ComponentDescription description;
- Component component = 0;
-
- Str255 name;
- AEDesc componentName = {typeNull, nil};
-
- fgScriptingComponentInstance = 0;
-
- // What we're looking for:
- // Defines are from OSA.h
-
- description.componentType = kOSAComponentType;
- description.componentSubType = kOSAGenericScriptingComponentSubtype;
- description.componentManufacturer = kAnyComponentManufacturer;
- description.componentFlags = kOSASupportsCompiling;
- description.componentFlagsMask = kOSASupportsCompiling;
-
- component = FindNextComponent((Component)0, &description);
-
- if (component == 0)
- {
- error = errOSACantOpenComponent;
- }
- else
- {
- ignoreError = GetComponentInfo(component, &description, nil, nil, nil);
- fgScriptingComponentInstance = OpenComponent(component);
- ignoreError = OSAScriptingComponentName(fgScriptingComponentInstance, &componentName);
- DescToPString(&componentName, name, sizeof(name));
- }
-
- if (fgScriptingComponentInstance == 0)
- error = errOSACantOpenComponent;
- // else
- // error = ASInit(fgScriptingComponentInstance, // Attempt to solve the stack overflow problem with Quark Converter script
- // kOSAModeNull,
- // kMRGMinStackSize, // commented out to remove write to NIL error
- // kMRGPreferredStackSize,
- // kMRGMaxStackSize,
- // kASDefaultMinHeapSize,
- // kASDefaultPreferredHeapSize,
- // kASDefaultMaxHeapSize);
-
- return error;
- }
-
- /*****************************************************************************
- *
- * LoadCompiledScript
- * Load the script data from the file, and return its OSAID value
- *
- *****************************************************************************/
-
- OSAError
- LoadCompiledScript(FSSpec *fsSpec, OSAID *scriptID)
- {
- short fileRef = 0;
- OSAError error = noErr;
- Handle scriptHandle = nil;
- AEDesc scriptDesc = {typeNull, nil};
-
- // Now open the Script the user selected
-
- fileRef = FSpOpenResFile(fsSpec,fsRdPerm);
- error = ResError();
-
- if (error == noErr)
- {
- scriptHandle = Get1Resource(kOSAScriptResourceType, 128);
-
- if (scriptHandle != nil)
- {
- scriptDesc.descriptorType = typeOSAGenericStorage;
- scriptDesc.dataHandle = scriptHandle;
-
- error = OSALoad(fgScriptingComponentInstance,
- &scriptDesc,
- kOSAModeNull,
- scriptID);
- ReleaseResource(scriptHandle);
- }
- CloseResFile(fileRef);
- fileRef = 0;
- }
- return error;
- }
-
- /*****************************************************************************
- *
- * ExecuteScript
- *
- *****************************************************************************/
-
- OSAError
- ExecuteScript(OSAID scriptID)
- {
- OSAError error = noErr;
- OSAError ignoreError = noErr;
- OSAID resultID;
-
- short startOffset;
- short endOffset;
-
- error = OSAExecute(fgScriptingComponentInstance,
- scriptID,
- kOSANullScript,
- kOSAModeNull,
- &resultID);
-
- ignoreError = OSADispose(fgScriptingComponentInstance, scriptID);
- ignoreError = OSADispose(fgScriptingComponentInstance, resultID);
-
- if (error == errOSAScriptError)
- {
- error = ExamineScriptError(fgScriptingComponentInstance, &startOffset, &endOffset);
- }
-
- return error;
- }
-
- //----------------------------------------------------------------------------------
-
- OSErr
- ExamineScriptError(ComponentInstance componentInstance, short *startOffset, short *endOffset)
- {
-
- OSErr error = noErr;
-
- short scriptError;
- OSErr ignoreError;
- OSAError theOSAError;
-
- AEDesc errorDesc = {typeNull, nil};
- AEDesc recordDesc = {typeNull, nil};
- DescType actualType;
- Size actualSize;
-
- char msg0[40];
- char msg1[256];
- char msg2[256];
-
- sprintf(msg0, "Script error");
- sprintf(msg1, "No further information available");
- msg2[0] = '\0';
-
- // Extract and cache the error number
-
- theOSAError = OSAScriptError(componentInstance,
- kOSAErrorNumber,
- typeShortInteger,
- &errorDesc);
- if (theOSAError == noErr)
- {
- scriptError = **(short**)(errorDesc.dataHandle);
-
- sprintf(msg0, "Script error %d", scriptError);
-
- ignoreError = AEDisposeDesc(&errorDesc);
-
- if (scriptError == userCanceledErr)
- return noErr;
- }
-
- // Extract and cache the error message
-
- theOSAError = OSAScriptError(componentInstance,
- kOSAErrorMessage,
- typeChar,
- &errorDesc);
-
- if (theOSAError == noErr)
- {
- TextToCString(errorDesc.dataHandle, msg1, sizeof(msg1));
-
- ignoreError = AEDisposeDesc(&errorDesc);
- }
-
- // Extract and cache the source start and ending offsets
-
- theOSAError = OSAScriptError(componentInstance,
- kOSAErrorRange,
- typeOSAErrorRange,
- &errorDesc);
- if (theOSAError == noErr)
- {
- error = AECoerceDesc(&errorDesc, typeAERecord, &recordDesc);
-
- if (error == noErr)
- {
- ignoreError = AEDisposeDesc(&errorDesc);
-
- error = AEGetKeyPtr(&recordDesc,
- keyOSASourceStart,
- typeShortInteger,
- &actualType,
- startOffset,
- sizeof(short),
- &actualSize);
-
- if (error == noErr)
- {
- error = AEGetKeyPtr(&recordDesc,
- keyOSASourceEnd,
- typeShortInteger,
- &actualType,
- endOffset,
- sizeof(short),
- &actualSize);
-
- if (!(*startOffset == 0 && *endOffset == 0))
- sprintf(msg2, "Source location: %d - %d", *startOffset, *endOffset);
- }
-
- ignoreError = AEDisposeDesc(&recordDesc);
- }
- }
-
- // Display the error message here!
-
- SysBeep(2);
-
- return noErr;
- }
-
- //----------------------------------------------------------------------------------
-
-
-