Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Indesign script that generates a text file with information on which pages each of the master pages have been applied to.
// Version 2.1.
// https://gist.github.com/joonaspaakko/efc6eb11759965d54b20
// *************
// Changelog:
// *************
// v.2.1.
// - Tidied up the code just a tiny bit. Mostly just made it slightly more readable.
// - Added colors to the text document to make it easier to read.
// - Added a variable "joinRanges" at the top of the file that joins number rangers if set to true.
// - This makes the output way cleaner.
// - For example pages "1,2,3,4,5,10,11,12,13,14,26,30,31" will be output as "1-5, 10-14, 26, 30-31"
// - FYI: Indesign supports number ranges like this in all inputs that take page ranges, like for example the "Apply master to pages" dialog.
// - Added a variable "useDocName" at the top of the file. Adds document name to the text file if set to true.
// - Text file is now saved in the folder containing the script file.
// - Tested in Mac - Indesign CC 2018
//
// v.2.
// - Now prints out the page information about every single page in the document.
// - Removed the dialog, as it's useless now.
// - Due to the increased number of text printed out at once, it is now printed into a rtf file.
// - Tested in Mac - Indesign CC 2014
//
// v.1.
// - The first version of the script gives you the pages of a single master = https://gist.github.com/joonaspaakko/efc6eb11759965d54b20/02a53c865ec1f228b1646d4dbe558c5ccfa94040
var joinRanges = true;
var useDocName = false;
var masters_array = {};
var subMaster_array = {};
var docMasters = null;
if ( app.documents.length > 0 ) init();
function init() {
var doc = app.activeDocument;
// List of master pages
docMasters = doc.masterSpreads.everyItem().name;
// Creates same amount of arrays as there are masters ( [None] is also included ).
// The arrays are populated later on.
for (i = 0; i <= docMasters.length; i++) {
masters_array[i] = [];
}
checkPages( doc );
// Text file path
// Points to the folder where the script sits...
var rtf_file = new File( (new File($.fileName)).parent + "/Check Master Page Numbers"+ (useDocName ? ("( "+ doc.name +" )") : '') +".rtf" );
print_Header( rtf_file );
print_Body( rtf_file, doc );
print_Footer( rtf_file );
} // init()
function checkPages( doc ) {
// Loop through all pages...
var docPages = doc.pages;
var pagesLength = docPages.length;
for ( var i = 0; i < pagesLength; i++ ) {
// Current page
var active_page = docPages[i];
// Name of the master in current page
// If master is not applied, use '[None]'
var active_pageMaster = active_page.appliedMaster ? active_page.appliedMaster.name : '[None]';
// Index of the master in current page
// Index is 0 if it's '[None]'
var active_pageMasterIndex = active_page.appliedMaster ? active_page.appliedMaster.index + 1 : 0;
// Push page numbers to their respective arrays...
masters_array[ active_pageMasterIndex ].push(' ' + (i + 1));
};
}
function print_Header( rtf_file ) {
rtf_file.open("w");
// First line in the text file
rtf_file.writeln('{\\rtf1\\ansi\\deff0\\line{\\colortbl;\\red0\\green0\\blue0;\\red255\\green0\\blue0;\\red153\\green153\\blue153;\\red31\\green165\\blue44;}\\i\\cf2 If you ran the script with this text file open, you may need to reopen it to see the changes.\\cf1 \\line\\line Sub masters are shown in parenthesis. \\i0 \\line\\line\\line');
}
function print_Body( rtf_file, doc ) {
// Loop through the masters yet again
// The point of this is to assing the correct heading to each line and their sub master info.
for (i = -1; i < docMasters.length; i++) {
var none = i === -1;
// Get the current line heading
var heading = none ? '[None]' : doc.masterSpreads[i].name;
// Check if current master has sub master
// '[None]' is never able to have sub masters, since it is not a master page.
var appliedMaster = none ? null : doc.masterSpreads[i].appliedMaster;
var appliedMasterIndex = !appliedMaster ? null : appliedMaster.index + 1;
var appliedMasterName = !appliedMaster ? null : appliedMaster.name;
var xerox = {};
// Find out which master is applied to current master
xerox.subMaster = appliedMaster ? ' ( Based on: \\b ' + appliedMasterName + ' \\b0 )' : '';
var currentMasterPages = joinRanges ? getRanges( masters_array[i + 1] ) : masters_array[i + 1];
var submasters = get_subMasters( doc );
// Prints pages in the current master or if it has not been applied to any page, it prints out the text string.
// In addition to the page numbers, it also prints the sub master of that specific master in parenthesis, if it has any.
xerox.pages = currentMasterPages == '' ? '\\cf3\\i Has not been applied to any page' + (submasters[i] ? '\\i directly, but it has been applied to other master(s)\\i0' : '') + '.\\i0\\cf1' : '\\cf4' + currentMasterPages + '\\cf1',
// If current master is applied to any other master, this prints out the name those masters.
xerox.parentMaster = (submasters[i] == null ? '' : '\\line\\b Sub master of: \\b0 \\cf3' + submasters[i]) + '\\cf1';
// Writes the text body...
rtf_file.writeln( '\\b ' + heading + '\\b0 ' + xerox.subMaster + '\\b:\\b0 ' + xerox.pages + xerox.parentMaster + '\\line\\line' );
}
}
function print_Footer( rtf_file ) {
// Writes text footer...
rtf_file.writeln('}');
rtf_file.close();
// Opens the text file
rtf_file.execute();
}
function get_subMasters( doc ) {
var submasters = {};
// One master can be applied to multiple other masters....
for ( index = 0; index < docMasters.length; index++ ) {
// Sub master
var appliedMaster = doc.masterSpreads[index].appliedMaster;
// Sub master index
var appliedMasterIndex = !appliedMaster ? null : appliedMaster.index;
// If current master has a sub master and it doesn't have its own array yet, create it.
if ( !submasters[appliedMasterIndex] ) submasters[appliedMasterIndex] = [];
// Push current master name to sub master array.
submasters[appliedMasterIndex].push(' ' + doc.masterSpreads[index].name);
}
return submasters;
}
function getRanges(array) {
var ranges = [], rstart, rend;
for (var i = 0; i < array.length; i++) {
rstart = array[i];
rend = rstart;
while (array[i + 1] - array[i] == 1) {
rend = array[i + 1].replace(' ','');
i++;
}
ranges.push(rstart == rend ? rstart+'' : rstart + '-' + rend);
}
return ranges;
}
@joonaspaakko

This comment has been minimized.

Copy link
Owner Author

commented Jul 17, 2019

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.