I can't get bridgetalk to pass result back
Hi, I'm trying to make a script to take the currently selected link in indesign, open it up in photoshop, scale it up & save it out in two different formats in the parent folder, then relink the file back in indesign.
my script can do all that except for relinking the file, because I can't figure out how to pass a result back to indesign (to use for the filename). I read the relevant bridgetalk documentation but must not be understanding it fully.
here is my script so far:
#target indesign
var doc = app.activeDocument;
var myImage = app.selection[0].images[0];
var myLink = app.selection[0].graphics[0].itemLink;
var myLinkfp = myLink.filePath;
docName = app.selection[0].graphics[0].itemLink.name.match(/(.*)(\.[^\.]+)/)[1];
$.writeln(docName.toSource());
var effectivePPI = String(myImage.effectivePpi)
effectivePPI.match(/(\d+),(\d+)/);
var ppiH = effectivePPI.replace(/(\d+),(\d+)/, '$1');
var ppiV = effectivePPI.replace(/(\d+),(\d+)/, '$2');
if (ppiH == ppiV) {
// alert("effective ppi " + effectivePPI + "\n" + "ppiH" + ppiH + "\n" + "ppiV" + ppiV)
if (ppiH < 300) {
var scalePercentage = ((300 - ppiH) / ppiH) * 100 + 100;
$.writeln(scalePercentage.toSource());
CreateBridgeTalkMessage(myLinkfp, docName, scalePercentage);
}
} else {
alert("Horizontal and vertical resolutions are not the same.");
}
$.writeln(effectivePPI.toSource());
//---------------------FUNCTIONS-----------------
function CreateBridgeTalkMessage(imagePath, docName, scalePct) {
var bt = new BridgeTalk();
bt.target = "photoshop";
bt.body = ResaveInPS.toSource()+"("+imagePath.toSource()+ "," + docName.toSource()+ "," + scalePct.toSource()+");";
bt.onError = function(errObj) {
$.writeln("Error: " + errObj.body);
}
bt.onResult = function(resObj) {
$.writeln("Result: " + resObj.body);
}
bt.send(30);
}
//-----------------------------------------------
function ResaveInPS(imagePath, docName, scalePct) {
var psDoc;
app.displayDialogs = DialogModes.NO;
var startRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PERCENT;
psDoc = app.open(new File(imagePath));
var currentPath = psDoc.path;
psdSaveOptions = new PhotoshopSaveOptions();
psdSaveOptions.layers = true;
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = 12;
var outputFolder = decodeURI(currentPath.parent);
var saveFilePSD = File( outputFolder + "/" + docName + "_upscaled.psd");
var saveFileJPG = File( outputFolder + "/" + docName + "_upscaled.jpg");
psDoc.resizeImage(Number(scalePct), null, 300, ResampleMethod.BICUBICAUTOMATIC);
psDoc.saveAs(saveFileJPG, jpgSaveOptions, true, Extension.LOWERCASE);
psDoc.saveAs(saveFilePSD, psdSaveOptions, true, Extension.LOWERCASE);
psDoc.close(SaveOptions.DONOTSAVECHANGES);
saveFileJPG.toSource();
app.open(saveFilePSD);
app.preferences.rulerUnits = startRulerUnits;
app.displayDialogs = DialogModes.ALL;
} sorry it's the full thing and probably messy but the relevant bits here are line 75 where I'm trying to pass the new file link back to source with saveFileJPG.toSource(); and line 45 where I'm trying to receive the result and write it in the console. it just comes back as undefined, no matter what I try.
not sure if I need to use eval or what but I couldn't get that to work either.
if I can get that working then all I need to do in indesign is this
myLink.relink(saveFileJPG);
myLink.update();
to link my selection up to the new file.
thanks for your help!