Skip to main content
tpk1982
Legend
November 8, 2016
Answered

Help needed for Find and Replace

  • November 8, 2016
  • 1 reply
  • 2088 views

Hi,

I am trying to find and replace the words from .txt file to indesign contents. If i have repeated words in .txt file the indesign also did for 3 times. The below image illustrates much better. Sorry i am improving my english to convey the requirement better. 

Script Tried:

myfndchange();

function myfndchange(){

var myFolder = File.openDialog("Select the TXT file to Proceed",isSupportedTXT,true);

if(myFolder!=null){FilePath=decodeURI(myFolder);}   

if(myFolder==null){return;}

var datafile = new File(FilePath);

datafile.open('r') ;

while (!datafile.eof){

    strLineIn = datafile.readln();

    colorArray = strLineIn.split("\t");

    var myColumn1=colorArray[0];

    var myColumn2=colorArray[1];

    var flag=0;

    x=myColumn1.split(" ");

    app.findTextPreferences = app.changeTextPreferences = null;

    app.findChangeTextOptions.includeLockedLayersForFind = true;

    app.findChangeTextOptions.includeLockedStoriesForFind = true;

    app.findChangeTextOptions.includeHiddenLayers = false;

    app.findChangeTextOptions.includeMasterPages = true;

    app.findChangeTextOptions.includeFootnotes = true;

    app.findChangeTextOptions.caseSensitive = false;

    app.findChangeTextOptions.wholeWord = true;

    mydoc=app.documents[0];

    app.findTextPreferences = null;     app.changeTextPreferences = null; 

    app.findTextPreferences.findWhat=myColumn1;

    var myfound=mydoc.findText();

    for(i=0;i<myfound.length;i++){

        if(myfound.words.length===x.length){

            app.changeTextPreferences.changeTo  = myColumn1+"/"+myColumn2;

            myfound.changeText()

            }

        }

    }

}

function isSupportedTXT(file) {

try {

if (file instanceof Folder)

return true;

else

return file.name.match(/\.txt$/i) != null;

} catch (e) {

alert("Error in isSupported method: " + e);

}

}

Thanks,

K

This topic has been closed for replies.
Correct answer Jump_Over

Hello Obi,

Any suggestions please


I suggest

(keeping idea read - split - unique - change):

Array.prototype.unique = function() {

    var res = [], k, i, loop;

    loop: for (k = 0; k < this.length; k++) {

                for (i = 0; i < res.length; i++)

                    if (this == res) continue loop;

                res.push(this);  

                }

    return res;

    }

//

var

    myDoc =app.documents[0],

    myFile = File.openDialog("Select a TXT file to proceed","*.txt",false),

    mySource = [], cLine, cCol1, cCol2;

//  

if (!myFile) exit();

///

app.findTextPreferences = app.changeTextPreferences = null;

//  

with (app.findChangeTextOptions){

    includeLockedLayersForFind = true;

    includeLockedStoriesForFind = true;

    includeHiddenLayers = false;

    includeMasterPages = true;

    includeFootnotes = true;

    caseSensitive = false;

    wholeWord = true;

    }

//

myFile.open('r');

do

    mySource.push(myFile.readln());

    while (!myFile.eof);

myFile.close();

//

mySource = mySource.unique();

//

while (cLine = mySource.pop()) {

    cCol1 = cLine.split("\t")[0];

    cCol2 = cLine.split("\t")[1];

    app.findTextPreferences.findWhat = cCol1;

    app.changeTextPreferences.changeTo = cCol1 + "/" + cCol2;

    myDoc.changeText();

    }

Jarek

1 reply

tpk1982
tpk1982Author
Legend
November 8, 2016

Hello Experts,

Any quick suggestions is appreciated. I hope somebody help to sort out the issue.

Regards,

Karthi

Obi-wan Kenobi
Legend
November 8, 2016

Hi Karthi,

I think I've understood what you mean!! 

Try this:

mydoc=app.documents[0]; 

myfndchange(); 

function myfndchange(){ 

var myFolder = File.openDialog("Select the TXT file to Proceed",isSupportedTXT,true); 

if(myFolder!=null){FilePath=decodeURI(myFolder);}     

if(myFolder==null){return;} 

var datafile = new File(FilePath); 

datafile.open('r') ; 

 

 

while (!datafile.eof){ 

    strLineIn = datafile.readln(); 

    colorArray = strLineIn.split("\t"); 

    var myColumn1=colorArray[0]; 

    var myColumn2=colorArray[1]; 

    var flag=0; 

    x=myColumn1.split(" "); 

    app.findGrepPreferences = app.findGrepPreferences = null;  

    app.findGrepPreferences.findWhat = myColumn1 + "(?!/)"; 

    var myfound=mydoc.findGrep(); 

    for(i=0;i<myfound.length;i++){ 

        if(myfound.words.length===x.length){ 

            app.changeGrepPreferences.changeTo  = myColumn1+"/"+myColumn2; 

            myfound.changeGrep() 

            } 

        } 

    app.findGrepPreferences = app.findGrepPreferences = null;  

    } 

 

 

function isSupportedTXT(file) { 

try { 

if (file instanceof Folder) 

return true; 

else 

return file.name.match(/\.txt$/i) != null; 

} catch (e) { 

alert("Error in isSupported method: " + e); 

(^/)