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

InDesign script for placing .indd files automatically.

New Here ,
Aug 16, 2024 Aug 16, 2024

Hello, I'm trying to create a script that would automatically place and position documents in my Indesign file automatically for the purpose of setting up a file for sending to print.

 

The script below currently handles PDFs exactly the way I need them to (placing each page of the PDF sequentially into each page of the  InDesign document), but struggles to to do the same for other filetypes, where it places the 1st page throughout the whole document. It would be ideal if i could get this to work for .indd files.

 

I'm not particularly savvy with script writing, so have been using ChatGPT to create the script, but so far nothing has worked. 

 

If anyone can help that would be greatly appreciated. (script is below)

 

#target indesign

// Get the active document
var doc = app.activeDocument;

// Prompt the user to select a file to place
var fileToPlace = File.openDialog("Select a file to place", "*.*"); // *.* allows all file types

if (fileToPlace) {
// Disable screen redraw to improve performance
app.scriptPreferences.enableRedraw = false;

// Loop through each page in the document
for (var p = 0; p < doc.pages.length; p++) {
var page = doc.pages[p];
var pageCenterX = (page.bounds[1] + page.bounds[3]) / 2; // Calculate the vertical center of the page
var pageCenterY = (page.bounds[0] + page.bounds[2]) / 2; // Calculate the horizontal center of the page

// Place the graphic in the center of the page
try {
var rect = page.rectangles.add();
rect.geometricBounds = [
pageCenterY - 50, // Top (arbitrary initial size)
pageCenterX - 50, // Left (arbitrary initial size)
pageCenterY + 50, // Bottom (arbitrary initial size)
pageCenterX + 50 // Right (arbitrary initial size)
];

var graphic;

// Place each page of the file sequentially if it's a multi-page document
if (fileToPlace.name.match(/\.pdf$/i)) {
app.pdfPlacePreferences.pageNumber = p + 1; // Move to the next page in the PDF
graphic = rect.place(fileToPlace)[0];
} else {
// For non-PDF files, place the same file on every page
graphic = rect.place(fileToPlace)[0];
}

// Rotate the graphic 90 degrees counter-clockwise
graphic.rotationAngle = -90;

// Fit the rotated graphic into the frame by scaling it proportionally
rect.fit(FitOptions.CONTENT_TO_FRAME);
graphic.horizontalScale = 100;
graphic.verticalScale = 100;

// Recalculate bounds to ensure the rotated graphic is aligned correctly
var graphicBounds = graphic.geometricBounds;
var rectBounds = rect.geometricBounds;

var graphicWidth = graphicBounds[3] - graphicBounds[1];
var graphicHeight = graphicBounds[2] - graphicBounds[0];

// Align the rotated graphic so that its right edge is at the center of the page
var newLeft = pageCenterX - graphicWidth;
var newTop = pageCenterY - graphicHeight / 2;

rect.geometricBounds = [newTop, newLeft, newTop + graphicHeight, newLeft + graphicWidth];
graphic.geometricBounds = rect.geometricBounds;

} catch (e) {
alert("An error occurred while placing the graphic: " + e.message);
break;
}
}

// Second loop: Align the file to the center and duplicate it
for (var p = 0; p < doc.pages.length; p++) {
var page = doc.pages[p];
var pageCenterX = (page.bounds[1] + page.bounds[3]) / 2; // Calculate the vertical center of the page

// Collect all rectangles with placed graphics
var rectangles = [];
for (var r = 0; r < page.rectangles.length; r++) {
var rect = page.rectangles[r];
if (rect.graphics.length > 0 && rect.graphics[0].itemLink) {
rectangles.push(rect);
}
}

// Process the collected rectangles
for (var i = 0; i < rectangles.length; i++) {
var rect = rectangles[i];
try {
// Duplicate the frame
var duplicatedRect = rect.duplicate();
var rectWidth = rect.geometricBounds[3] - rect.geometricBounds[1];
var rectHeight = rect.geometricBounds[2] - rect.geometricBounds[0];

// Set the geometric bounds for the duplicated frame
duplicatedRect.geometricBounds = [
rect.geometricBounds[0], // Top remains the same
pageCenterX, // Left is set to the vertical center
rect.geometricBounds[0] + rectHeight, // Bottom calculated based on height
pageCenterX + rectWidth // Right calculated based on width
];

// Align the duplicate's content to the new frame bounds
if (duplicatedRect.graphics.length > 0) {
var dupGraphic = duplicatedRect.graphics[0];
dupGraphic.geometricBounds = duplicatedRect.geometricBounds;
}
} catch (e) {
alert("Error duplicating graphic: " + e.message);
}
}
}

// Re-enable screen redraw
app.scriptPreferences.enableRedraw = true;

alert("Finished placing and duplicating the file on all pages.");
} else {
alert("No file selected.");
}

TOPICS
Scripting
240
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
New Here ,
Aug 16, 2024 Aug 16, 2024

Managed to resolve this in the end. If anyone is having a similar issue, i've attached the script I used as a text file below.

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
LEGEND ,
Aug 16, 2024 Aug 16, 2024
LATEST
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