示例#1
0
 static extern OsaError OSALoad(ComponentInstance scriptingComponent, ref AEDesc sourceData,
                                OsaMode modeFlags, ref OsaId previousAndResultingScriptID);
示例#2
0
 static extern OsaError OSALoadExecute(ComponentInstance scriptingComponent, ref AEDesc scriptData,
                                       OsaId contextID, OsaMode modeFlags, ref OsaId resultingScriptValueID);
示例#3
0
 static extern OsaError OSADisplay(ComponentInstance scriptingComponent, OsaId scriptValueID,
                                   DescType desiredType, OsaMode modeFlags, out AEDesc resultingText);
示例#4
0
 static extern OsaError OSADispose(ComponentInstance scriptingComponent, OsaId scriptID);
示例#5
0
 static extern OsaError OSAExecute(ComponentInstance scriptingComponent, OsaId compiledScriptID,
                                   OsaId contextID, OsaMode modeFlags, out OsaId resultingScriptValueID);
示例#6
0
 static extern OsaError OSADoScript(ComponentInstance scriptingComponent, ref AEDesc sourceData,
                                    OsaId contextID, DescType desiredType, OsaMode modeFlags,
                                    ref AEDesc resultingText);
示例#7
0
        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);
                }
            }
        }