Skip to content

Instantly share code, notes, and snippets.

@olehmelnyk
Last active December 11, 2018 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olehmelnyk/57ef6cc65801329c22ec3c91b53cfe01 to your computer and use it in GitHub Desktop.
Save olehmelnyk/57ef6cc65801329c22ec3c91b53cfe01 to your computer and use it in GitHub Desktop.
#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