Skip to content

Instantly share code, notes, and snippets.

@trych
Last active August 2, 2016 22:37
Show Gist options
  • Save trych/f6ede3b3038b403d6fea08ad77f620dd to your computer and use it in GitHub Desktop.
Save trych/f6ede3b3038b403d6fea08ad77f620dd to your computer and use it in GitHub Desktop.
Bundschnitt erstellen
#target indesign
var doc = app.activeDocument;
doc.viewPreferences.rulerOrigin = RulerOrigin.SPREAD_ORIGIN;
doc.documentPreferences.allowPageShuffle = false;
cleanDocument(doc);
insertSpreads(doc);
groupObjects(doc);
deletePages(doc);
ungroupTempGroups(doc);
removeOppositePageItems(doc);
fixBleed(doc);
//------------------------------------------------------------------------------
// script functions
//------------------------------------------------------------------------------
function cleanDocument(doc) {
// removes all pageItems that are on the pasteBoard, as those can cause issues later when ungrouping the grouped items
for (var i = doc.spreads.length - 1; i >= 0; i--) {
var s = doc.spreads[i]
for (var j = s.pageItems.length - 1; j >= 0; j--) {
if (s.pageItems[j].parentPage === null) s.pageItems[j].remove();
}
}
}
function insertSpreads(doc) {
// inserts new spreads after each spread to duplicate
// duplicating the spread does not help, as pageItems go missing, if the left page is deleted (even if they are all grouped together with their middle somewhere on the right)
for (var i = doc.spreads.length - 1; i >= 0; i--) {
var s = doc.spreads[i]; // multiplied by 2, because in each loop there is a new spread inserted
if(s.pages.length === 1) continue;
doc.spreads.add(LocationOptions.AFTER, s);
};
}
function groupObjects(doc) {
// group the objects, so they will not be deleted, if their underlying page gets removed .
var pageWidth = doc.pages[0].bounds[3] - doc.pages[0].bounds[1];
var counter = 0;
for (var i = 0; i < doc.spreads.length; i += 1) {
var s = doc.spreads[i];
if(s.pages.length === 1) continue;
if(counter % 2 === 0){
s.pageItems.everyItem().locked = false; // unlock all items for grouping
s.rectangles.add({name: "TEMP_FRAME", geometricBounds: [0, -pageWidth, 20, pageWidth]}); // to make sure the group is not on the right hand side
s.groups.add(s.pageItems.everyItem().getElements(), {name: "TEMP_GROUP"});
}
counter += 1;
};
}
function deletePages(doc) {
var counter = 0;
for (var i = doc.spreads.length - 1; i >= 0; i--) {
var s = doc.spreads[i];
if(s.pages.length === 1) continue;
if(counter % 2 === 0) {
s.pages[0].remove();
} else {
s.groups.item("TEMP_GROUP").duplicate(doc.spreads.nextItem(s));
s.pages[1].remove();
}
counter += 1;
}
}
function ungroupTempGroups(doc) {
for (var i = doc.spreads.length - 1; i >= 0; i--) {
var g = doc.spreads[i].groups.item("TEMP_GROUP");
if (g.isValid) g.ungroup();
var r = doc.spreads[i].rectangles.item("TEMP_FRAME");
if (r.isValid) r.remove();
}
}
function removeOppositePageItems(doc) {
var tolerance = 0.2;
for (var i = doc.spreads.length - 1; i >= 0; i--) {
var s = doc.spreads[i];
var p = s.pages[0];
for (var j = s.pageItems.length - 1; j >= 0; j--) {
var pI = s.pageItems[j];
var pIb = pI.geometricBounds;
if (s.pages[0].side === PageSideOptions.LEFT_HAND) {
if (pIb[1] > (p.bounds[3] - tolerance) ) pI.remove();
} else {
if (pIb[3] < tolerance) pI.remove();
}
}
}
}
function fixBleed(doc) {
var tolerance = 0.2;
var bleed = doc.documentPreferences.documentBleedInsideOrLeftOffset;
for (var i = doc.spreads.length - 1; i >= 0; i--) {
var s = doc.spreads[i];
var p = s.pages[0];
for (var j = s.pageItems.length - 1; j >= 0; j--) {
var pI = s.pageItems[j];
var pIb = pI.geometricBounds;
if (s.pages[0].side === PageSideOptions.LEFT_HAND) {
if (pIb[3] > (p.bounds[3] - tolerance) && pIb[3] < p.bounds[3] + bleed ) {
pI.geometricBounds = [pIb[0], pIb[1], pIb[2], p.bounds[3] + bleed];
}
} else {
if (pIb[1] > -bleed && pIb[1] < tolerance) {
pI.geometricBounds = [pIb[0], -bleed, pIb[2], pIb[3]];
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment