forked from graysitory/Novelty
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Master_2_Style.jsx
executable file
·140 lines (119 loc) · 4.33 KB
/
Master_2_Style.jsx
1
/* Master_2_Style.jsx Bruno Herfst 2010 An InDesign CS5 script to apply the selected master-page to any page that contains the searchresult. Tested in CS6 */#target indesign;var the_document = app.documents.item(0);// Create a list of paragraph stylesvar list_of_paragraph_styles = the_document.paragraphStyles.everyItem().name;list_of_paragraph_styles.unshift("--ANY STYLE--");// Create a list of master pagesvar list_of_master_pages = the_document.masterSpreads.everyItem().name;// Make the dialog box for selecting the paragraph stylesvar the_dialog = app.dialogs.add({name:"add master to pages containing style"});with(the_dialog.dialogColumns.add()){ with(dialogRows.add()){ staticTexts.add({staticLabel:"Find:"}); var find_paragraph = dropdowns.add({stringList:list_of_paragraph_styles, selectedIndex:0}); } with(borderPanels.add()){ // A decorative checkbox :) var myGREPCheckbox = checkboxControls.add({staticLabel:"Custom GREP:", checkedState:true}); var myGREPField = textEditboxes.add({editContents:"^.+"}); } with(dialogRows.add()){ staticTexts.add({staticLabel:"Apply "}); var change_master = dropdowns.add({stringList:list_of_master_pages, selectedIndex:0}); staticTexts.add({staticLabel:" to "}); var master_offset = dropdowns.add({stringList:["Page Before", "Page", "Page After"], selectedIndex:1}); } with(borderPanels.add()){ var myRMCheckbox = checkboxControls.add({staticLabel:"Replace masters first", checkedState:true}); var replace_master = dropdowns.add({stringList:list_of_master_pages, selectedIndex:0}); }}var myResult = the_dialog.show();if(myResult == true){ // Define variables if (find_paragraph.selectedIndex == 0) { var find_paragraph = false; } else { var find_paragraph = the_document.paragraphStyles.item(find_paragraph.selectedIndex-1); } var pageOfset = 0; if(master_offset.selectedIndex != 1){ if(master_offset.selectedIndex > 1){ // Page After pageOfset = 1; } else { // Page Before; pageOfset = -1; } } var change_master = the_document.masterSpreads.item(change_master.selectedIndex); var replace_master = the_document.masterSpreads.item(replace_master.selectedIndex); var myRM = myRMCheckbox.checkedState; var myGREP = myGREPCheckbox.checkedState; if(myGREP == true && myGREPField.editContents != "" && myGREPField.editContents != "^") { //Find paragraph ^ is buggy in CS5 it will only find the first one not the next var find_what = myGREPField.editContents; } else { var find_what = "^.+"; } if (myRM == true) { // Find and replace the pages for(var myCounter = 0; myCounter < the_document.pages.length; myCounter++){ myPage = the_document.pages.item(myCounter); if (myPage.appliedMaster == change_master){ myPage.appliedMaster = replace_master; } } } // Set find grep preferences to find all paragraphs with the selected paragraph style app.findChangeGrepOptions.includeFootnotes = false; app.findChangeGrepOptions.includeHiddenLayers = false; app.findChangeGrepOptions.includeLockedLayersForFind = false; app.findChangeGrepOptions.includeLockedStoriesForFind = false; app.findChangeGrepOptions.includeMasterPages = false; app.findGrepPreferences = NothingEnum.nothing; if(find_paragraph == false){ app.findGrepPreferences.appliedParagraphStyle = NothingEnum.nothing; } else { app.findGrepPreferences.appliedParagraphStyle = find_paragraph; } app.findGrepPreferences.findWhat = find_what; //Search the current story var found_paragraphs = the_document.findGrep(); var myCounter = 0; var myMessage = 0; var myCounterCounter = 0; try { if(found_paragraphs.length > 0){ do { // Create an object reference to the found paragraph and the next // for use in CS4 change parentPage to parent var myDocOfset = found_paragraphs[myCounter].insertionPoints[0].parentTextFrames[0].parentPage.documentOffset; var myPage = the_document.pages[myDocOfset + pageOfset]; // make sure page exist if(myPage.isValid){ myPage.appliedMaster = change_master; } else { myCounterCounter += 1; } myCounter++; } while (myCounter < found_paragraphs.length); } else { myMessage = "Couldn’t find anything!"; } } catch(err) { myMessage = err; } if(myMessage == 0){ var myMessage = "Done placing "+(myCounter-myCounterCounter)+" master pages!"; } alert(myMessage);} else { exit();}