I am writing sandboxed Mac OS X (10.10) app that should launch some Applescript. This is the script:
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
this is the code that prepares the invocation:
- (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;}
And this is the invocation:
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 {
}
}];
}
My expectation is to obtain some like this in Terminal:
macbook:~ xxx$ who
macbook:~ xxx$ date
macbook:~ xxx$ time
Instead I have:
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
Simply put: I want to pass to Applescript array of two values ("date" and "time") but the script receives only one (first) value. Iteration on params gives me 'd','a','t','e' only.
So where did I go wrong?
123is not a valid command, so the ride is over there. if that's not what you expect then start by trying something valid.NSAppleScriptTaskaccessing application related scripts in~/Library/Application ScriptsorAppleScriptObjCwith an ObjC bridge class.