Hi all-
I'm using a script I found here to override all master items across an entire document, but I need to add a subroutine (or modify the existing one) to then detach all those elements from the master as well.
My scripting skills are limited to very basic stuff, this is beyond me. Can anyone help, or point me in the right direction? Thanks!
Here's the current script to override the master page elements (which works perfectly, thanks to whoever originally wrote this!):
#target indesign;
var myDocument = app.activeDocument;
var TotalPages = (myDocument.pages.count());
for(var CurrentPage=0; CurrentPage < TotalPages; CurrentPage++) {
OverrideMasterItems();
}
function OverrideMasterItems() {
var allItems = myDocument.pages[CurrentPage].appliedMaster.pageItems.everyItem().get Elements();
for(var i=0;i<allItems.length;i++){
try{allItems[i].override(myDocument.pages[CurrentPage])}
catch(e){}
}
}
See if this does what you need:
#target indesign
var count = app.documents.count();
// Make sure there is an active document first
if (count > 0) {
processDocument(app.activeDocument);
} else {
alert ("You don't have any documents open.");
}
function processDocument(docRef) {
if (docRef == null || docRef == '') {
return false;
}
var masterCount = docRef.masterSpreads.length;
if (masterCount > 0) {
for( var i = 0; i < masterCount; i++) {
var m = docRef.masterSpreads[i];
var pageCount = m.pages.length;
if (pageCount > 0) {
for( var j = 0; j < pageCount; j++) {
var p = m.pages[j];
OverrideMasterItems(p);
}
}
}
}
var pageCount = docRef.pages.length;
if (pageCount > 0) {
for( var i = 0; i < pageCount; i++) {
var p = docRef.pages[i];
OverrideMasterItems(p);
}
}
}
function OverrideMasterItems(p) {
if (p == null || p == '') {
return false;
}
if (p.appliedMaster == null || p.appliedMaster == '') {
return false;
}
// Override all items on Page
var allItems = p.appliedMaster.pageItems.everyItem().getElements();
for(var i=0;i<allItems.length;i++){
var pi = allItems[i];
if (!pi.overridden) {
try{
allItems[i].override(p);
}
catch(e){}
}
}
// Remove master from page
p.appliedMaster = NothingEnum.nothing;
} // END OverrideMasterItems
PageItem has a detach() method:
function main() {
var doc = app.activeDocument,
i, l, page, j, k;
for (i = 0, l = doc.pages.length; i < l; i++) {
page = doc.pages[i];
if (page.appliedMaster !== null) {
for (j = 0, k = page.appliedMaster.pageItems.length; j < k; j++) {
try {
page.appliedMaster.pageItems[j].override(page);
} catch(e) {}
}
}
page.pageItems.everyItem().detach();
}
}
if (app.documents.length > 0) {
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Detach Master Page Items");
}
Jeff
bpylant wrote:
I wish I knew more about this stuff... every time I start to teach myself to code I get overwhelmed so quickly. I should really take some classes, I guess.
If your primary interest is in scripting InDesign, I would recommend you start with Peter Kahrel's book, which will help you begin doing actual work without getting bogged down (and overwhelmed!) in abstractions—that stuff can wait for later.
North America
Europe, Middle East and Africa
Asia Pacific