Skip to main content
Participant
February 25, 2024
Answered

Script - move or copy selected documents into one document

  • February 25, 2024
  • 2 replies
  • 1067 views

Hello,

I'm fairly new to InDesign and would appreciate some assistance. I've been attempting to write a script for the past few days.

Let's say I have 5 unsaved documents open. What I'd like to achieve is to select a certain number of documents (for example, 2 documents, which may have varying numbers of pages but are of the same size), and merge them into one document. This merged document can either replace one of the original two or be created as a new document. (Additional information: the document size is landscape 297 x 210 mm with 5 mm margins and no facing pages).

However, the documents that were merged would need to be closed without being saved.

If my expectations are too high, I apologize.

Thank you for your assistance!

Correct answer Kasyan Servetsky

Merge files by Michael Zaichenko

Mass combine a bunch of indd files into one by Simon Wiscombe
Put all the files you want to combine into one document in a folder, named in the order you want them to appear in the book (e.g. "001.indd", "002.indd", etc. or something) so they appear correctly when sorted by name in finder/explorer. Set the destination document as the active document.

2 replies

Kasyan Servetsky
Kasyan ServetskyCorrect answer
Legend
February 26, 2024

Merge files by Michael Zaichenko

Mass combine a bunch of indd files into one by Simon Wiscombe
Put all the files you want to combine into one document in a folder, named in the order you want them to appear in the book (e.g. "001.indd", "002.indd", etc. or something) so they appear correctly when sorted by name in finder/explorer. Set the destination document as the active document.

Jan MaricAuthor
Participant
February 26, 2024

Thank you @Kasyan Servetsky this helped me a lot.

I helped myself with chatGPT and managed to get the desired result. Otherwise, in some cases it folds the pages incorrectly. Across the width instead of down.
But it doesn't affect my usage.

I am attaching the script if anyone needs it. (comments are only in Slovenian). 

 

When the script is started, a window with all open documents opens. The desired ones are selected, and then they are combined into one document.
However, all downloaded documents are closed without being saved.

 

 

#target indesign

// Preveri, če so odprti dokumenti
if (app.documents.length == 0) {
alert("Ni odprtih dokumentov.");
exit(0);
}

// Ustvari dialogovno okno za izbiro dokumentov
var dialog = new Window("dialog", "Izberite dokumente za združitev");

// Panel za izbiro dokumentov z kvadratki
var docPanel = dialog.add("panel", undefined, "Dokumenti:");
docPanel.alignChildren = "left";
var checkboxes = [];
for (var i = 0; i < app.documents.length; i++) {
checkboxes.push(docPanel.add("checkbox", undefined, app.documents[i].name));
}

// Gumbi za dialog
var buttons = dialog.add("group");
buttons.alignment = "center";
buttons.add("button", undefined, "V redu", {name: "ok"});
buttons.add("button", undefined, "Prekliči", {name: "cancel"});

// Prikaži dialog in preveri izbiro
if (dialog.show() == 1) { // V redu
var selectedDocuments = [];
for (var i = 0; i < checkboxes.length; i++) {
if (checkboxes[i].value) {
selectedDocuments.push(app.documents.itemByName(checkboxes[i].text));
}
}

// Preveri, če je bil izbran vsaj en dokument
if (selectedDocuments.length < 1) {
alert("Izberite vsaj en dokument.");
exit(0);
}

// Izračunaj skupno število strani za napredek
var totalPages = 0;
for (var i = 0; i < selectedDocuments.length; i++) {
if (selectedDocuments[i] != selectedDocuments[0]) { // Preskoči ciljni dokument
totalPages += selectedDocuments[i].pages.length;
}
}

// Ustvari okno za napredek
var progressWin = new Window('palette', 'Napredek združevanja');
var progressBar = progressWin.add('progressbar', undefined, 0, totalPages);

// Nastavi velikost okna za napredek
progressWin.preferredSize.width = 300; // Nastavi prednostno širino
progressBar.preferredSize.width = 280; // Nastavi prednostno širino za napredovalno vrstico

// Center okna na zaslonu
progressWin.center();

// Prikaži okno za napredek
progressWin.show();
var pagesDone = 0; // Za sledenje napredka

// Nastavi ciljni dokument kot prvi izbrani dokument
var destination_doc = selectedDocuments[0];

// Nastavi ciljni dokument na enostranske razpredelnice
destination_doc.documentPreferences.facingPages = false;

// Preglej izbrane dokumente za združitev
for (var i = 1; i < selectedDocuments.length; i++) { // Začni z 1, ker je 0 ciljni dokument
var source_doc = selectedDocuments[i];

// Preglej vse strani v trenutnem dokumentu
for (var j = 0; j < source_doc.pages.length; j++) {
var sourcePage = source_doc.pages.item(j);

// Prekini povezavo z master stranmi, če je potrebno
var masterItems = sourcePage.masterPageItems;
if (masterItems.length > 0) {
for (var k = 0; k < masterItems.length; k++) {
masterItems[k].override(sourcePage);
}
}

// Odstrani uporabljeno master stran
sourcePage.appliedMaster = null;

// Preveri, če je število strani v zadnji razpredelnici večje ali enako 10
var lastSpread = destination_doc.spreads.lastItem();
if (lastSpread.pages.length >= 10) {
// Dodaj novo stran na koncu, kar bo po potrebi ustvarilo novo razpredelnico
destination_doc.pages.add(LocationOptions.AT_END);
}

// Dupliciraj stran v izvornem dokumentu in jo premakni v ciljni dokument
sourcePage.duplicate(LocationOptions.AT_END, destination_doc.pages.item(-1));

// Posodobi napredek
pagesDone++;
progressBar.value = pagesDone;
}

// Zapri izvorni dokument brez shranjevanja
source_doc.close(SaveOptions.NO);
}

// Zapri okno za napredek
progressWin.close();

} else { // Prekliči
exit(0);
}

 

 

Known Participant
May 16, 2025

Hello!

This does combine files, but it loses the text threads in the files after the first one. Since I'm working in book publishing, my need is to, for example, combinge 10 individual multi-page lessons into a single file. (Not using the ID Book feature, but actually having one ID file so I can combine master pages and bulk edit styles in 1 file instead of 10).

 

The Move Pages.... command works well manually and does exactly what I am trying to automate, but all of the scripts I've seen break the text threads across pages. 

 

I would welcome any ideas you have. I am not a programmer, but I love using time-saving scripts that also save my hands and wrists from repetitive stress injuries!

 

-Carol

brian_p_dts
Community Expert
Community Expert
February 26, 2024

What is the script you're attempting?