Parameter-Problem while calling AppleScript from Cocoa
Parameter-Problem while calling AppleScript from Cocoa
- Subject: Parameter-Problem while calling AppleScript from Cocoa
- From: Manfred Lippert <email@hidden>
- Date: Sun, 25 May 2003 18:59:27 +0200
Hi,
I can successfull call handlers of an AppleScript out of my Cocoa
application with [NSAppleScript executeAppleEvent:error:]. This is a
nice feature and normally works great, but now I have a problem:
I want to call one handler and pass the returned object of this handler
to another handler (two calls of [NSAppleScript
executeAppleEvent:error:]).
For simple returned objects like numbers this works great, without any
problems. But for complex objects I get runtime errors from AppleScript
like "Can't get <object> of <object> of ...", even if I do not use the
given object in the second handler. (Error number -1728)
I think the main problem lies in the fact, that the complex parameter
"belongs" to another application, but when passing it as parameter to
the handler, the script context (?) is "belonging" to my application
yet. I have to change to insert a "tell application" for the parameter
or such things. (But I really don't know what happens here, that's the
reason why I write this email ;-))
Example, so maybe you can understand it better:
1.) The AppleScript in my Application is something like that:
------------------------------
on function1()
tell application "OtherApplication"
return someObjectOfOtherApplication
end tell
end function1
on function2(objectOfOtherApplication)
...
end function2
------------------------------
For example "OtherApplication" maybe "iTunes" and
"someObjectOfOtherApplication" maybe "selection" or something like that.
2.) Parts of Cocoa code in my Application:
I have an Obj-C wrapper class for my AppleScript that contains the
object "script" of type "NSAppleScript" (I load it when initializing
this wrapper class) and I wrote the following method to call handlers
in my script:
------------------------------
- (NSAppleEventDescriptor *)
executeAppleScriptHandler:(const char *)routineName
parameters:(NSAppleEventDescriptor *)parameters {
ProcessSerialNumber PSN = {0, kCurrentProcess};
NSAppleEventDescriptor *target =
[NSAppleEventDescriptor
descriptorWithDescriptorType:typeProcessSerialNumber
bytes:&PSN
length:sizeof(PSN)];
NSAppleEventDescriptor *event =
[NSAppleEventDescriptor
appleEventWithEventClass:'ascr'
eventID:kASSubroutineEvent
targetDescriptor:target
returnID:kAutoGenerateReturnID
transactionID:kAnyTransactionID];
NSAppleEventDescriptor *routine =
[NSAppleEventDescriptor
descriptorWithDescriptorType:typeText
bytes:routineName
length:strlen(routineName)];
[event setParamDescriptor:routine forKeyword:keyASSubroutineName];
if (parameters) {
[event setParamDescriptor:parameters forKeyword:keyDirectObject];
}
NSDictionary *error = nil;
NSAppleEventDescriptor *result = [script executeAppleEvent:event
error:&error];
if (error) {
NSLog(@"ERROR: %@", error);
}
return result;
}
------------------------------
Calling "function1" with this works great. No error is returned, and I
can look into "results".
Now I am trying to call "function2" and pass it the results of
function1. There executeAppleEvent returns an error for complex
parameters.
To call "function2", I build an NSEventDescriptor of type "list
descriptor" and put the result of "function1" in it (as first and only
parameter), e.g. like this:
------------------------------
NSAppleEventDescriptor *params = [NSAppleEventDescriptor
listDescriptor];
[params insertDescriptor:result atIndex:0];
// result is from the first executeAppleScriptHandler call (to
"function1")
[scriptWrapper executeAppleScriptHandler:"function2" parameters:params];
------------------------------
For simple objects like numbers this works great, but for complex
objects I got runtime errors from AppleScript.
Has someone any ideas what I can try next? I run out of ideas ... :-(
Am I right that the main problem lies in the fact that "result" is not
belonging to my application and AppleScript tries to "interpret"
something here that is could not as soon as it knows to which
application the object belongs?
If yes, how can I fix that?
Thanks,
Mani
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.