如何从其他文档页面的子集中创建新文档(使用ExtendScript在InDesign(CS6)中)?

时间:2012-06-04 20:37:47

标签: extendscript adobe-indesign

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

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

原始文件:

Overview of the original document. The middle is removed (red lines).

新文件:

Overview of the new document. The middle is removed (red lines).

问题:如何在其他文档页面的子集中创建新文档(在使用ExtendScript的InDesign中),而不会在屏幕截图中显示问题?

注意:在CS5.5和CS6中,脚本的行为完全不同。我的问题与CS6有关。


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

CODE

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();

1 个答案:

答案 0 :(得分: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();
   }
};