0

I'm trying to find all "#"s on the active page and change them to numbers, ie. 1, 2, 3 . . .

The code below is what I thought would work but it doesn't. Instead, it changes every "#" to a "0".

app.findTextPreferences = app.changeTextPreferences = NothingEnum.NOTHING;
app.findTextPreferences.findWhat = "#";

var finds = app.activeDocument.findText();
if (finds.length > 0) {
   for (var i = 0; i < finds.length; i++)  
   {  
      app.changeTextPreferences.changeTo = "no: " + i;
      app.activeDocument.changeText();
   }  
   else
   {
      alert("Nothing has been found");
   }
}
Josh Voigts
  • 4,114
  • 1
  • 18
  • 43
ali haydar
  • 125
  • 10

3 Answers3

1

As ali haydar stated, changeText will apply globally and will break the former findText texts references. What you need is to use the contents property in your loop.

app.findTextPreferences = app.changeTextPreferences = NothingEnum.NOTHING;
app.findTextPreferences.findWhat = "#";

var finds = app.activeDocument.findText();
if (finds.length > 0) 
{
    for (var i = 0; i < finds.length; i++)  
    {  
      finds[i].contents = "no: " + String(i);
    }  
}
else
{
alert("Nothing has been found");
}
Jongware
  • 22,200
  • 8
  • 54
  • 100
Loic
  • 2,173
  • 10
  • 13
  • Thank you very much, it works! How is the best approach to limit this find/change only on the current page? – ali haydar Apr 07 '16 at 09:31
  • Shouldn't be there a "{" between the first "if" and "for"? – ali haydar Apr 07 '16 at 09:58
  • Indeed. I just copy/pasted your code without closer look. It's always a good move to add curly braces and semi-colons even if possibly optional. – Loic Apr 07 '16 at 10:12
0

In fact you can even get rid of the if condition:

app.findTextPreferences = app.changeTextPreferences = null;
app.findTextPreferences.findWhat = "#";

var finds = app.activeDocument.findText();
var n = finds.length;
var nStart = n;
while (n-- ) finds[n].contents = "no: " + String(n+1);
alert(nStart? nStart+" replacements made…" : "Nothing has been found");
Jongware
  • 22,200
  • 8
  • 54
  • 100
Loic
  • 2,173
  • 10
  • 13
  • Thank you, this works too, and certainly looks better. But it says "-1 replacements made" after the changes, and when I hit ctrl+z, I see the replacements was made in reverse order, is it normal? – ali haydar Apr 07 '16 at 11:04
  • any comment on how to make this work only on the current page :) – ali haydar Apr 07 '16 at 11:51
0

To consider page 1 only…

var main = function(){
    var doc = app.properties.activeDocument,
     finds,n;

    app.findTextPreferences = app.changeTextPreferences = null;
    app.findTextPreferences.findWhat = "#";

    if ( !doc ) return;
    finds = doc.findText();
    n = finds.length;

    while (n-- ) {
        finds[n].parentTextFrames.length
        && finds[n].parentTextFrames[0].isValid
        && finds[n].parentTextFrames[0].parentPage.id==doc.pages[0].id
        && finds[n].contents = "no: " + String(n+1);
    }

};

main();
Jongware
  • 22,200
  • 8
  • 54
  • 100
Loic
  • 2,173
  • 10
  • 13
  • thank you, i got an error "doc not defined"; if I define it like this `var doc = app.activeDocument` works on every page not just page 1 – ali haydar Apr 07 '16 at 13:19
  • 1
    That doesn't make sense. I just edited snippet so it includes all necessary code. Did you leave your changeText call ? – Loic Apr 07 '16 at 13:41
  • can you make it count only when it changes, not every time it finds? – ali haydar Apr 07 '16 at 14:14