//DESCRIPTION: Shuffles pages for booklet printing
/*
Written by Dave Saunders
Latest Revision: March 11, 2005
Copyright ©2005 PDS Associates
*/
// This version is derived from earlier versions posted often on the Adobe U2U forum
if (app.documents.length == 0) {
alert("There are no open documents.");
exit();
}
// Make sure that the pages are a multiple of four
myPages = app.documents[0].pages;
if (myPages.length % 4 != 0) {
alert("Document length isn't a multiple of four.");
exit();
}
// Document is suitable for converting to a booklet
/*
Before proceeding, check to see if document needs to be saved
*/
if (app.documents[0].modified) {
if (confirm("Document has unsaved changes. Click OK to save them and proceed.")){
app.documents[0].save(app.documents[0].fullName);
} else {
exit();
}
}
// Some trickiness to get a copy of the document opened
app.userInteractionLevel = UserInteractionLevels.neverInteract;
myFN = app.documents[0].fullName;
myName = app.documents[0].name;
for (n=0; 1000>n; n++) {
myTN = app.documents[0].filePath + "/" + myName.split(".indd")[0] + String(n) + ".indt";
if (File(myTN).exists) { continue } // Template already exists, try another name
app.documents[0].save(File(myTN),true); // Save as template
app.documents[0].close();
app.open(File(myTN)); // Open another untitled copy
File(myTN).remove();
break; // This loop will drop through if you already have 1000 template files with the same
// name as your document with 0 - 999 as postfix; This is highly unlikely, but if it does
// happen, I just go ahead and work on the original; the only warning will be that the
// booklet document won't be untitled; it'll have the same name as your original document.
}
app.userInteractionLevel = UserInteractionLevels.interactWithAll;
app.documents[0].documentPreferences.allowPageShuffle = false;
for (i=0; myPages.length > i; i++) {
origSection = myPages[i].appliedSection;
origNumbering = origSection.pageNumberStyle;
origSection.pageNumberStyle = PageNumberStyle.arabic;
pageName = myPages[i].name;
sectName = origSection.name;
if (sectName != "") {
pageName = pageName.split(sectName)[1];
}
pageNo = Number(pageName);
newSection = app.documents[0].sections.add(undefined, undefined, {pageStart:app.documents[0].pages[i], continueNumbering:false, pageNumberStart:pageNo, pageNumberStyle:origNumbering, marker: origSection.marker, name: origSection.name});
origSection.pageNumberStyle = origNumbering;
}
app.documents[0].documentPreferences.allowPageShuffle = true;
for (i=0; (myPages.length/2)>i; i++){
if (i % 2 == 0) {
app.documents[0].pages[myPages.length - 1].move(LocationOptions.before,app.documents[0].pages[i*2],BindingOptions.leftAlign);
} else {
app.documents[0].pages[myPages.length - 1].move(LocationOptions.after,app.documents[0].pages[i*2],BindingOptions.rightAlign);
}
}
// Just in case the shuffling has affected the position of a text-wrapped group:
app.activeDocument.textPreferences.zOrderTextWrap = !app.activeDocument.textPreferences.zOrderTextWrap;
app.activeDocument.textPreferences.zOrderTextWrap = !app.activeDocument.textPreferences.zOrderTextWrap;