Copy link to clipboard
Copied
Hi All,
Is it possible to use findGrep and findText together?
I need to find the words from TXT file using grep function and then i need to pass that value to findtext.
Tried Coding:
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 myGrepfound=mydoc.findGrep();
//~ alert(myGrepfound.contents)
///sending varable to findText
app.findTextPreferences = app.changeTextPreferences = null;
app.findChangeTextOptions.caseSensitive = false;
app.findChangeTextOptions.wholeWord = true;
app.findTextPreferences.findWhat=myGrepfound;
var myfound=mydoc.findText();
for(i=0;i<myfound.length;i++){
if(myfound.words.length==x.length){
app.changeTextPreferences.changeTo = myColumn1+"/"+myColumn2;
myfound.changeText();
}
app.findTextPreferences = null; app.changeTextPreferences = 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);
}
}
Thanks,
K
Copy link to clipboard
Copied
Hello,
I don't understand why you want to use findText and findGrep to search the same text (myColumn1).
findText will find more results without "(?!/)" (and replace more).
findGrep result is an array, like findText result.
You can test with this, commenting/uncommenting the 2 parts :
var mydoc=app.documents[0];
myfndchange();
function myfndchange(){
// no need for "isSupportedTXT"
var myTextFile = File.openDialog("Select the TXT file to Proceed", "*.txt",true);
if(myTextFile != null) { FilePath = decodeURI(myTextFile); }
else {return false;}
var datafile = new File(FilePath);
datafile.open('r') ;
app.findGrepPreferences = app.findGrepPreferences = null;
app.findTextPreferences = app.changeTextPreferences = null;
app.findChangeTextOptions.caseSensitive = false;
app.findChangeTextOptions.wholeWord = true;
while (!datafile.eof) {
var strLineIn = datafile.readln();
var colorArray = strLineIn.split("\t");
if (colorArray.length > 1) {
var myColumn1 = colorArray[0];
var myColumn2 = colorArray[1];
var flag = 0;
app.findGrepPreferences.findWhat = myColumn1 + "(?!/)";
var myGrepfound = mydoc.findGrep();
//*
// those 2 lines do the same as below
app.changeGrepPreferences.changeTo = myColumn1+"/"+myColumn2;
mydoc.changeGrep();
//*/
/*
// sending variable to findText
var x = myColumn1.split(" ");
for (var f = 0; f < myGrepfound.length; f++) {
app.findTextPreferences.findWhat = myGrepfound
.contents; var myfound = mydoc.findText();
for(var i=0; i < myfound.length; i++) {
if(myfound.words.length == x.length) {
app.changeTextPreferences.changeTo = myColumn1+"/"+myColumn2;
myfound.changeText();
}
}
}
*/
}
}
}
Perhaps there's another pattern. This text is always at the beginning/end of the line ?
Or you need to change text in a special order, using Grep and "(?!/)".
Swo.
Copy link to clipboard
Copied
Dear Wosven,
I'm searching a syntax for the script to find and replace the exact text and also starts with, ends with options given by user. could you guide me to finish my task.
Thanks,
Jo
Copy link to clipboard
Copied
Hi,
You can't use both Find Grep() and Find Text() at a time, you can prefer Fing Grep() and Sorry! for late reply that i have founded the solution for my query asked in your thread.
-Jothi