2

我需要提供一项功能,允许 InDesign 用户在 InDesign 文档中选择页面范围并从这些页面中创建新文档。这听起来很简单,但事实并非...

我尝试了许多不同的方法来做到这一点,但它们都在某种程度上失败了。有些方法将所有页面放在一个跨页中(这有时会导致 InDesign 崩溃)。我所能做的最好的(见下面的代码)在开始和结束时仍然存在问题(见下面的截图):

原始文件:

原始文件概览。 中间被删除(红线)。

新文件:

新文档概述。 中间被删除(红线)。

问题:如何从另一个文档页面的子集(在 InDesign 中使用 ExtendScript)创建新文档而不会出现屏幕截图中显示的问题?

注意:脚本的行为在 CS5.5 和 CS6 中完全不同。我的问题涉及 CS6。


第二个屏幕截图是通过将以下代码应用于第一个屏幕截图中显示的文档获得的:

代码

var firstPageName = { editContents: "117" };  // This page number is actually entered by the user in an integerEditbox
var lastPageName = { editContents: "136" };  // This page number is actually entered by the user in an integerEditbox
var sourceDocument = app.activeDocument;
var destDocument = app.documents.add();
destDocument.importStyles(ImportFormat.paragraphStylesFormat, new File(sourceDocument.filePath + "/" + sourceDocument.name), GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE);
destDocument.importStyles(ImportFormat.characterStylesFormat, new File(sourceDocument.filePath + "/" + sourceDocument.name), GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE);
destDocument.viewPreferences.horizontalMeasurementUnits = sourceDocument.viewPreferences.horizontalMeasurementUnits;
destDocument.viewPreferences.verticalMeasurementUnits = sourceDocument.viewPreferences.verticalMeasurementUnits;
destDocument.documentPreferences.facingPages = sourceDocument.documentPreferences.facingPages;
destDocument.documentPreferences.pageHeight = sourceDocument.documentPreferences.pageHeight;
destDocument.documentPreferences.pageWidth = sourceDocument.documentPreferences.pageWidth;
destDocument.documentPreferences.pageSize = sourceDocument.documentPreferences.pageSize;
var sourceSpreads = sourceDocument.spreads;
var nbSourceSpreads = sourceSpreads.length;
var firstPageFound = false;
var lastPageFound = false;
var i;
var newSpreadNeeded;
var currentDestSpread;

for (i = 0; !lastPageFound, i < nbSourceSpreads; ++i) {
  newSpreadNeeded = true;
  var sourcePages = sourceSpreads[i].pages;
  var nbSourcePages = sourcePages.length;
  var j;
  for (j = 0; !lastPageFound, j < nbSourcePages; ++j) {
    if (sourcePages[j].name === firstPageName.editContents) {
      firstPageFound = true;
      destDocument.documentPreferences.startPageNumber = parseInt(firstPageName.editContents);  // We want to preserve page numbers
    }
    if (firstPageFound) {
      // Copy this page over to the new document.
      var firstInNewSpread = false;
      if (newSpreadNeeded) {
        currentDestSpread = destDocument.spreads.add();
        newSpreadNeeded = false;
        firstInNewSpread = true;
      }
      var newPage = sourcePages[j].duplicate(LocationOptions.AT_END, currentDestSpread);
      var k;
      for (k = 0; k < newPage.index; ++k) {
        currentDestSpread.pages[k].remove();
      }
    }
    if (sourcePages[j].name === lastPageName.editContents) {
      lastPageFound = true;
    }
  }
}
destDocument.spreads[0].remove();
4

1 回答 1

1

我到处乱搞,想出了这个小脚本。虽然它从相反的方向解决问题,但它似乎在这里工作正常。另外,我还在 InDesign CS5 中运行,但也许它对你有用。希望我明白你的问题的要点?

这会将第 3 页到第 5 页提取到一个单独的文档中:

var doc = app.activeDocument;

var newFilePath = doc.filePath + "/subset_" + doc.name;
var newFile = File(newFilePath); // Create a new file path

doc.saveACopy(newFile); // Save a copy of the doc
var newDoc = app.open(newFile); // Open the copy

var firstPageNum = 3; // First page number in the range
var lastPageNum = 5;  // Last page number in the range

var firstPage = newDoc.pages[firstPageNum-1];
var lastPage = newDoc.pages[lastPageNum-1];

// Remove all text from the last page in the range to the end of the document
var lastPageFrames = lastPage.textFrames.everyItem().getElements();
for (var i=0; i < lastPageFrames.length; i++) {
   var frame = lastPageFrames[i];
   var parentStory = frame.parentStory;
   var lastFrameInsert = frame.insertionPoints.lastItem();
   var lastStoryInsert = parentStory.insertionPoints.lastItem();
   var textAfter = parentStory.insertionPoints.itemByRange(lastFrameInsert,lastStoryInsert);
   textAfter.remove();
};

// Remove all text from the beginning of the document to the first page in the range
var firstPageFrames = firstPage.textFrames.everyItem().getElements();
for (var i=0; i < firstPageFrames.length; i++) {
   var frame = firstPageFrames[i];
   var parentStory = frame.parentStory;
   var firstFrameInsert = frame.insertionPoints.firstItem();
   var textBefore = parentStory.insertionPoints.itemByRange(0,firstFrameInsert.index);
   textBefore.remove();
};

// Remove the pages that aren't in the range
var allPages = newDoc.pages.everyItem().getElements();
for (var i=0; i < allPages.length; i++) {
   var page = allPages[i];
   if (i < firstPageNum || i > lastPageNum) {
      page.remove();
   }
};
于 2012-06-30T04:01:02.133 回答