
CHAPTER 5: Interapplication Communication with Scripts Communicating through messages 171
var bt = new BridgeTalk;
// send this msg to the Adobe Bridge CS4 application
var targetApp = BridgeTalk.getSpecifier( "bridge-3.0");
bt.target = targetApp;
// the script to evaluate is contained in a string in the "body" property
bt.body = "new Document(’C:\\BridgeScripts’);
app.document.target.children.length;"
Step 3: Specify how to handle a response
If you want to handle a response for this message, or use the data that is returned from the script’s
evaluation, you must set up the response-handling mechanism before you send the message. You do this
by defining the onResult
callback in the message object.
N
OTE: The message callbacks are optional, and are not implemented by all message-enabled applications.
The response to a message is, by default, the result of evaluation of the script contained in that message’s
body property. The target application might define some different kind of response; see “Receiving
messages” on page 172.
When the target has finished processing this message, it looks for an
onResult callback in the message
object it received. If it is found, the target automatically invokes it, passing it the response. The response is
packaged into a string, which is in turn packaged into the
body property of a new message object. That
message object is the argument to your
onResult callback function.
This handler, for example, processes the returned result using a script-defined
processResult function.
bt.onResult = function(returnBtObj)
{ processResult(returnBtObj.body); }
If you want to handle errors that might arise during script processing, you can define an onError callback in
the message object. Similarly, you can define a timeout
value and onTimeout callback to handle the case
where the target cannot process the message within a given time. For more information, see “
Handling
responses from the message target” on page 173.
N
OTE: If you define callbacks to handle a response, you must store the message in a variable that still exists
when the response is received. Otherwise, JavaScript might garbage-collect the message object, and the
response would be lost.
Step 4: Send the message
To send the message, call the message object’s
send method. You do not need to specify where to send
the message to, since the target application is set in the message itself.
bt.send();
You can optionally specify a timeout value, which makes the call synchronous; when you do this, the
method waits for a response from the target application, or for the timeout value to expire, before
returning. When a timeout is not specified, as in this example, the call is asynchronous and the
send()
method returns immediately.
A second optional parameter allows you to specify launch parameters, in case the target application is not
currently running, and the messaging framework needs to launch it.
The complete script looks like this:
// script to be executed in Photoshop CS4
#target "photoshop-11.0"
// check that the target app is installed
Comentários a estes Manuais