Last active
December 11, 2018 18:21
-
-
Save olehmelnyk/57ef6cc65801329c22ec3c91b53cfe01 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#target indesign; | |
#targetengine 'myCustomGetFileNameHandler'; | |
if (app.activeDocument && app.activeDocument.name) { | |
var underscoreIndex = app.activeDocument.name.indexOf("_"); | |
if (underscoreIndex !== -1) { | |
var fileNameVariable = app.activeDocument.name.slice(0, ++underscoreIndex); // get XXXXX_ from XXXXX_Name1_Name2_Name3.indd file name | |
function grepClearSearchFields() { | |
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing; // clear settings | |
} | |
function grepIncludeOptions() { | |
app.findChangeGrepOptions.includeLockedLayersForFind = false; // search in Locked Layers | |
app.findChangeGrepOptions.includeLockedStoriesForFind = false; // search in Locked Stories | |
app.findChangeGrepOptions.includeHiddenLayers = false; // search in HiddenLayers | |
app.findChangeGrepOptions.includeMasterPages = false; // search in Master Pages | |
app.findChangeGrepOptions.includeFootnotes = true; // search in Footnotes | |
} | |
function grep(findWhat, changeTo) { | |
grepClearSearchFields(); // clear before we start | |
grepIncludeOptions(); // load "where to search" options | |
app.findGrepPreferences.findWhat = findWhat; | |
app.changeGrepPreferences.changeTo = changeTo; | |
var foundInstances = app.activeDocument.findGrep(); | |
if (foundInstances.length) { | |
for(i = 0; i < foundInstances.length; i++) { | |
var foundContents = foundInstances[i].contents; | |
if (foundContents !== changeTo && | |
confirm("Do you want to replace " + foundContents + " with " + fileNameVariable, false, "Replace?") | |
) { | |
app.findGrepPreferences.findWhat = foundContents; | |
app.changeGrepPreferences.changeTo = changeTo; | |
app.activeDocument.changeGrep(); | |
} | |
} | |
} | |
grepClearSearchFields(); // clear once we done | |
} // end of GREP find / change function | |
grep("[\\l\\u]+\\d+_", fileNameVariable); | |
} else { | |
alert("Wrong file name\n" + app.activeDocument.name, "Error", 1); | |
} | |
} else { | |
alert("No documents opened", "Error", 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment