Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
13

Scripting For InDesign To Do Several Actions

Community Beginner ,
Mar 13, 2024 Mar 13, 2024

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.

TOPICS
Scripting , SDK
699
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 13, 2024 Mar 13, 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;
            va
...
Translate
Community Expert ,
Mar 13, 2024 Mar 13, 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!");
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 14, 2024 Mar 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2024 Mar 14, 2024

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

Glad it worked for you

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2024 Mar 14, 2024

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

 

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2024 Mar 14, 2024
LATEST

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);
        }
    }
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines