将Obj-C中的参数列表或数组传递给Applescript

时间:2015-12-29 20:54:14

标签: objective-c macos applescript parameter-passing appstore-sandbox

我正在编写应该发布一些Applescript的沙盒Mac OS X(10.10)应用程序。 这是脚本:

on terminal(params)
tell application "Terminal"
do script "who"
repeat with counter_variable_name from 1 to count of params
    set current_character to item counter_variable_name of params
    do script current_character in window 1
end repeat
end tell
end terminal

这是准备调用的代码:

- (NSAppleEventDescriptor *)commandEventDescriptor:(NSString*) command withParams:(NSArray*)inputParams{
NSUInteger c=1;
NSAppleEventDescriptor *parameters = [NSAppleEventDescriptor listDescriptor];
for (NSString* s in inputParams) {
    NSAppleEventDescriptor *parameter = [NSAppleEventDescriptor descriptorWithString:s];
    [parameters insertDescriptor:parameter atIndex:c]; 
    parameter=nil;
    c++;
}

// target
ProcessSerialNumber psn = {0, kCurrentProcess};
NSAppleEventDescriptor *target = [NSAppleEventDescriptor descriptorWithDescriptorType:typeProcessSerialNumber bytes:&psn length:sizeof(ProcessSerialNumber)];

// function
NSAppleEventDescriptor *function = [NSAppleEventDescriptor descriptorWithString:command];

// event
NSAppleEventDescriptor *event = [NSAppleEventDescriptor appleEventWithEventClass:kASAppleScriptSuite eventID:kASSubroutineEvent targetDescriptor:target returnID:kAutoGenerateReturnID transactionID:kAnyTransactionID];
[event setParamDescriptor:function forKeyword:keyASSubroutineName];
[event setParamDescriptor:parameters forKeyword:keyDirectObject];
parameters=nil;
function=nil;
return event;}

这是调用:

NSUserAppleScriptTask *automationScriptTask = [self automationScriptTask];
if (automationScriptTask) {
    NSAppleEventDescriptor *event = [self commandEventDescriptor:@"terminal" withParams:@[@"date",@"time"]];
    [automationScriptTask executeWithAppleEvent:event completionHandler:^(NSAppleEventDescriptor *resultEventDescriptor, NSError *error) {
        if (! resultEventDescriptor) {
            NSLog(@"%s AppleScript task error = %@", __PRETTY_FUNCTION__, error);
        }
        else {

        }
    }];
}

我的期望是在终端获得这样的一些:

  

macbook:~xxx $ who

     

macbook:~xxx $ date

     

macbook:~xxx $ time

相反,我有:

  

macbook:xxx $ who

     

macbook:xxx $ d

     

-bash:d:找不到命令

     

macbook:xxx $ a

     

-bash:a:找不到命令

     

macbook:xxx $ t

     

-bash:t:找不到命令

     

macbook:xxx $ e

     

-bash:e:找不到命令

简单地说:我想传递给两个值的Applescript数组(" date"" time")但脚本只接收一个(第一个) )价值。迭代 params 给了我'' a'''' e'仅

那我哪里出错了?

1 个答案:

答案 0 :(得分:2)

我想我找到了答案。不完全确定它是否是正确的和正典的答案,但似乎是正确的方向。无论如何,我对问题的理解是上述脚本只接受一个参数。我提供了两个参数。因此,该脚本礼貌地忽略了第二个参数。 我的解决方案是将两个参数打包到一个描述符中,然后将其作为单个参数提供。更具体 - 这是相关代码:

- (NSAppleEventDescriptor *)commandEventDescriptor:(NSString*) command withParams:(NSArray*)inputParams{
NSUInteger c=1;
NSAppleEventDescriptor *parameters = [NSAppleEventDescriptor listDescriptor];
for (NSString* s in inputParams) {
    NSAppleEventDescriptor *parameter = [NSAppleEventDescriptor descriptorWithString:s];
    //indexing starts from 1, NOT 0
    [parameters insertDescriptor:parameter atIndex:c];
    parameter=nil;
    c++;
}
// parameter

// target
ProcessSerialNumber psn = {0, kCurrentProcess};
NSAppleEventDescriptor *target = [NSAppleEventDescriptor descriptorWithDescriptorType:typeProcessSerialNumber bytes:&psn length:sizeof(ProcessSerialNumber)];

// function
NSAppleEventDescriptor *function = [NSAppleEventDescriptor descriptorWithString:command];

// event
NSAppleEventDescriptor *event = [NSAppleEventDescriptor appleEventWithEventClass:kASAppleScriptSuite eventID:kASSubroutineEvent targetDescriptor:target returnID:kAutoGenerateReturnID transactionID:kAnyTransactionID];
[event setParamDescriptor:function forKeyword:keyASSubroutineName];

//packing the pair of arguments into one Event Descriptor
NSAppleEventDescriptor* l0=[NSAppleEventDescriptor listDescriptor];
[l0 insertDescriptor:parameters atIndex:1];

[event setParamDescriptor:l0 forKeyword:keyDirectObject];
parameters=nil;
function=nil;
return event;}

现在脚本按预期运行 - 打开终端窗口,运行命令“who”,然后命令“date”,然后命令“time”。