Copy link to clipboard
Copied
Hi
Is it possible to get the value shown in the width box in the tranform panel, through a script ?
I have tried substracting geometricbounds values, but for every embedded link(png/psd), the values are not matching.
I am not aware of any alternative method to read the width of an element except using the geometricBounds coordinates.
Select an element and try this:
var gb = app.selection[0].geometricBounds;
var width = gb[3] - gb[1];
alert(width);
Copy link to clipboard
Copied
I am not aware of any alternative method to read the width of an element except using the geometricBounds coordinates.
Select an element and try this:
var gb = app.selection[0].geometricBounds;
var width = gb[3] - gb[1];
alert(width);
Copy link to clipboard
Copied
the values are not matching
Hi @Chandrakanthi26574696ugut , do you have the linked image or its parent container selected? They wouldn't necessarily have the same width.
Copy link to clipboard
Copied
Hi the script is looping through all the graphics and calculating width on geometricbounds.
script is not selecting these links.
Copy link to clipboard
Copied
script is not selecting these links.
But what are you selecting to check the Transform panel Width?
For example this gets both the linked image width and its container frame width, which is what‘s showing in the Transform panel when I select the frame :
var lnks = app.documents[0].links;
//an image, the link’s parent
var img = lnks[0].parent
//the image bounds and width
var ib = img.geometricBounds
var iw = ib[3]-ib[1]
//the image’s parent container frame bounds and width
var fb = img.parent.geometricBounds
var fw = fb[3]-fb[1]
alert("\rImage Width: " + iw + "\r" + "Container Width: " + fw)
The image direct selected:
Copy link to clipboard
Copied
To verify width i am clicking on the links from the links panel.
I have tried with app.documents[0].links
and app.documents[0].allgraphics
Only for some links the values donot match. some are matchingthough.
purpose of this script is to resize the images in photoshop using the values from indesign.
Copy link to clipboard
Copied
Can you post some screenshots of what exactly are you talking about / looking for?
Copy link to clipboard
Copied
Just to clarify a link doesn’t have a geometricBounds property—this throws an error when i try to get the geometricBounds of the first link:
var lnks = app.documents[0].links;
$.writeln(lnks[0].geometricBounds)
Copy link to clipboard
Copied
Yes, Link doesn't have a geometric properties - as it aways needs a container - and only this container have geometric properties.
Copy link to clipboard
Copied
Hi All
Thanks for responding to my queries so quickly. I am pasting the script below and the indesign file against which i am running this script contains many links which i donot wish to attach here for privacy reasons .
Copy link to clipboard
Copied
Hi @Chandrakanthi26574696ugut , I’m not seeing a problem with the width part of your code, it’s returning the images’ widths. You are declaring a variable for the image’s frame (cont), but not using it.
Also you might need to check if the graphic is an image otherwise you would get an error when you try to get .effectiveResolution.
Here’s an example where I’m getting both the image and its frame widths:
var images = app.activeDocument.allGraphics;
var file = new File(Folder.desktop + "/inddsizes2.txt");
if (file.exists) {
file.remove();
}
//declare the loop variables
var ef, pp, cont, w, fw, data, newdata;
for (var i=0; i<images.length; i++) {//images.length
//make sure the graphic is an image
if (images[i].constructor.name == "Image") {
pp = images[i]//the image
cont = pp.parent;//the image frame
eff = pp.effectivePpi; //returns the vertical and horizontal as an array
//if either the vertical or horizontal res exceeds 320
if(eff[0] || eff[1] > 320){
//the scaled image width
w = Math.round(pp.geometricBounds[3]-pp.geometricBounds[1]);
//the image frame width
fw = Math.round(cont.geometricBounds[3]-cont.geometricBounds[1])
if (file.exists) {
file.open("a");
newData = images[i].itemLink.filePath+" - Image Width: "+w+ " Frame Width: " + fw + "\n"
file.write(newData);
file.close();
} else {
data = images[i].itemLink.filePath+" - Image Width: "+w+ " Frame Width: " + fw + "\n"
file.open("w");
file.write(data);
file.close();
}
}
}
if(i == (images.length-1)){
alert('files marked for PS')
}
}
The result
Copy link to clipboard
Copied
Also, it might be better to capture the image info in a string and write the text file once at the end rather than including the file writes in the loop. Here I’m using a function to write the info once:
var file = new File(Folder.desktop + "/inddsizes2.txt")
var images = app.activeDocument.allGraphics;
//declare the loop variables
var ef, pp, cont, w, fw;
var data = "Image Info: \n"; //a string to capture the image info
for (var i=0; i<images.length; i++) {
//make sure the graphic is an image
if (images[i].constructor.name == "Image") {
pp = images[i]//the image
cont = pp.parent;//the image frame
eff = pp.effectivePpi; //returns the vertical and horizontal as an array
//if either the vertical or horizontal res exceeds 320
if(eff[0] || eff[1] > 320){
//the scaled image width
w = Math.round(pp.geometricBounds[3]-pp.geometricBounds[1]);
//the image frame width
fw = Math.round(cont.geometricBounds[3]-cont.geometricBounds[1])
//add to Image Info string
data += images[i].itemLink.filePath+" - Image Width: "+w+ " Frame Width: " + fw + "\n"
}
}
if(i == (images.length-1)){
//write the text file once
writeText(file, data)
alert('files marked for PS')
}
}
/**
* Write a text file
* @ param the file path
* @ param the text
*
*/
function writeText(p,s){
var file = new File(p);
file.encoding = 'UTF-8';
file.open('w');
file.write(s);
file.close();
}
Copy link to clipboard
Copied
Hi
Thank you for your valuable response and suggestions for improvising.
As i mentioned in my earlier Post, only for some images the values are not accurate.
After consent from my team i am sending link to the file , where i am facing this issue.
Kindly download and let me know
Copy link to clipboard
Copied
I meant to ask if you were rotating your page items—geometricBounds gets the bounds of the image on the page including any rotation or skewing, so to get the actual scaled image width via geometricBounds you would have to set any rotation or skew to 0, get the geometric bounds, then reset.
Here the width of the image is 165.594mm, but the space it takes up on the page (geometricBounds) is 178.8mm.
I assume you plan to manually downsample the images over 320ppi in Photoshop? Why not let the downsampling happen on a PDF Export?
Copy link to clipboard
Copied
Yeah, @rob day is right, after rotating - you'll get a different GeometricBound values.
Copy link to clipboard
Copied
A doughnut from your document - left is the original one - right is after resetting rotation:
The value that is on the screenshot - InDesign values (top-left green rect) - are for the left object - image selected in the InDesign.
Highlighted in green are the values for the right object - after resetting angle to "0".
Copy link to clipboard
Copied
I think, the easiest way to get what you want @Chandrakanthi26574696ugut through scripting, would be to check if image is rotated, unrotate it - by setting angle to 0 - rotation point doesn't matter, read GeometricBounds, calculate width, then just undo the rotation.
Maybe it could be done using Transformation Matrices - but I won't even try this method.
But don't use Clear Transformation option - it will reset EVERYTHING - including scale, etc.
Copy link to clipboard
Copied
I think, the easiest way to get what you want @Chandrakanthi26574696ugut through scripting, would be to check if image is rotated, unrotate it - by setting angle to 0 - rotation point doesn't matter, read GeometricBounds, calculate width, then just undo the rotation.
Hi Robert, you would have to also clear any other parent rotations (and skews)—the parent frame, groups, groups inside of groups, etc. It gets messy if you want something that works for all cases.
Copy link to clipboard
Copied
@rob day, no, you don't - image will be rotated to 0 - no matter what is the rotation angle of the all parent objects combined.
And when object is skewed / sheared - it is reflected in what InDesign returns for the rotated image - so if you clear skew / shear AND rotation - you'll get a different size - if you just unrotate.
Copy link to clipboard
Copied
Groups of objects:
And here are the Width & Height values calculated from the GeometricBounds:
I'm not doing rounding - so values are "almost 0".
And full document's structure:
Copy link to clipboard
Copied
If I add lines to set the image rotation and shear to 0 to @Chandrakanthi26574696ugut ‘s code I still get different width calculations for one of the example files.
Here the image on the right has no rotations and the image on the left was copied from the example file and has rotations applied to both the image and its frame—they are returning different widths even though there rotations have both been set to 0—the actual width is 166:
The code with image rotation and shear set to 0
//Undo Command
app.doScript(getImageWidths, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Get Image Widths');
function getImageWidths(){
var file = new File(Folder.desktop + "/inddsizes2.txt")
var images = app.activeDocument.allGraphics;
//declare the loop variables
var ef, pp, cont, w, fw;
var data = "Image Info: \n"; //a string to capture the image info
for (var i=0; i<images.length; i++) {
//make sure the graphic is an image
if (images[i].constructor.name == "Image") {
pp = images[i]//the image
cont = pp.parent;//the image frame
eff = pp.effectivePpi; //returns the vertical and horizontal as an array
//if either the vertical or horizontal res exceeds 320
if(eff[0] || eff[1] > 320){
pp.absoluteRotationAngle = 0;
pp.absoluteShearAngle = 0
//the scaled image width
w = Math.round(pp.geometricBounds[3]-pp.geometricBounds[1]);
//the image frame width
fw = Math.round(cont.geometricBounds[3]-cont.geometricBounds[1])
//add to Image Info string
data += images[i].itemLink.name +" - Image Width: "+w+ " Image rotation: " + pp.absoluteRotationAngle + "\n"
}
}
}
writeText(file, data)
alert('files marked for PS')
}
/**
* Write a text file
* @ param the file path
* @ param the text
*
*/
function writeText(p,s){
var file = new File(p);
file.encoding = 'UTF-8';
file.open('w');
file.write(s);
file.close();
}
Copy link to clipboard
Copied
Hi Rob, Robert
Thanks for looking in to the files and providing a work around.
Though i am not sure of how the undo command works. Because it is placed on top of the other methods. I assumed it needs to be placed after the calculation ofthe width.
However i am going to try this script.
Regarding downsampling, yes I have prepared a script for resizing in PS. I was not aware of downsampling through PDF export until now. Will check that option as well.
Will post further, in case of any hurdles.
Copy link to clipboard
Copied
I was not aware of downsampling through PDF export until now
Besides the extra work there are other problems with resampling in Photoshop. The resample is destructive, so if you sampled everything to an effective resolution of 300, and sometime in the future decided to increase the scaling in the pagelayout, the output res is going to drop below the desired 300ppi
Photoshop does have different resampling methods, but I don’t think you would see any benefit over a PDF downsample in print. You would want to run some output tests before going to the extra work of a PS down sample.
A more reliable scripting solution would be to have the script open images that are over 320 PPI in Photoshop, which can be done with a BridgeTalk object. Here‘s an example:
checkRes()
var np;
function checkRes(){
var bt = new BridgeTalk();
bt.target = "photoshop";
if (app.documents.length > 0) {
var lnks = app.documents[0].links;
var img, eff, pa;
for (var i = 0; i < lnks.length; i++){
img = lnks[i].parent;
if (img.constructor.name == "Image" && lnks[i].status == LinkStatus.NORMAL) {
eff = img.effectivePpi; //returns the vertical and horizontal as an array
if(eff[0] || eff[1] > 320){
pa = lnks[i].filePath;
setImageRes(pa, eff, bt)
}
}
/* try {
lnks[i].relink(File(pa))
}catch(e) {} */
};
} else {
alert("No Documents Open")
return
}
}
/**
* Image sampling
* @ param file path
* @ param InDesign Effective res
* @ param Bridgetalk instance
* return new file path
*/
function setImageRes(pa, ef, bt){
bt.body = psScript.toString() + "\rpsScript('"+pa+"'"+",["+ef+"]);";
//$.writeln(bt.body)
bt.onError = function( inBT ) { alert(inBT.body); };
bt.onResult = function(resObj) {
np = resObj.body
}
bt.send(1000);
function psScript(pa, ef) {
//Open the link
var of = open (File(pa));
//get the scaled resolution from the InDesign Effective resolution (ef)
var rs = (of.resolution/ef[0])*of.resolution
of.resizeImage(null, null,rs,ResampleMethod.BICUBICSHARPER);
//additional commands and save...
//return
}
};
Copy link to clipboard
Copied
What you get from the script - when reading GeometricBounds - will always be different to what InDesign is showing - because InDesign is showing values for when rotation angle is set to "0".
I have no idea if it's calculated on the spot - or stored somewhere - but that's what I've observed.
Of course I can be wrong.
Copy link to clipboard
Copied
Hi
There is no change in the width of the images. For few images the width is still not accurate / not same as showin in indesign transform panel.