首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将参数的列表或数组从Obj传递到Applescript

将参数的列表或数组从Obj传递到Applescript
EN

Stack Overflow用户
提问于 2015-12-29 20:54:13
回答 1查看 762关注 0票数 2

我正在编写沙箱Mac (10.10)应用程序,应该启动一些Applescript。这是一个脚本:

代码语言:javascript
复制
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

这是准备调用的代码:

代码语言:javascript
复制
- (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;}

这就是调用:

代码语言:javascript
复制
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数组,但是脚本只接收一个(第一个)值。对角线上的迭代只给我'd','a','t','e‘。

那我哪里出错了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-30 17:25:09

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

代码语言:javascript
复制
- (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“。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34518951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档