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: Tue, 27 May 2003 14:37:32 +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 no chance 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:transactionID];
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 (transactionID == kAnyTransactionID) {
transactionID = [event returnID];
}
if (error) {
NSLog(@"ERROR: %@", error);
}
return result;
}
------------------------------
Calling "function1" with this works great. No error is returned, and I
can look into "result".
Now I am trying to call "function2" and pass it the result 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 it could not as soon as it knows to which
application the object belongs?
If yes, how can I fix that?
Does anyone have an idea how I can debug the communication between the
applications?
If I understand it correct, my Cocoa application sends AppleEvents to
itself to communicate with its embedded AppleScript, and the
AppleScript is communicating via (other) AppleEvents to another app,
e.g. iTunes.
Could it be that the object could not be recreated because the
communication between the AppleScript and the other object changes its
transaction ID? For the communication between my Cocoa application and
my script I made sure to always use the same transaction ID. (I don't
know if this is neccessary)
Thanks,
Mani
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.