Copy link to clipboard
Copied
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!
Many Thanks Laubender, good suggestion... after much mucking around I finally got it to work as follows:
#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;
var myLinkparentFolder = myLink.filePath.match(/(.*\\[^\\]*)(\\[^\\]*\\)/)[1];
var myLinkName = myLink.name.match(/(.*)(\.[^\.]+)/)[1];
var effectivePPI = String(myImage.effectivePpi);
effectivePPI.match(/(\d+),(\d+)/);
...
Copy link to clipboard
Copied
Hi spaciousmind,
without testing anything of your code:
You could cheat a bit.
No need to pass the file back to InDesign because you are doing the new file with a predictable recipe. Simply use the same procedure and place the file to InDesign. Of course first check if the file exists.
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Many Thanks Laubender, good suggestion... after much mucking around I finally got it to work as follows:
#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;
var myLinkparentFolder = myLink.filePath.match(/(.*\\[^\\]*)(\\[^\\]*\\)/)[1];
var myLinkName = myLink.name.match(/(.*)(\.[^\.]+)/)[1];
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) {
if (ppiH < 300) {
var scalePercentage = ((300 - ppiH) / ppiH) * 100 + 100;
var scalePercentageRounded = Math.round(scalePercentage);
$.writeln("scalePercentage = " + scalePercentage)
$.writeln("scalePercentageRounded = " + scalePercentageRounded)
var theFile = File(myLinkparentFolder + "\\" + myLinkName + '_upscaled_' + Math.round(scalePercentage) + '-pct.jpg');
CreateBridgeTalkMessage(myLinkfp, myLinkName, scalePercentage);
} else {
alert("PPI higher than 300 already")
}
} else {
alert("Horizontal and vertical resolutions are not the same.");
}
//---------------------FUNCTIONS-----------------
function CreateBridgeTalkMessage(imagePath, myLinkName, scalePct) {
var bt = new BridgeTalk();
bt.target = "photoshop";
bt.body = ResaveInPS.toSource()+"("+imagePath.toSource()+ "," + myLinkName.toSource()+ "," + scalePct.toSource()+");";
bt.onError = function(errObj) {
}
bt.onResult = function(resObj) {
}
bt.send(30);
myLink.relink(theFile);
myLink.update();
}
//-----------------------------------------------
function ResaveInPS(imagePath, myLinkName, 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 + "/" + myLinkName + '_upscaled_' + Math.round(scalePct) + '-pct.psd');
var saveFileJPG = File( outputFolder + "/" + myLinkName + '_upscaled_' + Math.round(scalePct) + '-pct.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);
app.open(saveFilePSD);
app.preferences.rulerUnits = startRulerUnits;
app.displayDialogs = DialogModes.ALL;
}
which works fine for this purpose.....
For future reference I would still like to know how to or why I couldn't get any variables back from photoshop if anyone can explain where I went wrong in my first example that'd be really helpful 🙂
Copy link to clipboard
Copied
Hi,
I haven't tested it with your script but it should be rather simple:
At the end of the photoshop script part below the line
app.displayDialogs = DialogModes.ALL;
add the command
return saveFileJPG
resObj.body should then contain the desired information.