2009-10-23

Delete Unused Master Pages

Master Pages in Adobe InDesign are a great way to automate the production of pages. But Master Pages, also called Master Spreads, can have a serious impact of production in a negative way too. Every Master Page added to a template or document increases the file size. And sometimes, if the Master Pages aren't built in a efficient and productive manner, the increase in size can be substantial.
And late in the production cycle, that extra file size slows down every save, every print, every export, every open, everything.
The partial solution is to delete the unused Master Pages. And this function does that.


function xUnusedMasters( docRef ) {
//-------------------------------------------------------------------------
//-- X U N U S E D M A S T E R S
//-------------------------------------------------------------------------
//-- Generic: Yes for Adobe InDesign.
//-------------------------------------------------------------------------
//-- Purpose: To delete all the master pages that are not applied to a
//-- a document page or a Master Page that is applied to a document.
//-------------------------------------------------------------------------
//-- Arguments: docRef: the document to examine.
//-------------------------------------------------------------------------
//-- Calls: addMasterName() an internal recursive function to generate
//-- an associative array of master page names that are in use.
//-------------------------------------------------------------------------
//-- Returns: nohting.
//-------------------------------------------------------------------------
//-- Sample Use:
//~ xUnusedMasters( app.documents[0] ) ;
//-- OR:
//~ xUnusedMasters( returnDocumentReference ( pageOfWindow () ) ) ;
//-------------------------------------------------------------------------
//-- Notes: Preserves the entire hierachy of all the master pages that
//-- are based on other master pages.
//-------------------------------------------------------------------------
//-- Written: 2009.10.22 by Jon S. Winters of electronic publishing support
//-- eps@electronicpublishingsupport.com
//-------------------------------------------------------------------------

//-- Create an object to record the names of the master pages to keep.
var mpNames = new Object () ;

//-- get a reference to all the pages of the passed document.
var allPages = docRef.pages ;
//-- loop through each page and add to the list of
for ( var pIndex = allPages.length - 1 ; pIndex >= 0 ; pIndex-- ) {
//-- Add the name of the master page applied to this page
//-- to the list of master pages to keep. Using and
//-- Associative Array allows the list to only contain
//-- a single instance if a single master page is used
//-- more than once in the document.
mpNames = addMasterName ( mpNames , allPages[pIndex] ) ;
}
//-- At this point the mpNames object has a property for the
//-- name of every master page that needs to be kept.

//-- Get a reference to all the master pages.
//-- NOTE: the '[None]' master page will not show up in this.
var allMasterPages = docRef.masterSpreads ;
//-- Loop through all the master pages.
for ( var mpIndex = allMasterPages.length - 1 ; mpIndex >= 0 ; mpIndex-- ) {
//-- compare this against the database of master pages to keep
if ( ! mpNames[allMasterPages[mpIndex].name] ) {
//-- If the associative array dosn't have that master page name
//-- then delete the master page.
allMasterPages[mpIndex].remove() ;
}
}
return ; //-- nothing, just return
//-- Below is an internal function to keep the main function generic
function addMasterName ( MPDB , pageRef ) {
//---------------------------------------------------------------------
//-- A D D M A S T E R N A M E
//---------------------------------------------------------------------
//-- Generic: Yes for Adobe InDesign
//---------------------------------------------------------------------
//-- Purpose: A recursive function that adds the name of the master
//-- page applied to the passed pageRef to the passed
//-- associative array MPDB
//---------------------------------------------------------------------
//-- Arguements: 2
//-- MPDB: An Associative Array with a value for each Master Page
//-- used.
//-- pageRef: The page or master page to get and add the master
//-- page name
//---------------------------------------------------------------------
//-- Calls: itself. This is a recursive function.
//---------------------------------------------------------------------
//-- Returns: The associative array MPDB with any new master page
//-- names from the passed pageRef
//---------------------------------------------------------------------
//-- Written entirely from scratch to replace a flawed version.
//-- Written 2009.10.22 by Jon S. Winters
//-- eps@electronicpublishingsupport.com
//---------------------------------------------------------------------

//-- Because the function is recursive, check to see if it was sent
//-- a reference to a non-existent master page. If so, return
//-- to the caller the same object it was sent.
if ( pageRef == null ) { return MPDB ; }

//-- Check to see if the passed pageRef is a page.
//-- If it is a page don't add page name.
//-- But if pageRef is master page then add its name.
if ( pageRef.constructor.name == 'MasterSpread' ) {
MPDB[pageRef.name] = true ;
}
//-- Call the function recursively, but send the appliedMaster
//-- of the passed pageRef and return the value.
return addMasterName ( MPDB , pageRef.appliedMaster ) ;
}//-- end of internal function
}//-- end of xUnusedMasters
//

No comments:

Post a Comment