Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Export to txt a list of grouped objects and their coordinates in Adobe Indesign (tested on CS6)
//Download sample file: https://www.dropbox.com/s/2ng7oyjnfjk9pm2/indesign-coordinates-test-file.indd.zip?dl=0
app.activeDocument.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;
var myDocument = app.activeDocument;
var activeLayer = myDocument.activeLayer;
if(!activeLayer.visible) {
alert("The selected layer: '"+ activeLayer.name +"' is not visible." + '\n' + "Make it visible and try again."); exit();
} else {
// duplicateLayer(activeLayer);
}
list = [];
// Add first line. Useful when importing into Excel
list.push ('Project Name \t X Coord \t Y Coord');
// get all groups on the active layer. We will call them "post its"
postIts = activeLayer.groups.everyItem().getElements();
for (var i = 0; i < postIts.length; i++) {
//if the post-it is grouped twice whe need ungroup it first
if (postIts[i].textFrames.length == 0) {
DoublePostIt = postIts[i];
DoublePostIt.ungroup();
} else {
getProperties(postIts[i]);
}
}
function getProperties(item) {
/*
Geometric bounds and visible bounds are arrays containing four coordinates,
which define (in order) the top, left, bottom, and right edges of the object’s bounding box, (y1, x1, y2, x2).
*/
var top = roundMe(item.geometricBounds[0]);
var left = roundMe(item.geometricBounds[1]);
var bottom = roundMe(item.geometricBounds[2]);
var right = roundMe(item.geometricBounds[3]);
var x = roundMe([right - left]/2 + left);
var y = roundMe([bottom - top]/2 + top);
list.push ( item.textFrames[0].contents + '\t' + x +'\t'+ y);
}
if(list.length < postIts.length) { //if there is post-its remaining to be processed
alert("You need to run the script again." + '\n' + "There are post-its grouped twice on the layer: '"+ activeLayer.name);
exit();
} else {
exportFile();
// alert(list);
}
function exportFile() {
defaultFile = new File (Folder.myDocuments+"/"+app.activeDocument.name.replace(/\.indd$/i, '')+".txt");
if (File.fs == "Windows")
writeFile = defaultFile.saveDlg( 'Save report', "Plain text file:*.txt;All files:*.*" );
else
writeFile = defaultFile.saveDlg( 'Save report');
if (writeFile != null) {
if (writeFile.open("w"))
if (File.fs == "Windows") writeFile.encoding = "utf8";
else writeFile.encoding = "Western"; // so Excel get the accents right
//write list contents
writeFile.write (list.join("\r")+"\r");
writeFile.close();
}
}
/*------------------------------
UTILITIES
------------------------------*/
function roundMe(val,to) {
//round with 'to' decimal places
// default zero
var t = 1;
while (to-- > 0) t *= 10;
return Math.round(val*t)/t;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.