-
1. Re: How to get result from BridgeTalk message?
Bob Stucky Sep 25, 2008 9:24 AM (in response to Kasyan Servetsky)Your questions tell me that you don't really understand the asynchronous behavior or BridgeTalk.
When you call bt.send() to send your BridgeTalk message, your script doesn't stop. I continues to execute until completion. Your onResult handler function is called when a result is received from the target application.
Without a persistent session, InDesign (and PS) start a scripting engine for your script. When your script completes, the scripting engine is killed off.
If you send a BridgeTalk message in such a script, when the result comes back to InDesign, the reference to your script and its scripting engine no longer exists. That's why you MUST use a persistent session in InDesign if you are sending a BridgeTalk message and expecting a response.
In the code you provided, think of the onResult handler as a separate function to be executed at some future time.
Your function infoFromPS() does not return anything, which is why the first writeln returns "undefined".
Your onResult function will execute when a result is received; but, that will be at some future time, long after your infoFromPS() function has already returned.
The execution works like so:
var myResult2 = infoFromPS();
- this causes the infoFromPS() function to execute...
infofromPS() creates a BT message, sets the properties including the onResult handler, and calls "bt.send()". It then completes, and returns nothing (undefined).
The next line to execute (because infoFromPS() has compelted) is:
$.writeln("myResult2 = " + myResult2);
And since infoFromPS() returned noting, you get
myResult = undefined
in the ESTK.
Then, at some time (depends on how long the script takes to execute in Photoshop - could be hours), a response message is received from Photoshop. At that time, your onResult handler executes:
myResult1 is set to the response message body, the $.writeln writes the correct stuff into the ESTK console, and the onResult function returns myResult1 (which does nothing). You can't "return" something from an onResult handler.
The correct way to use BridgeTalk is to write your script so that it sends the message, and then to write the onResult handler so it does what you need with the returning message.
#target indesign;
#targetengine "session"
function infoFromPS() {
var bt = new BridgeTalk();
bt.target = "photoshop";
bt.body = "app.colorSettings;"
bt.onResult = function( msg ) {
var myResult = msg.body;
$.writeln( "BridgeTalk result = " + myResult );
doSomethingNow( myResult );
}
bt.send();
}
doSomethingNow = function( result ) {
$.writeln( "Now I am doing something with " + result );
}
infoFromPS();
This *should* (I haven't tested it) write into the ESTK console:
"BridgeTalk result..."
"Now I am doing something with..."
Regards
Bob -
2. Re: How to get result from BridgeTalk message?
Kasyan Servetsky Sep 25, 2008 10:43 AM (in response to Kasyan Servetsky)Hi Bob,
Thank you so much for the quick reply and intelligible explanation. You opened my eyes to this problem. I reached a deadlock and you showed me the way out of it. I wouldn't be able to solve this thing without your help. Now it works at last!
Thank you again, Bob.
Kasyan
#target indesign-5.0
#targetengine "session"
function infoFromPS() {
var bt = new BridgeTalk;
bt.target = "photoshop";
bt.body = "app.colorSettings;"
bt.onResult = function(resObj) {
var myResult = resObj.body;
$.writeln( "BridgeTalk result = " + myResult );
doSomethingNow( myResult );
}
bt.send();
}
function doSomethingNow( result ) {
if (result != "ISO Web Coated") {
alert( "Color Settings in Photoshop are set to \"" + result + "\"");
}
}
infoFromPS(); -
3. Re: How to get result from BridgeTalk message?
dhishok Nov 3, 2011 7:43 PM (in response to Kasyan Servetsky)Hi Bob & Kasyan,
Currently i am working with InDesign CS3. I need to check whether the Object layered images preseneted in my InDesign document, if it is found i need to open the particular image (.ai or .psd) in the (Illustrator or Photoshop) applications and do some layer manipulation in it and do saveAs with different name. Finally i need to relink the image in InDesign.
I have written the script for InDesign, Illustrator & Photoshop seperately for the above stuff. The only thing i need to do is combine these scripts and make them intermediate. Finally i have to run this script from InDesign Scripts palette.
In a script, targetting the different applications is not work out i think so. Hence i tried with Bridge Talk() but facing difficulties on execution from ESTK.
Below is my code:
---------------------------------------------------
#target InDesign-5.0
var doc = app.activeDocument;
var docLinks = doc.links;
var OLImage = [];
var OLayer = [];
var myCheck = 0;
for (e=0; e<docLinks.length; e++){
var myLink = docLinks[e];
var myGLayer = myLink.parent.graphicLayerOptions.graphicLayers;
if (myGLayer.length > 1){
for (g=0; g<myGLayer.length; g++){
if (myGLayer[g].currentVisibility != myGLayer[g].originalVisibility){
OLayer.push(myGLayer[g].name);
OLayer.push(myGLayer[g].currentVisibility);
OLImage.push(OLayer);
myCheck = 1;
}
}
if (myCheck == 1){
var myUpdatedImage = setLayerVisibility(myLink.filePath, OLImage);
alert (myUpdatedImage);
}
}
}
function setLayerVisibility(myFile, ObjLayers){
if (myFile.toString().match(/\.ai$/gi) != null){
//~ #target Illustrator-13.0
var doc = app.open(File(myFile));
var docPath = doc.path;
for (o=0; o<ObjLayers.length; o++){
if (ObjLayers[o][1])
doc.layers.getByName(ObjLayers[o][0]).visible = true;
else
doc.layers.getByName(ObjLayers[o][0]).remove();
}
var saveOptions = new IllustratorSaveOptions();
var aiCS4Doc = new File(docPath + "/" + doc.name.replace(".ai", "-b.ai"));
saveOptions.compatibility = Compatibility.ILLUSTRATOR13;
saveOptions.flattenOutput = OutputFlattening.PRESERVEAPPEARANCE;
doc.saveAs( aiCS4Doc, saveOptions );
return aiCS4Doc;
}
else if (myFile.toString().match(/\.psd$/gi) != null){
//~ #target Photoshop
var doc = app.open(File(myFile));
var docLayers = doc.layers;
var docPath = doc.fullName.toString().replace(/\.psd$/gi, "-b.psd");
for (p=0; p<ObjLayers.length; p++){
if (ObjLayers[p][1])
doc.layers.getByName(ObjLayers[p][0]).visible = true;
else
doc.layers.getByName(ObjLayers[p][0]).remove();
}
doc.saveAs (File(docPath));
return docPath;
}
}
Can you please guide me in this regard.
Thanks in advance
Thiyagu


