Hey everyone,
I’m currently working on a small InDesign UXP plugin that allows users to select multiple InDesign files and then merge the pages from these files into a new document. Here’s a simple example of what I’m trying to achieve:
Imagine I have three documents, each containing just one page (Document 1, Document 2, and Document 3). The user selects these files, clicks “Merge,” and the plugin creates a new target document. It then opens each selected document in turn and moves the pages into this new target document.
Most of my code is working fine, but I’m running into an issue when trying to move a page from one document to another.
The line of code that’s giving me trouble is:
await page.move(LocationOptions.AT_END, mainDoc.pages[-1]);
Here, page
represents a page from the source document, and mainDoc
is the new target document I’ve created. Unfortunately, this line doesn’t seem to work as expected.
Interestingly, when I use this line:
await page.duplicate(LocationOptions.AT_END, tempDoc.pages[-1]);
It works perfectly—but only within the same document (tempDoc
).
It seems that while the duplicate
method works fine within a single document, I’m struggling to move pages across different documents.
I’d really appreciate any advice on how to properly move a page from one document to another in this context. Is there a different method or approach I should be using?
Below is the full code snippet for reference:
import React, { useState } from "react";
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const fs = require("uxp").storage.localFileSystem;
const app = require("indesign").app;
const { LocationOptions, SaveOptions } = require("indesign");
export default function FilePicker() {
const [selectedFiles, setSelectedFiles] = useState<
{ name: string; nativePath: string }[]
>([]);
const handleFileSelection = async () => {
try {
const files = await fs.getFileForOpening({
allowMultiple: true,
types: ["indd"],
});
console.log("Selected files", files);
setSelectedFiles(files);
} catch (err) {
console.log("Error selecting files", err);
}
};
const handleClearCache = () => {
setSelectedFiles([]);
console.log("Selected files cleared");
};
const handleMergeFiles = async () => {
console.log("Selected files in state", selectedFiles);
if (selectedFiles.length === 0) {
console.log("No files selected");
return;
}
try {
// 1. Create a new InDesign document
const mainDoc = await app.documents.add();
console.log("Main Document:", mainDoc);
// 2. Loop through selected files and merge pages
for (const file of selectedFiles) {
const tempDoc = await app.open(file.nativePath, true); // Open file in background
await sleep(5000); // Wait for the file to open
console.log("Opened Document:", tempDoc);
if (tempDoc && tempDoc.isValid) {
console.log("Document is valid");
const pages = tempDoc.pages.everyItem().getElements();
console.log(`Found ${pages.length} pages in ${file.name}`);
console.log("Pages:", pages);
for (const page of pages) {
console.log("Moving page:", page);
await page.move(LocationOptions.AT_END, mainDoc.pages[-1]);
}
} else {
console.error(
`Document is not valid or could not be opened: ${file.nativePath}`
);
}
}
console.log("Documents merged successfully.");
} catch (error) {
console.error("Error merging documents:", error);
}
};
Thanks in advance for any help or suggestions!
Best,
Alex