Reply To: How to save a single Indesign page from a multipage ID document

Home Page / Forums / General InDesign Topics (CLOSED) / How to save a single Indesign page from a multipage ID document / Reply To: How to save a single Indesign page from a multipage ID document

#14404159

try it:

// ExtendScript for Adobe InDesign
// This script prompts for a range of pages to keep and saves a new document with those pages.

#target indesign

// Function to show input dialog and get page ranges
function getPageRanges() {
var dialog = app.dialogs.add({name: "Select Pages to Keep"});
with (dialog.dialogColumns.add()) {
staticTexts.add({staticLabel: "Enter page numbers or ranges (e.g., 1,3-5,7):"});
var inputField = textEditboxes.add({editContents: "", minWidth: 400}); // Adjusted width to be 2x wider
}
if (dialog.show() == true) {
var pageRanges = inputField.editContents;
dialog.destroy();
return pageRanges;
} else {
dialog.destroy();
return null;
}
}

// Function to parse page ranges and return an array of page numbers
function parsePageRanges(pageRanges) {
var pagesToKeep = [];
var ranges = pageRanges.split(",");
for (var i = 0; i < ranges.length; i++) {
if (ranges[i].indexOf("-") > -1) {
var range = ranges[i].split("-");
var start = parseInt(range[0]);
var end = parseInt(range[1]);
for (var j = start; j <= end; j++) {
pagesToKeep.push(j);
}
} else {
pagesToKeep.push(parseInt(ranges[i]));
}
}
return pagesToKeep;
}

// Main function
function main() {
if (app.documents.length === 0) {
alert("No document open.");
return;
}

var doc = app.activeDocument;
var pageRanges = getPageRanges();
if (!pageRanges) {
return;
}

var pagesToKeep = parsePageRanges(pageRanges);
var newDoc = app.documents.add();
newDoc.properties = doc.properties;

// Copy pages to new document
for (var i = 0; i < pagesToKeep.length; i++) {
var pageIndex = pagesToKeep[i] - 1;
if (pageIndex >= 0 && pageIndex < doc.pages.length) {
doc.pages[pageIndex].duplicate(LocationOptions.AT_END, newDoc.pages[-1]);
}
}

// Remove blank page that is created by default
newDoc.pages[0].remove();

// Save the new document
var originalFilePath = doc.fullName.absoluteURI;
var newFilePath = originalFilePath.replace(/\.indd$/, "_pages.indd");
newDoc.save(new File(newFilePath));

alert("New document saved as: " + newFilePath);
newDoc.close();
}

main();

This article was last modified on June 17, 2024

Comments (0)

Loading comments...