Re: Should Cocoa apps pass parameters to AppleScript?
Re: Should Cocoa apps pass parameters to AppleScript?
- Subject: Re: Should Cocoa apps pass parameters to AppleScript?
- From: Christopher Nebel <email@hidden>
- Date: Mon, 14 Jan 2008 09:37:24 -0800
On Jan 13, 2008, at 5:55 PM, Devraj Mukherjee wrote:
I have a design question. My humble Cocoa application runs bits and
pieces of AppleScript to interact with various standard
applications. It would be nice to send the AppleScript some
parameters that it can use out of my application to feed into the
other apps it interacts with.
Now should I consider passing parameters to the AppleScript or
should I run the AppleScript from with the Cocoa app, the
AppleScript in turn queries my application (so assuming that my
Cocoa app responds to the AppleScript calls) and fetch vars out like
that?
There are a few different ways to approach the problem:
1. Pass parameters to the script. You'll need to use OSAExecuteEvent
to do this, but what the function interface in the script looks like
is up to you. You can use the "run" handler, define your own
terminology-based handler a la Folder Actions, or use a user-defined
handler -- see QA1111 for an example.
2. Use a script template. That is, the "script" in your application
is a string constant with some replacement bits ("%@") in it; to
execute the script, use +stringWithFormat: to fill everything in, then
compile and execute.
3. Make your application scriptable. (Ideally, you ought to be doing
this anyway.) Because the script runs in the context of your
application, it can easily ask for the necessary bits of information.
Adding scriptable bits *purely* for the benefit of a script handler is
probably not a good idea, though.
4. Don't use AppleScript. Really, NSAppleScript should be reserved
for running user-supplied scripts -- in other words, you don't know at
build time what the scripts are going to be. If the logic is static,
then you ought to be sending Apple events directly from Cocoa. In
theory, you can use NSAppleEventDescriptor and AESend to do this, but
it's pretty painful, so we suggest using a higher-level framework.
appscript is a third-party framework which works from Panther on;
Scripting Bridge is Apple-supplied and works from Leopard on.
--Chris Nebel
AppleScript Engineering
P.S.: This sort of question is probably better directed to email@hidden
, but since a lot of the Implementors folks also read Users, don't
sweat it too much.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden