Copy link to clipboard
Copied
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
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)
Copy link to clipboard
Copied
Hello Experts,
Any quick suggestions is appreciated. I hope somebody help to sort out the issue.
Regards,
Karthi
Copy link to clipboard
Copied
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);
}
}
(^/)
Copy link to clipboard
Copied
Hi,
I suggest to read entire txt file --> split it by lines --> make an array unique --> use it as a base for find...replace procedure
Jarek
Copy link to clipboard
Copied
This version seems to be faster!
myDoc = app.documents[0];
myFindchange();
function myFindchange () {
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();
var myWords = datafile.read();
myWords = myWords.split("\n");
W = myWords.length;
datafile.close();
while (W--) {
myWord = myWords
.split("\t"); var myWord0 = myWord[0];
var myWord1 = myWord[1];
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = myWord0 + "(?!/)";
var myfound = myDoc.findGrep();
for(i = 0; i < myfound.length; i++){
app.changeGrepPreferences.changeTo = "$0/" + myWord1;
myfound.changeGrep()
}
}
app.findGrepPreferences = app.changeGrepPreferences = 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);
}
}
(^/)
Copy link to clipboard
Copied
Hi Obi,
Thanks for the quick reply. I expected your answer after i posted You did that..
Thanks Jump_Over for your suggestions...
Thread 4..This version seems to be faster!.. NOT WORKING>> IT ALMOST RUNNING ENDLESS
Regards,
K
Copy link to clipboard
Copied
Hi Obi,
I am facing with the code as you provided in thread 2.
See the below screen shot:
Please see the red marked line. It should not be updated. Also the replaced things also wrong.
It can be eliminated in Text search instead of Grep. Because it have
app.findChangeTextOptions.wholeWord = true;
Thanks,
K
Copy link to clipboard
Copied
Hello Obi,
Any suggestions please
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Hi Jarek,
Awesome.. it working as expected.. you are my time saver man.. for this reason i created a new thread..
FindGrep() and FindText() together
Thank you so much
Thanks Obi for the part of this thread.. your's suggestions also valuable and new learning for me
Regards,
K
Copy link to clipboard
Copied
Hello Jarek,
One small issue happened. See below:
The word "STARTING AT" used in "SALE STARTING AT", so it put the two words (10, 11) in "SALE STARTING AT"
Could you please suggest?
Thanks,
K
Copy link to clipboard
Copied
Karthi,
Would do you want?
SALE STARTING AT/10
SALE STARTING AT/10
STARTING AT/11
(^/)
Copy link to clipboard
Copied
Hi,
The devil is in the details - as always
I thinks it should go with Grep
alike:
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.findGrepPreferences = app.changeGrepPreferences = null;
//
with (app.findChangeGrepOptions){
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);
//
mySource = mySource.unique();
//
while (cLine = mySource.pop()) {
cCol1 = cLine.split("\t")[0];
cCol2 = cLine.split("\t")[1];
app.findGrepPreferences.findWhat = "(?i)^" + cCol1 + "$";
app.changeGrepPreferences.changeTo = cCol1 + "/" + cCol2;
myDoc.changeGrep();
}
Jarek
Copy link to clipboard
Copied
Hi Jarek & Obi,
Sorry if i confused anything. Yes i need as you said in thread 12.
@Jarek: I avoid using Grep search because it does not have case sensitive option. i tried from my end and it is working fine...
@Obi: Thanks again for your effort
The below script is working for me
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();
//
var flag=0;
while (cLine = mySource.pop()) {
cCol1 = cLine.split("\t")[0];
cCol2 = cLine.split("\t")[1];
x=cCol1.split(" ");
app.findTextPreferences.findWhat = cCol1;
var myfound=myDoc.findText(true);
for(i=0;i<myfound.length;i++){
xx=0;
for(j=0;j<x.length;j++){
xx=xx+myfound.words
.characters.length; }
if(myfound.words.length>1){xx=xx+myfound.words.length-1;}
if(xx===cCol1.length){
app.changeTextPreferences.changeTo = cCol1+"/"+cCol2;
try{myfound.changeText();}
catch(er){
if(flag==0){
alert(cCol1+"\n");
flag=1;
}
}
}
}
if(myfound.length==0){ alert(cCol1+"\n")}
}
}
Thanks,
K
Copy link to clipboard
Copied
Jarek,
I try to play the game with other text!
For that, First, I apply a condition, then I search on this condition!
My list is:
Obi-wan | 1 |
Master Obi-wan | 2 |
Obi-wan The Jedi | 3 |
Obi-wan Kenobi | 4 |
My text is:
I’m Obi-wan!
Master Obi-wan!
Obi-wan The Jedi!
Obi-wan Kenobi!
… and I would want to get this:
I’m Obi-wan/1!
Master Obi-wan/2!
Obi-wan The Jedi/3!
Obi-wan Kenobi/4!
But your modified script aborts before the second "while"! An idea? Thanks in advance!
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();
myFile.open('r');
do
mySource.push(myFile.readln());
while (!myFile.eof);
mySource = mySource.unique();
while (cLine = mySource.pop()) {
cCol1 = cLine.split("\t")[0];
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = cCol1;
app.changeGrepPreferences.appliedConditions = ["OBW"];
myDoc.changeGrep();
}
while (cLine = mySource.pop()) {
cCol1 = cLine.split("\t")[0];
cCol2 = cLine.split("\t")[1];
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = "(?<!.)" + cCol1 + "(?!.)";
app.findGrepPreferences.appliedConditions = ["OBW"];
app.changeGrepPreferences.changeTo = "$0/" + cCol2;
app.changeGrepPreferences.appliedConditions = [];
myDoc.changeGrep();
}
app.findGrepPreferences = app.changeGrepPreferences = null;
(^/)
Copy link to clipboard
Copied
Hi,
At the end of 1st 'while' mySource become an empty array ==> method pop() decrease it by 1 returning last element
Jarek
Copy link to clipboard
Copied
Jarek,
I saw that but I don't find the way to reinitiate "mySource'!
(^/)
Copy link to clipboard
Copied
OK!
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();
myFile.open('r');
do
mySource.push(myFile.readln());
while (!myFile.eof);
mySource1 = mySource.unique();
while (cLine = mySource1.pop()) {
cCol1 = cLine.split("\t")[0];
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = cCol1;
app.changeGrepPreferences.appliedConditions = ["OBW"];
myDoc.changeGrep();
}
mySource2 = mySource.unique();
while (cLine = mySource2.pop()) {
cCol1 = cLine.split("\t")[0];
cCol2 = cLine.split("\t")[1];
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = "(?<!.)" + cCol1 + "(?!.)";
app.findGrepPreferences.appliedConditions = ["OBW"];
app.changeGrepPreferences.changeTo = "$0/" + cCol2;
app.changeGrepPreferences.appliedConditions = [];
myDoc.changeGrep();
}
app.findGrepPreferences = app.changeGrepPreferences = null;
To finish, just need to insert the creation of the condition "OBW" and its deletion at the end!
(^/)
Copy link to clipboard
Copied
Done! Thanks Jarek, your code is coo!
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],
myCondition = myDoc.conditions.add ({name: "OBW"}),
myFile = File.openDialog("Select a TXT file to proceed","*.txt",false),
mySource = [], cLine, cCol1, cCol2;
if (!myFile) exit();
myFile.open('r');
do
mySource.push(myFile.readln());
while (!myFile.eof);
mySource1 = mySource.unique();
while (cLine = mySource1.pop()) {
cCol1 = cLine.split("\t")[0];
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = cCol1;
app.changeGrepPreferences.appliedConditions = [myCondition];
myDoc.changeGrep();
}
mySource2 = mySource.unique();
while (cLine = mySource2.pop()) {
cCol1 = cLine.split("\t")[0];
cCol2 = cLine.split("\t")[1];
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = "(?<!.)" + cCol1 + "(?!.)";
app.findGrepPreferences.appliedConditions = [myCondition];
app.changeGrepPreferences.changeTo = "$0/" + cCol2;
app.changeGrepPreferences.appliedConditions = [];
myDoc.changeGrep();
}
app.findGrepPreferences = app.changeGrepPreferences = null;
myCondition.remove();
(^/)
Copy link to clipboard
Copied
Hi,
Use "for" instead of while.
Copy link to clipboard
Copied
You mean that if I use a "for" loop, no need to reinitiate "mySource"?
(^/)
Copy link to clipboard
Copied
right
Copy link to clipboard
Copied
Thanks!
(^/)
Copy link to clipboard
Copied
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],
myCondition = myDoc.conditions.add ({name: "OBW"}),
myFile = File.openDialog("Select a TXT file to proceed","*.txt",false),
mySource = [], cCol1, cCol2;
if (!myFile) exit();
myFile.open('r');
do
mySource.push(myFile.readln());
while (!myFile.eof);
mySource = mySource.unique();
for (var c = 0; c < mySource.length; c++ ) {
cCol1 = mySource
.split("\t")[0]; app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = cCol1;
app.changeGrepPreferences.appliedConditions = [myCondition];
myDoc.changeGrep();
}
for (var c = 0; c < mySource.length; c++ ) {
cCol1 = mySource
.split("\t")[0]; cCol2 = mySource
.split("\t")[1]; app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = "(?<!.)" + cCol1 + "(?!.)";
app.findGrepPreferences.appliedConditions = [myCondition];
app.changeGrepPreferences.changeTo = "$0/" + cCol2;
app.changeGrepPreferences.appliedConditions = [];
myDoc.changeGrep();
}
app.findGrepPreferences = app.changeGrepPreferences = null;
myCondition.remove();
Need now to study well, understand and to be able to replicate all the niceties of your great code!
Thanks a lot for the learning!
(^/)
Copy link to clipboard
Copied
Karthi,
You wasn't not clear at all!
If I understand now well, the right list you could have could be this one:
Obi-wan 1
Obi-wan 2
Obi-wan 3
Luke 4
Han 5
Luke 6
… and the result you would want would be this one:
Obi-wan/1/2/3
Luke/4/6
Obi-wan/1/2/3
Han/5
Obi-wan/1/2/3
Darth Fourth
Luke/4/6 Fifth
Obi-wan/1/2/3
Right? [it's just for me to understand!]
Tests:
(^/)