static extern OsaError OSALoad(ComponentInstance scriptingComponent, ref AEDesc sourceData, OsaMode modeFlags, ref OsaId previousAndResultingScriptID);
static extern OsaError OSALoadExecute(ComponentInstance scriptingComponent, ref AEDesc scriptData, OsaId contextID, OsaMode modeFlags, ref OsaId resultingScriptValueID);
static extern OsaError OSADisplay(ComponentInstance scriptingComponent, OsaId scriptValueID, DescType desiredType, OsaMode modeFlags, out AEDesc resultingText);
static extern OsaError OSADispose(ComponentInstance scriptingComponent, OsaId scriptID);
static extern OsaError OSAExecute(ComponentInstance scriptingComponent, OsaId compiledScriptID, OsaId contextID, OsaMode modeFlags, out OsaId resultingScriptValueID);
static extern OsaError OSADoScript(ComponentInstance scriptingComponent, ref AEDesc sourceData, OsaId contextID, DescType desiredType, OsaMode modeFlags, ref AEDesc resultingText);
static OsaError Run(bool compile, ref AEDesc scriptData, out string value) { var component = ComponentManager.OpenDefaultComponent((OSType)(int)OsaType.OsaComponent, (OSType)(int)OsaType.AppleScript); if (component.IsNull) { throw new Exception("Could not load component"); } AEDesc resultData = new AEDesc(); OsaId contextId = new OsaId(), scriptId = new OsaId(), resultId = new OsaId(); try { AppleEvent.AECreateDescNull(out resultData); //apparently UnicodeText doesn't work var resultType = new DescType() { Value = (OSType)"TEXT" }; //var result = OSADoScript (component, ref sourceData, contextId, resultType, OsaMode.Default, ref resultData); OsaError result; if (compile) { result = OSACompile(component, ref scriptData, OsaMode.Default, ref scriptId); } else { result = OSALoad(component, ref scriptData, OsaMode.Default, ref scriptId); } if (result == OsaError.Success) { result = OSAExecute(component, scriptId, contextId, OsaMode.Default, out resultId); if (result == OsaError.Success) { result = OSADisplay(component, resultId, resultType, OsaMode.Default, out resultData); if (result == OsaError.Success) { value = AppleEvent.GetStringFromAEDesc(ref resultData); return(result); } } } AEDesc errorDesc = new AEDesc(); try { AppleEvent.AECreateDescNull(out resultData); if (OsaError.Success == OSAScriptError(component, OsaErrorSelector.Message, resultType, out errorDesc)) { value = AppleEvent.GetStringFromAEDesc(ref errorDesc); return(result); } else { throw new InvalidOperationException(string.Format("Unexpected result {0}", (long)result)); } } finally { AppleEvent.AEDisposeDesc(ref errorDesc); } } finally { AppleEvent.AEDisposeDesc(ref scriptData); AppleEvent.AEDisposeDesc(ref resultData); if (!contextId.IsZero) { OSADispose(component, contextId); } if (!scriptId.IsZero) { OSADispose(component, scriptId); } if (!resultId.IsZero) { OSADispose(component, resultId); } if (!component.IsNull) { ComponentManager.CloseComponent(component); } } }