
CHAPTER 5: Interapplication Communication with Scripts Communicating through messages 175
bt.send();
Example: Handling any error
In this example, the
onError handler re-throws the error message within the sending application.
var bt = new BridgeTalk;
bt.onError = function (btObj) {
var errorCode = parseInt (btObj.headers ["Error-Code"]);
throw new Error (errorCode, btObj.body);
}
Example: Handling expected errors and responses
This example creates a message that asks Adobe Bridge to return XMP metadata for a specific file. The
onResult method processes the data using a script-defined processFileSize function. Any errors are
handled by the
onError method. For example, if the file requested is not an existing file, the resulting error
is returned to the
onError method.
var bt = new BridgeTalk;
bt.target = "bridge-3.0";
bt.body = "var tn = new Thumbnail(’C/MyPhotos/temp.gif’);
tn.core.immediate.size;"
bt.onResult = function( resultMsg ) {
processFileSize(resultMsg.body);
}
bt.onError = function( errorMsg ) {
var errCode = parseInt (errorMsg.headers ["Error-Code"]);
throw new Error (errCode, errorMsg.body);
}
bt.send();
Example: Setting up a target to send multiple responses
This example integrates the sending of multiple responses with the evaluation of a message body. It sets
up a handler for a message such as the one sent in the following example.
The target application (Adobe Bridge) defines a static onReceive
method to allow for a new type of
message, which it calls an iterator. An
iterator type of message expects the message.body to use the
iteration variable
i within the script, so that different results are produced for each pass through the while
loop. Each result is sent back to the sending application with the sendResult()
method. When the
message.body has finished processing its task, it sets a flag to end the while loop.
// Code for processing the message and sending intermediate responses
// in the target application (Adobe Bridge)
BridgeTalk.onReceive = function (message){
switch (message.type) {
case "iterator":
done = false;
i = 0;
while (!done) {
// the message.body uses "i" to produce different results
// for each execution of the message.
// when done, the message.body sets "done" to true
// so this onReceive method breaks out of the loop.
message.sendResult(eval(message.body));
i++; }
break;
default: //"ExtendScript"
return eval( message.body );
Kommentare zu diesen Handbüchern