Skip to main content
Hypons111
Participating Frequently
November 7, 2019
Question

Find Change script

  • November 7, 2019
  • 5 replies
  • 1341 views

I have a hundred title like this: G.N./d/d/d/r

exsample: G.N. 123

                 G.N. 234

                 G.N. 345. etc....

My job is assign a new number to these titles. I used the app.activeDocument.changeGrep();. But turns out its "Change All", they were all changed to the same number now. 
Is it possible to use script to get it done?

This topic has been closed for replies.

5 replies

Community Expert
November 11, 2019

Hi hypons111,

if the scope of findGrep() is the document I wonder if the order of found texts is always the one you like to see.

If the order is no issue, then you are good to go. Otherwise one would need a different approach where the order of found texts is the order you like to see, e.g. in a geometrical order from right to left and top to down on a given page. For that you have to detect the values of baseline and horizontalOffset maybe and do a sort according to that values. Or you could convert a copy of the found text to outlines and detect the bounding box values and sort accordingly. Also note the order of parentPage to get the order of pages right.

 

Regards,
Uwe Laubender

( ACP )

Community Expert
November 7, 2019

Hi hypons111,

so in effect are you saying that there is no logical way to compute the new number from the old contents?

Now that brings us to a totally different solution where you need to use a list.

 

How could that work?

You could do a table in InDesign, a second document behind the one you want to change the contents.

A table with two columns. Column one entries are the old number expressions, column two entries are the new number expressions just like that:

 

 

Next step woul be:

Make the other document the active one.

Have no other documents open, just the two.

 

Then run this ExtendScript script snippet.

It will pull the data from the document's table that is not the active document.

Column 1 contains the old text, column 2 contains the new text.

 

The script will then run on the active document with a loop and does find/change text actions according to the contents of the table in the other document:

 

var mySourceTable = app.documents[1].stories[0].tables[0];

var myListOldArray = mySourceTable.columns[0].contents ;
var myListNewArray = mySourceTable.columns[1].contents ;


var target = app.activeDocument;

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

for( var n=0; n<mySourceTable.rows.length; n++  )
{

	app.findTextPreferences.findWhat = myListOldArray[n] ;
	app.changeTextPreferences.changeTo = myListNewArray[n] ;
	target.changeText();
	
	app.findTextPreferences = null;
	app.changeTextPreferences = null;

};

 

Regards,
Uwe Laubender

( ACP )

 

 

Hypons111
Hypons111Author
Participating Frequently
November 8, 2019

no, cant use this skill on this job.  but Ajabon gave me a solution, im going try this at work tonight. thanks for helping ~

Community Expert
November 7, 2019

Now I'm confused.

Please give an example with a couple of numbers.

Number Old > Number New

 

If there is no rule how to change a particular number you cannot automate this.

 

Regards,
Uwe Laubender

( ACP )

Hypons111
Hypons111Author
Participating Frequently
November 7, 2019

let say i have 3 titles. roughly looks like this:

G.N. 1234

G.N. 9730

G.N. 1045

then i have to change them to:

G.N. 001

G.N. 002

G.N. 003

ajabon grinsmith
Community Expert
Community Expert
November 8, 2019

Hi, try this.

 

function pepsi (num){
    var str = String(num);
    while(str.length < 3){
        str = "0" + str;
        }
    return str;
    }

app.findGrepPreferences.findWhat = "G\\.N\\.\\d{3}$";

var myFind =app.activeDocument.findGrep();

var count = 0;

app.findGrepPreferences.findWhat = "\\d{3}";

for(var i = 0; i < myFind.length; i++) {
    app.changeGrepPreferences.changeTo = pepsi(++count);
    myFind[i].changeGrep();
    }

 

Community Expert
November 7, 2019

If its just about find and replace, why not just use the FindChangeList script, just create an entry for each replacement with as targetted a grep expression as you can. So as Uwe mentioned the logic is the key, if you make the grep expression targetted enough to only match the instance that you need then it should be fine.

 

-Manan

-Manan
Hypons111
Hypons111Author
Participating Frequently
November 7, 2019

becoz those number comes randomly every time, and I have to change them to serial number.

Community Expert
November 7, 2019

Instead of using changeGrep use findGrep this will give you a collection of your matches. Then you can iterate through the collection and replace each one of them with a new sequence of numbers

 

-Manan

-Manan
Community Expert
November 7, 2019

Hi hypons111,

changeGrep() is exactly working this way.

You could fetch all texts that match your search using method findGrep() instead and loop the result texts array to change every result individually. FWIW: You did not tell the algorithm for the right result, the recipe for how every single found text should be changed.

 

DOM documentation compiled by Jongware:

http://jongware.mit.edu/idcs6js/pc_Document.html#findGrep

 

Regards,
Uwe Laubender

( ACP )

Hypons111
Hypons111Author
Participating Frequently
November 7, 2019

you mean like this?

 

var num= 987;

for(var i=1; i<101; i++){

    app.findGrepPreferences.findWhat="G.N.//d//d//d/r";

    app.changeGrepPreferences.chsngeTo="G.N."+num;

    app.activeDocument.changeGrep();

    num++;

}