Skip to main content
Participant
March 14, 2024
Answered

Scripting For InDesign To Do Several Actions

  • March 14, 2024
  • 1 reply
  • 598 views

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.

This topic has been closed for replies.
Correct answer Eugene Tyson

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!");

1 reply

Eugene TysonCommunity ExpertCorrect answer
Community Expert
March 14, 2024

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!");
Participant
March 14, 2024

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.

Community Expert
March 14, 2024

Oh right yes - ha - well you managed to fix it 

Glad it worked for you