将参数列表或数组从 Obj-C 传递到 Applescript
Passing list or array of arguments from Obj-C to Applescript
我正在编写沙盒 Mac OS X (10.10) 应用程序,它应该启动一些 Applescript。
这是脚本:
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: command not found
macbook :xxx$ a
-bash: a: command not found
macbook :xxx$ t
-bash: t: command not found
macbook :xxx$ e
-bash: e: command not found
简单地说:我想将两个值(“日期”和“时间”)的数组传递给 Applescript,但脚本只接收一个(第一个)值。 params 的迭代只给我 'd'、'a'、't'、'e'。
那我哪里错了?
我想我找到了答案。不完全确定它是否是正确和规范的答案,但它似乎是在正确的方向。无论如何,我对问题的理解是上述脚本只接受 one 参数。我提供了 两个 参数。因此脚本礼貌地忽略了第二个参数。
我的解决方案是将两个参数打包到一个描述符中,然后将其作为单个参数提供。更具体 - 这是相关代码:
- (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;}
现在脚本按预期运行 - 打开终端 window,运行命令 "who",然后运行命令 "date",然后运行命令 "time"。
我正在编写沙盒 Mac OS X (10.10) 应用程序,它应该启动一些 Applescript。 这是脚本:
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: command not found
macbook :xxx$ a
-bash: a: command not found
macbook :xxx$ t
-bash: t: command not found
macbook :xxx$ e
-bash: e: command not found
简单地说:我想将两个值(“日期”和“时间”)的数组传递给 Applescript,但脚本只接收一个(第一个)值。 params 的迭代只给我 'd'、'a'、't'、'e'。
那我哪里错了?
我想我找到了答案。不完全确定它是否是正确和规范的答案,但它似乎是在正确的方向。无论如何,我对问题的理解是上述脚本只接受 one 参数。我提供了 两个 参数。因此脚本礼貌地忽略了第二个参数。 我的解决方案是将两个参数打包到一个描述符中,然后将其作为单个参数提供。更具体 - 这是相关代码:
- (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;}
现在脚本按预期运行 - 打开终端 window,运行命令 "who",然后运行命令 "date",然后运行命令 "time"。