Created
February 18, 2013 22:43
-
-
Save nukleas/4981456 to your computer and use it in GitHub Desktop.
A simple find/change that uses a tab-delimited text file as input for Indesign/InCopy
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
// Simple Find-Change with Range | |
function myDisplayDialog() { | |
"use strict"; | |
var findChangeTarget, dialogWindow, diaColRows, targetRangeButtons; | |
dialogWindow = app.dialogs.add({name: "Find/Change"}); | |
diaColRows = dialogWindow.dialogColumns.add().dialogRows.add(); // Just to shorten the line so it reads easier | |
diaColRows.staticTexts.add({staticLabel: "Search Range:"}); | |
targetRangeButtons = diaColRows.radiobuttonGroups.add(); | |
targetRangeButtons.radiobuttonControls.add({staticLabel: "Document", checkedState: true}); | |
targetRangeButtons.radiobuttonControls.add({staticLabel: "Selected Story"}); | |
// If you have a selection, target that. | |
if (app.selection[0].contents !== "") { | |
targetRangeButtons.radiobuttonControls.add({staticLabel: "Selection", checkedState: true}); | |
} | |
if (dialogWindow.show()) { | |
switch (targetRangeButtons.selectedButton) { | |
case 0: | |
findChangeTarget = app.documents[0]; | |
break; | |
case 1: | |
findChangeTarget = app.selection[0].parentStory; | |
break; | |
case 2: | |
findChangeTarget = app.selection[0]; | |
break; | |
} | |
dialogWindow.destroy(); | |
return findChangeTarget; | |
} | |
dialogWindow.destroy(); | |
} | |
function doFindChange(find_text, change_text, target) { | |
"use strict"; | |
app.findTextPreferences.findWhat = find_text; | |
// Setting whole word to true. You want this most of the time, I'd think, but feel free to change | |
app.findTextPreferences.findWhat.wholeWord = true; | |
app.changeTextPreferences.changeTo = change_text; | |
// Perform find/change | |
target.changeText(); | |
// Write a line to console for debugging | |
$.writeln("Changed " + find_text + " to " + change_text); | |
// Reset Find/Change | |
app.changeTextPreferences = NothingEnum.nothing; | |
app.findTextPreferences = NothingEnum.nothing; | |
} | |
function openFile() { | |
"use strict"; | |
var filePath, open_file; | |
/* | |
Instead of a hard-coded path, I decided to just have a dialog. | |
This is so you don't have to worry about changing the code too much. | |
*/ | |
filePath = File.openDialog("Choose the file containing your find/change list"); | |
if (filePath && filePath.open("r", undefined, undefined)) { | |
open_file = new File(filePath); | |
open_file.open("r", undefined, undefined); | |
return open_file; | |
} | |
return false; | |
} | |
function parseFile(open_file) { | |
"use strict"; | |
var line, findChangeArray = []; | |
do { | |
line = open_file.readln().split('\t'); | |
if (line.length > 1) { | |
findChangeArray.push([line[0], line[1]]); | |
} | |
} while (open_file.eof === false); | |
return findChangeArray; | |
} | |
function main() { | |
"use strict"; | |
var open_file, findChangeArray, i, target; | |
app.changeTextPreferences = NothingEnum.nothing; | |
app.findTextPreferences = NothingEnum.nothing; | |
open_file = openFile(); | |
if (open_file) { | |
target = myDisplayDialog(); | |
findChangeArray = parseFile(open_file); | |
for (i = 0; i < findChangeArray.length; i += 1) { | |
doFindChange(findChangeArray[i][0], findChangeArray[i][1], target); | |
} | |
} | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello Nukleas, thank you very much for the script, it's incredibly useful.
I wanted to ask if you can add an option to make each item in the list of search/replace only run once, ie when it finds the first match and replaces stop and go to the next item.
It would be a very useful option.
Thank you very much for your time.
Xesc.