使用参数从 Cocoa App 执行 Applescript [英] Execute Applescript from Cocoa App with params

查看:25
本文介绍了使用参数从 Cocoa App 执行 Applescript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何从传递参数的可可应用程序执行 applescript.我已经看到在 stackoverflow 的其他问题中执行不带参数的 applescripts 是多么容易,但是使用 NSAppleScript 类,在其中,我没有看到任何方法可以解决我的问题.有没有人有任何想法.

I would like to know how to execute an applescript from a cocoa application passing parameters. I have seen how easy is to execute applescripts with no parameters in other questions here at stackoverflow, however the use NSAppleScript class, in which, i haven't seen no method that solve my problem. Does anyone have any idea.

我想要一个与此外壳具有相同效果的 Cocoa 代码:

I would like a Cocoa code with the same effect o this shell:

osascript teste.applescript "snow:Users:MyUser:Desktop:MyFolder" "snow:Users:MyUser:Desktop:Example:" 

所以它可能会运行这个 AppleScript.

So it may run this AppleScript.

on run argv

    set source to (item 1 of argv)

    set destiny to (item 2 of argv)

    tell application "Finder" to make new alias file at destiny to source
    0

end run

感谢任何帮助.提前致谢.

Any help is appreciated. Thanks in advance.

推荐答案

我发现遵循这段代码更容易.我从这里获取了一个代码,并根据我的目的对其进行了修改.

I found easier to follow this piece code. I took a code from here and modified it to my purpose.

 - (BOOL) executeScriptWithPath:(NSString*)path function:(NSString*)functionName andArguments:(NSArray*)scriptArgumentArray
{
    BOOL executionSucceed = NO;

    NSAppleScript           * appleScript;
    NSAppleEventDescriptor  * thisApplication, *containerEvent;
    NSURL                   * pathURL = [NSURL fileURLWithPath:path];

    NSDictionary * appleScriptCreationError = nil;
    appleScript = [[NSAppleScript alloc] initWithContentsOfURL:pathURL error:&appleScriptCreationError];

    if (appleScriptCreationError)
    {
        NSLog([NSString stringWithFormat:@"Could not instantiate applescript %@",appleScriptCreationError]);
    }
    else 
    {
        if (functionName && [functionName length])
        {
            /* If we have a functionName (and potentially arguments), we build
             * an NSAppleEvent to execute the script. */

            //Get a descriptor for ourself
            int pid = [[NSProcessInfo processInfo] processIdentifier];
            thisApplication = [NSAppleEventDescriptor descriptorWithDescriptorType:typeKernelProcessID
                                                                             bytes:&pid
                                                                            length:sizeof(pid)];

            //Create the container event

            //We need these constants from the Carbon OpenScripting framework, but we don't actually need Carbon.framework...
            #define kASAppleScriptSuite 'ascr'
            #define kASSubroutineEvent  'psbr'
            #define keyASSubroutineName 'snam'
            containerEvent = [NSAppleEventDescriptor appleEventWithEventClass:kASAppleScriptSuite
                                                                      eventID:kASSubroutineEvent
                                                             targetDescriptor:thisApplication
                                                                     returnID:kAutoGenerateReturnID
                                                                transactionID:kAnyTransactionID];

            //Set the target function
            [containerEvent setParamDescriptor:[NSAppleEventDescriptor descriptorWithString:functionName]
                                    forKeyword:keyASSubroutineName];

            //Pass arguments - arguments is expecting an NSArray with only NSString objects
            if ([scriptArgumentArray count])
            {
                NSAppleEventDescriptor  *arguments = [[NSAppleEventDescriptor alloc] initListDescriptor];
                NSString                *object;

                for (object in scriptArgumentArray) {
                    [arguments insertDescriptor:[NSAppleEventDescriptor descriptorWithString:object]
                                        atIndex:([arguments numberOfItems] + 1)]; //This +1 seems wrong... but it's not
                }

                [containerEvent setParamDescriptor:arguments forKeyword:keyDirectObject];
                [arguments release];
            }

            //Execute the event
            NSDictionary * executionError = nil;
            NSAppleEventDescriptor * result = [appleScript executeAppleEvent:containerEvent error:&executionError];
            if (executionError != nil)
            {
                NSLog([NSString stringWithFormat:@"error while executing script. Error %@",executionError]);

            }
            else 
            {
                NSLog(@"Script execution has succeed. Result(%@)",result);          
                executionSucceed = YES;
            }
        } 
        else 
        {
            NSDictionary * executionError = nil;
            NSAppleEventDescriptor * result = [appleScript executeAndReturnError:&executionError];

            if (executionError != nil)
            {
                NSLog([NSString stringWithFormat:@"error while executing script. Error %@",executionError]);
            }
            else
            {
                NSLog(@"Script execution has succeed. Result(%@)",result);  
                executionSucceed = YES;
            }
        }
    }

    [appleScript release];  

    return executionSucceed;
}

这篇关于使用参数从 Cocoa App 执行 Applescript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆