//FindChangeByList_mod2.jsx
//An InDesign CS3 JavaScript
//
// -----------------------------------------
// A variation of FindChangeByList.jsx
// New function 'chooseMyOwnList()' added by Martin Fischer
// Saves statistics in 'FindChangeResults.txt', either in the document's path or at the desktop (if document has not been saved yet)
// -----------------------------------------
//
//Loads a series of tab-delimited strings from a text file, then performs a series
//of find/change operations based on the strings read from the file.
//
//The data file is tab-delimited, with carriage returns separating records.
//
//The format of each record in the file is:
//findType<tab>findProperties<tab>changeProperties<tab>findChangeOptions<tab>description
//
//Where:
//<tab> is a tab character
//findType is "text", "grep", or "glyph" (this sets the type of find/change operation to use).
//findProperties is a properties record (as text) of the find preferences.
//changeProperties is a properties record (as text) of the change preferences.
//findChangeOptions is a properties record (as text) of the find/change options.
//description is a description of the find/change operation
//
//Very simple example:
//text {findWhat:"--"} {changeTo:"^_"} {includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false} Find all double dashes and replace with an em dash.
//
//More complex example:
//text {findWhat:"^9^9.^9^9"} {appliedCharacterStyle:"price"} {include footnotes:true, include master pages:true, include hidden layers:true, whole word:false} Find $10.00 to $99.99 and apply the character style "price".
//
//All InDesign search metacharacters are allowed in the "findWhat" and "changeTo" properties for findTextPreferences and changeTextPreferences.
//
//If you enter backslashes in the findWhat property of the findGrepPreferences object, they must be "escaped"
//as shown in the example below:
//
//{findWhat:"\\s+"}
//
//For more on InDesign scripting, go to http://www.adobe.com/products/indesign/scripting.html
//or visit the InDesign Scripting User to User forum at http://www.adobeforums.com
//
var myData = new Array();
main();
function main(){
var myObject;
var myCheckSelection = false;
if(app.documents.length > 0){
if(app.selection.length > 0){
switch(app.selection[0].constructor.name){
case "InsertionPoint":
case "Character":
case "Word":
case "TextStyleRange":
case "Line":
case "Paragraph":
case "TextColumn":
case "Text":
case "TextFrame":
myObject = myDisplayDialog();
myCheckSelection = myObject[1];
myObject = myObject[0];
if(myObject != "None"){
myFindChangeByList(myObject, myCheckSelection);
}
break;
default:
myFindChangeByList(app.documents.item(0), false);
}
}
else{
//Nothing was selected, so simply search the document.
myFindChangeByList(app.documents.item(0), false);
}
exportTXT( myData.join( '\r'), 'FindChangeResults.txt' );
}
else{
alert("No documents are open. Please open a document and try again.");
}
}
function myDisplayDialog(){
var myObject;
var myCheckSelection = false;
var myDialog = app.dialogs.add({name:"FindChangeByList"});
with(myDialog.dialogColumns.add()){
with(dialogRows.add()){
with(dialogColumns.add()){
staticTexts.add({staticLabel:"Search Range:"});
}
var myRangeButtons = radiobuttonGroups.add();
with(myRangeButtons){
radiobuttonControls.add({staticLabel:"Selection", checkedState:true});
radiobuttonControls.add({staticLabel:"Selected Story"});
radiobuttonControls.add({staticLabel:"Document"});
}
}
}
var myResult = myDialog.show();
if(myResult == true){
switch(myRangeButtons.selectedButton){
case 0:
myObject = app.selection[0];
myCheckSelection = true;
break;
case 1:
myObject = app.selection[0].parentStory;
myCheckSelection = false;
break;
case 2:
myObject = app.documents.item(0);
myCheckSelection = false;
break;
}
}
else{
myObject = "None";
}
myDialog.destroy();
return [myObject, myCheckSelection];
}
function myFindChangeByList(myObject, myCheckSelection){
var myScriptFileName, myFindChangeFile, myFindChangeFileName, myScriptFile, myResult;
var myFindChangeArray, myFindPreferences, myChangePreferences, myFindLimit;
var myStartCharacter, myEndCharacter;
var myFindChangeFile = myFindFile("/FindChangeSupport/FindChangeList.txt")
if(myFindChangeFile != null){
// Because the selection will change as we add/remove characters,
// we'll need to reset the selection after each pass if we are
// checking the selection. We'll get the index of the first character
// in the selection (relative to the start of its parent story) and
// the index of the last character in the selection (relative to the
// *end* of the story, and we'll use them later in the script to
// keep the ends of the selection in place.
if(myCheckSelection == true){
var myStart = myObject.characters.item(0).index;
var myEnd = myObject.characters.item(-1).index;
var myStory = myObject.parentStory;
var myStoryEnd = myStory.characters.item(-1).index;
myEnd = (myStoryEnd - myEnd)+1;
}
myFindChangeFile = File(myFindChangeFile);
var myResult = myFindChangeFile.open("r", undefined, undefined);
if(myResult == true){
//Loop through the find/change operations.
do{
myLine = myFindChangeFile.readln();
//Ignore comment lines and blank lines.
if((myLine.substring(0,4)=="text")||(myLine.substring(0,4)=="grep")||(myLine.substring(0,5)=="glyph")){
myFindChangeArray = myLine.split("\t");
//The first field in the line is the findType string.
myFindType = myFindChangeArray[0];
//The second field in the line is the FindPreferences string.
myFindPreferences = myFindChangeArray[1];
//The second field in the line is the ChangePreferences string.
myChangePreferences = myFindChangeArray[2];
//The fourth field is the range--used only by text find/change.
myFindChangeOptions = myFindChangeArray[3];
switch(myFindType){
case "text":
myFindText(myObject, myFindPreferences, myChangePreferences, myFindChangeOptions);
break;
case "grep":
myFindGrep(myObject, myFindPreferences, myChangePreferences, myFindChangeOptions);
break;
case "glyph":
myFindGlyph(myObject, myFindPreferences, myChangePreferences, myFindChangeOptions);
break;
}
if(myCheckSelection == true){
myStartCharacter = myStory.characters.item(myStart);
myEndCharacter = myStory.characters.item(-myEnd);
myObject = myStory.texts.itemByRange(myStartCharacter, myEndCharacter);
app.select (myObject);
}
}
} while(myFindChangeFile.eof == false);
myFindChangeFile.close();
}
}
}
function myFindText(myObject, myFindPreferences, myChangePreferences, myFindChangeOptions){
//Reset the find/change preferences before each search.
app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences = NothingEnum.nothing;
var myString = "app.findTextPreferences.properties = "+ myFindPreferences + ";";
myString += "app.changeTextPreferences.properties = " + myChangePreferences + ";";
myString += "app.findChangeTextOptions.properties = " + myFindChangeOptions + ";";
app.doScript(myString, ScriptLanguage.javascript);
myFoundItems = myObject.changeText();
if (myFoundItems.length > 0)
myData.push( myFoundItems.length + ':\t' + myString);
//Reset the find/change preferences after each search.
//app.changeTextPreferences = NothingEnum.nothing;
//app.findTextPreferences = NothingEnum.nothing;
}
function myFindGrep(myObject, myFindPreferences, myChangePreferences, myFindChangeOptions){
//Reset the find/change grep preferences before each search.
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences = NothingEnum.nothing;
var myString = "app.findGrepPreferences.properties = "+ myFindPreferences + ";";
myString += "app.changeGrepPreferences.properties = " + myChangePreferences + ";";
myString += "app.findChangeGrepOptions.properties = " + myFindChangeOptions + ";";
app.doScript(myString, ScriptLanguage.javascript);
var myFoundItems = myObject.changeGrep();
if (myFoundItems.length > 0)
myData.push( myFoundItems.length + ':\t' + myString);
//Reset the find/change grep preferences after each search.
//app.changeGrepPreferences = NothingEnum.nothing;
//app.findGrepPreferences = NothingEnum.nothing;
}
function myFindGlyph(myObject, myFindPreferences, myChangePreferences, myFindChangeOptions){
//Reset the find/change glyph preferences before each search.
app.changeGlyphPreferences = NothingEnum.nothing;
app.findGlyphPreferences = NothingEnum.nothing;
var myString = "app.findGlyphPreferences.properties = "+ myFindPreferences + ";";
myString += "app.changeGlyphPreferences.properties = " + myChangePreferences + ";";
myString += "app.findChangeGlyphOptions.properties = " + myFindChangeOptions + ";";
app.doScript(myString, ScriptLanguage.javascript);
var myFoundItems = myObject.changeGlyph();
if (myFoundItems.length > 0)
myData.push( myFoundItems.length + ':\t' + myString);
//Reset the find/change glyph preferences after each search.
//app.changeGlyphPreferences = NothingEnum.nothing;
//app.findGlyphPreferences = NothingEnum.nothing;
}
function myFindFile(myFilePath){
var myScriptFile = myGetScriptPath();
var myScriptFile = File(myScriptFile);
var myScriptFolder = myScriptFile.path;
myFilePath = myScriptFolder + myFilePath;
if(File(myFilePath).exists == false){
//Display a dialog to choose own list
var aResult = chooseMyOwnList( myScriptFile.path + '/FindChangeSupport');
if ( aResult != 'None' )
myFilePath = File( aResult );
else
myFilePath = File.openDialog("Choose the file containing your find/change list");
}
return myFilePath;
}
function myGetScriptPath(){
try{
myFile = app.activeScript;
}
catch(myError){
myFile = myError.fileName;
}
return myFile;
}
function chooseMyOwnList( aPath )
{
var myChoosedList = "None";
var myListFiles = Folder( aPath ).getFiles( '*.txt' )
var myLists = new Array;
for ( var f = 0; f < myListFiles.length; f++ )
myLists.push( myListFiles[f].name );
if (myLists.length > 0 ) {
var myDialog = app.dialogs.add({name:"FindChangeByList"});
with(myDialog.dialogColumns.add()){
with(dialogRows.add()){
with(dialogColumns.add()){
staticTexts.add({staticLabel:"Liste:"});
}
with(dialogColumns.add()){
var myListsDropdown = dropdowns.add({stringList:myLists, selectedIndex:0});
}
}
}
var myResult = myDialog.show();
if(myResult == true)
var myChoosedList = aPath + '/' + myLists[myListsDropdown.selectedIndex];
myDialog.destroy();
}
return myChoosedList;
}
function exportTXT( myData, myFileName ){
var myCreator = "R*ch";
var myType = "UTF-8";
var myPath = ( app.activeDocument.saved == true)
? app.activeDocument.filePath
: '~/Desktop';
var myFile = new File( myPath + '/' + myFileName);
myFile.open('w', myType, myCreator);
myFile.write(myData);
myFile.close();
}