
CHAPTER 5: Interapplication Communication with Scripts Communicating through messages 173
To change the default behavior set the BridgeTalk.onReceive property to a function definition in the
following form:
BridgeTalk.onReceive = function( bridgeTalkObject ) {
// callback definition here
};
The body property of the received message object contains the received data.
The function can return any type.
The function that you define does not need to explicitly create and return a
BridgeTalk message object.
The messaging framework creates a new
BridgeTalk message object, and packages the return value of
the
onReceive handler as a string in the body property of that object.
Return values are flattened into a string using the Unicode Transformation Format-8 (UTF-8) encoding. If
the function does not specify a return value, the resulting string is the empty string.
The result object is transmitted back to the sender if the sender has implemented an
onResult callback for
the original message.
Message-handling examples
This example shows the default mechanism for handling unsolicited messages received from other
applications. This simple handler executes the message’s data as a script and returns the results of that
execution.
BridgeTalk.onReceive = function (message) {
return eval( message.body );
}
This example shows how you might extend the receive handler to process a new type of message.
BridgeTalk.onReceive = function (message) {
switch (message.type) {
case "Data":
return processData( message );
break;
default: //"ExtendScript"
return eval( mesage.body );
}
}
Handling responses from the message target
To handle responses to a message you have sent, you define callback handler functions in the message
object itself. The target application cannot send a response message back to the sender unless the
message object it received has the appropriate callback defined.
N
OTE: The message callbacks are optional, and are not implemented by all message-enabled applications.
When your message is received by its target, the target application’s static
BridgeTalk object’s onReceive
method processes that message, and can invoke one of the message object’s callbacks to return a
response. In each case, the messaging framework packages the response in a new message object, whose
target application is the sender. Your callback functions receive this response message object as an
argument.
Komentarze do niniejszej Instrukcji