Re: best practice with helper AppleScript - another solution
Re: best practice with helper AppleScript - another solution
- Subject: Re: best practice with helper AppleScript - another solution
- From: matt neuburg <email@hidden>
- Date: Mon, 30 Jun 2003 20:29:09 -0700
On Mon, 30 Jun 2003 20:43:33 -0400, Kenneth Ferry <email@hidden> said:
>
Like the 'call method' command from AppleScript, the class
>
translates simple arguments (collections, strings,
>
integers*, booleans*) to their AppleScript forms and
>
translates the return value to an appropriate Objective-C
>
class if it can find one. You can download it here:
>
http://homepage.mac.com/kenferry/software.html
I didn't actually try running the code but it looks like you are forming
the ascr/psbr apple event, which is the correct way to call a handler. And
you're executing it with executeAppleEvent:error:, which is what I
typically advise for this purpose when the question arises. However, as you
probably know, there are several ways to declare a handler, and
corresponding ways to call it, that you're not taking account of; so your
approach cannot be termed general. Also there are many types of result that
your code cannot return; it just throws up its hands.
Unfortunately I didn't see the earlier posts in this thread so this may not
be helpful, but here goes. If your goal is merely to run an AppleScript,
then just use NSAppleScript. If your goal is to call a handler in a script,
in such a way that you can pass it parameters, you can load it and call it
from an NSAppleScript. Suppose, for example, we have a script like this:
to divide from x by y
return x / y
end divide
Kenneth Ferry's example can't deal with that syntax. Here's a way to do it.
Imagine that the script is a compiled script in our project, called
divider.scpt. Then this code will call its "divide" handler, passing it
arguments 4 and 3, and obtain the result:
NSString* scpt =
[[NSBundle mainBundle] pathForResource: @"divider" ofType: @"scpt"];
NSString* s =
[@"set f to posix file \"" stringByAppendingString: scpt];
s = [s stringByAppendingString: @"\"\rset fs to f as string\r"];
s = [s stringByAppendingString: @"set s to load script file fs\r"];
s = [s stringByAppendingString: @"s's divide from 4 by 3"];
NSAppleScript* as = [[NSAppleScript alloc] initWithSource: s];
NSAppleEventDescriptor* res = [as executeAndReturnError: nil];
NSLog([[res coerceToDescriptorType: 'TEXT'] stringValue]);
That is not general at all, of course; the idea is simply to illustrate how
to go where Kenneth Ferry's example code doesn't. As I say, I don't know
what the original question was, but I hope this will be useful to someone
sometime. m.
--------
matt neuburg, phd = email@hidden,
http://www.tidbits.com/matt
pantes anthropoi tou eidenai oregontai phusei
Subscribe to TidBITS! It's free and smart.
http://www.tidbits.com/
_______________________________________________
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.