Skip to main content
Participating Frequently
November 5, 2024
Question

InDesign import script works but overfills ...

  • November 5, 2024
  • 2 replies
  • 268 views

1. I create an InDesign page with x number of boxes.

2. I select the script and it asks me to point it to an AI file. This AI file has multiple pages the same size as the boxes in the iNDD doc. 

3. the script then fills the selected boxes in the INDD file with the AI doc pages.

 

Perfect! But, once the import is completed, if there are any boxes left, the script fills them with the first page of the imported AI file. I need them to remain blank.

 

For example, I create an INDD file with 126 boxes. I select those boxes and activate the script. It imports all 220 pages form the AI file then continues on to fill the remaining 16 boxes with the first page of the AI file.

 

I've tried everyhitng I can think of, but cant figure out how to stop that. Any advice appreciated (please send the full updated code  if you can figure it out)

 

thanks.

 

// Import Pages to Frames Script for Adobe InDesign with PDF and AI Support
// Fills frames left-to-right, top-to-bottom, clears extra frames after pages run out

#target indesign

function main() {
    var doc = app.activeDocument;

    // Ensure there are selected frames
    if (app.selection.length === 0) {
        alert("Please select one or more frames on the current page.");
        return;
    }

    // Filter selected items to include only frames on the active spread
    var activeSpread = doc.layoutWindows[0].activeSpread;
    var selectedFrames = [];
    for (var i = 0; i < app.selection.length; i++) {
        var item = app.selection[i];
        if (item instanceof Rectangle && item.parentPage !== null && item.parentPage.parent === activeSpread) {
            selectedFrames.push(item);
        }
    }

    if (selectedFrames.length === 0) {
        alert("No frames selected on the current spread.");
        return;
    }

    // Sort frames by Y-coordinate first (top to bottom), then by X-coordinate (left to right)
    selectedFrames.sort(function(a, b) {
        var aBounds = a.geometricBounds;
        var bBounds = b.geometricBounds;
        return (aBounds[0] - bBounds[0]) || (aBounds[1] - bBounds[1]);
    });

    // Prompt user to select an InDesign, PDF, or Illustrator document
    var importFile = File.openDialog("Select an InDesign, PDF, or Illustrator document to import pages from", "*.indd;*.pdf;*.ai");
    if (!importFile) {
        alert("No file selected. Script will exit.");
        return;
    }

    // Determine the file type based on extension
    var fileType = importFile.name.split('.').pop().toLowerCase();
    var numPagesPlaced = 0;

    try {
        if (fileType === "indd") {
            // Open InDesign file and get pages
            var importDoc = app.open(importFile, true);
            var pages = importDoc.pages;
            var numPages = pages.length;

            // Place each page in a frame, stop as soon as pages are exhausted
            for (var i = 0; i < selectedFrames.length && i < numPages; i++) {
                var targetFrame = selectedFrames[i];
                var placedPage = pages[i].duplicate(LocationOptions.AT_END, doc.pages[doc.pages.length - 1]);

                // Center and fit placed page in the frame
                var pageBounds = targetFrame.geometricBounds;
                var pageItem = doc.pages[doc.pages.length - 1].pageItems[0];

                pageItem.geometricBounds = pageBounds;
                pageItem.fit(FitOptions.CENTER_CONTENT);
                
                numPagesPlaced++;
            }
            importDoc.close(SaveOptions.NO);

        } else if (fileType === "pdf" || fileType === "ai") {
            // Place each page from a PDF or AI file, one per frame, stop if pages are exhausted
            var pdfOptions = app.pdfPlacePreferences;
            pdfOptions.pdfCrop = PDFCrop.CROP_MEDIA;

            for (var i = 0; i < selectedFrames.length; i++) {
                pdfOptions.pageNumber = i + 1;
                var targetFrame = selectedFrames[i];

                try {
                    var placedItem = targetFrame.place(importFile)[0];
                    placedItem.geometricBounds = targetFrame.geometricBounds;
                    placedItem.fit(FitOptions.CENTER_CONTENT);
                    
                    numPagesPlaced++;
                } catch (e) {
                    break; // Stop if there are no more pages to place
                }
            }

        } else {
            alert("Unsupported file type. Please use an InDesign, PDF, or Illustrator file.");
            return;
        }

        // Clean up extra frames that were filled with the first page
        var firstFrameContent = selectedFrames[0].allGraphics[0];
        for (var j = numPagesPlaced; j < selectedFrames.length; j++) {
            var frame = selectedFrames[j];
            if (frame.allGraphics.length > 0 && frame.allGraphics[0].itemLink.name === firstFrameContent.itemLink.name) {
                frame.allGraphics[0].remove(); // Clear content if it matches the first page
            }
        }

    } catch (e) {
        alert("An error occurred: " + e.message);
    }
}

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Import Pages to Frames Sequentially with Cleanup");
This topic has been closed for replies.

2 replies

John D Herzog
Inspiring
November 5, 2024

Have you tried changing your loops to the length of the placed items instead of the selected boxes?

RP&S SSAuthor
Participating Frequently
November 5, 2024

I got around it by adding the number of pages to the filename that's being imported, then having the script reference that, and only fill that number of the selected boxes.

RP&S SSAuthor
Participating Frequently
November 5, 2024

I mistyped..... For example, I create an INDD file with 126 boxes. I select those boxes and activate the script. It imports all 110 pages form the AI file then continues on to fill the remaining 16 boxes with the first page of the AI file.