Re: Calling AppleScript from Cocoa?
Re: Calling AppleScript from Cocoa?
- Subject: Re: Calling AppleScript from Cocoa?
- From: Brian Webster <email@hidden>
- Date: Tue, 21 Jan 2003 15:11:25 -0600
The way to execute a specific handler within a script is to use the
executeEvent() method, passing in a specially configured Apple event.
It should go something along these lines (this is all Objective-C but
should translate over to Java OK):
-----------------------------------
NSAppleScript* myScript; //assume initialized
NSAppleEventDescriptor* event;
NSAppleEventDescriptor* targetAddress;
NSAppleEventDescriptor* subroutineDescriptor;
NSAppleEventDescriptor* arguments;
NSAppleEventDescriptor* result;
NSDictionary* myErrorDict;
int pid = [[NSProcessInfo processInfo] processIdentifier];
targetAddress = [[NSAppleEventDescriptor alloc]
initWithDescriptorType:typeKernelProcessID bytes:&pid
length:sizeof(pid)];
event = [[NSAppleEventDescriptor alloc]
initWithEventClass:kASAppleScriptSuite eventID:kASSubroutineEvent
targetDescriptor:targetAddress returnID:kAutoGenerateReturnID
transactionID:kAnyTransactionID];
subroutineDescriptor = [NSAppleEventDescriptor
descriptorWithString:@"mysubroutine"];
[event setParamDescriptor:subroutineDescriptor
forKeyword:keyASSubroutineName];
arguments = [[NSAppleEventDescriptor alloc] initListDescriptor];
//add stuff to your arguments list
[event setParamDescriptor:arguments forKeyword:keyDirectObject];
result = [myScript executeEvent:event errorInfo:&myErrorDict];
-------------------------------------
This code should execute the handler defined as:
on mysubroutine (arg1, arg2)
-- stuff
end mysubroutine
and return any results to you in the result descriptor. Of course,
change the subroutine name and arguments as appropriate to match your
script. Be aware that Applescript handler names are not case
sensitive, so you should pass in a lower-case string in all cases.
Also, handlers that have event terminology defined for them (standard
ones like open, run, etc.) don't use strings for the handler names. I
forget exactly what gets used for those, but I could look it up if need
be. Also, I typed all the code above in Mail, so there may be errors,
but the general idea is there.
On Tuesday, January 21, 2003, at 02:09 PM,
email@hidden wrote:
Ok, here's my real question,
Using an NSAppleScript object, can I call a script's subroutine using
an NSSelector in Cocoa/Java? It doesn't look like it so far... If
not, is there ANY way to call a script's subroutine through Cocoa?? I
take it there must be, given that AppleScript Studio exists...
What I've tried so far...
I built a simple script that calls a subroutine in the run handler.
The subroutine simply displays a dialog, and finishes with a beep.
Using the handlers, nothing happens, no errors returned in the
NSMutableDictionary. I would have guessed that execute() would have at
least called the run handler, but apparently it doesn't. Take all the
handlers out and only include the code to the subroutine itself in the
NSAppleScript object and it works fine. But it's extremely not useful
since everything must be hardwired; meaning I need a different
NSAppleScript for every variable I would have otherwise passed into a
subroutine. Trying to use a NSSelector to call the subroutine and pass
it a simple string variable fails miserably.
Any suggestions/pointers/advice is greatly appreciated.
--
Brian Webster
email@hidden
http://homepage.mac.com/bwebster
_______________________________________________
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.