Copy link to clipboard
Copied
Hi All,
I am trying to find out how to compose a script for InDesign to do several things (similar to an Action in Photoshop).
1. Scale selected grouped objects to 81%.
2. Centre to page vertically & horizontally.
3. Change the document Document Setup from 297mm x 210mm to 148mm to 210mm.
Any thoughts please?
Happy to give it a go if I can find the right script editor - I do web design but not scripting as such so need a base to work from rather than writing from scratch.
Cheers.
Yes - possible - does this work for you?
// Step 1: Scale selected grouped objects to 81%
var doc = app.activeDocument;
var selection = doc.selection;
if(selection.length > 0) {
for(var i = 0; i < selection.length; i++) {
var item = selection[i];
if(item.constructor.name === "Group") {
var scaleFactor = 81 / 100; // Scale factor for 81%
var itemBounds = item.geometricBounds;
var centerX = (itemBounds[1] + itemBounds[3]) / 2;
va...
Copy link to clipboard
Copied
Yes - possible - does this work for you?
// Step 1: Scale selected grouped objects to 81%
var doc = app.activeDocument;
var selection = doc.selection;
if(selection.length > 0) {
for(var i = 0; i < selection.length; i++) {
var item = selection[i];
if(item.constructor.name === "Group") {
var scaleFactor = 81 / 100; // Scale factor for 81%
var itemBounds = item.geometricBounds;
var centerX = (itemBounds[1] + itemBounds[3]) / 2;
var centerY = (itemBounds[0] + itemBounds[2]) / 2;
var transformMatrix = app.transformationMatrices.add({horizontalScaleFactor: scaleFactor, verticalScaleFactor: scaleFactor});
item.transform(CoordinateSpaces.INNER_COORDINATES, AnchorPoint.CENTER_ANCHOR, transformMatrix);
}
}
}
// Step 2: Change the document Document Setup from 297mm x 210mm to 148mm to 210mm
doc.documentPreferences.pageWidth = "148mm";
doc.documentPreferences.pageHeight = "210mm";
// Step 3: Centre to page vertically & horizontally
var page = doc.pages[0]; // Assuming you want to center on the first page
var pageBounds = page.bounds;
var pageWidth = pageBounds[3] - pageBounds[1];
var pageHeight = pageBounds[2] - pageBounds[0];
for(var i = 0; i < selection.length; i++) {
var item = selection[i];
var itemBounds = item.geometricBounds;
var itemWidth = itemBounds[3] - itemBounds[1];
var itemHeight = itemBounds[2] - itemBounds[0];
item.move([
(pageWidth - itemWidth) / 2,
(pageHeight - itemHeight) / 2
]);
}
alert("Script completed successfully!");
Copy link to clipboard
Copied
Well Eugene,
I would have to say:
1. Yes it does, and
2. You're a legend!
The width & height dimensions were around the wrong way but I managed to fix that LOL.
Thanks very much 🙂
PS Where is the best place to learn this? And the best Mac editor to use?
I can't go on asking people in Adobe Community every time I want to write an InDesign script for repetitive stuff.
Copy link to clipboard
Copied
Oh right yes - ha - well you managed to fix it
Glad it worked for you
Copy link to clipboard
Copied
Hi @creativejim , Adobe's ExtendScript toolkit no longer works on current OSs, so for debugging there is VS Code
https://code.visualstudio.com/download
Youll also need the ESTK plugin
https://marketplace.visualstudio.com/items?itemName=Adobe.extendscript-debug
Copy link to clipboard
Copied
There is also this API reference:
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Application.html
If you check the document object there is an align() method, which could be used to simplify Eugene‘s code depending on what you need:
var doc = app.activeDocument;
doc.documentPreferences.pageWidth = "148mm";
doc.documentPreferences.pageHeight = "210mm";
var selection = doc.selection;
if(selection.length > 0) {
for(var i = 0; i < selection.length; i++) {
if(selection[i].constructor.name === "Group") {
selection[i].horizontalScale = 81
selection[i].verticalScale = 81
app.activeDocument.align (selection[i], AlignOptions.HORIZONTAL_CENTERS, AlignDistributeBounds.PAGE_BOUNDS);
app.activeDocument.align (selection[i], AlignOptions.VERTICAL_CENTERS, AlignDistributeBounds.PAGE_BOUNDS);
}
}
}Get ready! An upgraded Adobe Community experience is coming in January.
Learn more