Skip to main content
Inspiring
September 14, 2016
Answered

find/change script highlight found text

  • September 14, 2016
  • 2 replies
  • 2396 views

Hello,

I've written this script and it works great. But what I'd like to do is go through each found word individually (like in the find/change dialog with the button 'find next').

This is a snippet:

app.findTextPreferences=app.changeTextPreferences=null;
var what = app.findTextPreferences.findWhat="ensure";

app.changeTextPreferences.changeTo="make sure";
var found =app.activeDocument.changeText();
if (found.length=1){
alert(found.length+" instance of 'ensure' was changed");
}else{
   alert(found.length+" instances of 'ensure' were changed");
  }

I want the script to make each instance of "ensure" my active selection so I can see where they are before I change them.

Thanks

This topic has been closed for replies.
Correct answer Kai Rübsamen

Hi Guys,

try this one:

app.findTextPreferences = app.changeTextPreferences = null;

app.findTextPreferences.findWhat = "ensure";

app.changeTextPreferences.changeTo = "make sure";

var allFounds = app.activeDocument.findText();

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

  var curFound = allFounds;

  curFound.select();

  app.layoutWindows[0].zoomPercentage = app.layoutWindows[0].zoomPercentage;

   if(confirm("Do you wish to change this instance?",undefined, "Find/Replace")) {

     curFound.changeText();

  }

}

app.findTextPreferences = app.changeTextPreferences = null;

Disadvantage of the confirm-dialog: There is no exit-button .

2 replies

Obi-wan Kenobi
Legend
September 16, 2016

Hi,

I don't really understand what you mean!

I often do this: Write, e.g., a regex: Find something and replace by some other thing!

So, I place my smart fingers on the keyboard and …:

Ctrl-[num]1: jump to the first occurrence => if I want to replace => Ctrl-[num]2; if not, Ctrl-[num]1, … and so on!

I'm not Mozart! … but I can play some cool music with my keyboard!

(^/) 

Obi-wan Kenobi
Legend
September 16, 2016

Thanks Jarek! 

(^/)

Skemicle
Inspiring
September 14, 2016

I believe scripting find/change changes all instances in one action, making it very improbable if not impossible to select each instance individually before making the change.. I would use something like:

var doc = app.activeDocument;

var txts = doc.textFrames;

var x = 0;

for(c=0;c<txts.length;c++){

    words = txts.words;

    for(c=0;c<words.length;c++){

        if(words.contents.toLowerCase() == "ensure"){

            app.selection = words;

            app.layoutWindows[0].zoomPercentage = app.layoutWindows[0].zoomPercentage;

                if(confirm("Do you wish to change this instance?",undefined, "Find/Replace") == true){

                    words.contents = "make sure";

                    x++

                }

        }app.selection = null;

    }

}alert(x + " changes were made");

Though for some reason I am only to get this to run through only 1 text frame. Maybe you or someone else can fix that flaw =)

Kai Rübsamen
Kai RübsamenCorrect answer
Participating Frequently
September 15, 2016

Hi Guys,

try this one:

app.findTextPreferences = app.changeTextPreferences = null;

app.findTextPreferences.findWhat = "ensure";

app.changeTextPreferences.changeTo = "make sure";

var allFounds = app.activeDocument.findText();

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

  var curFound = allFounds;

  curFound.select();

  app.layoutWindows[0].zoomPercentage = app.layoutWindows[0].zoomPercentage;

   if(confirm("Do you wish to change this instance?",undefined, "Find/Replace")) {

     curFound.changeText();

  }

}

app.findTextPreferences = app.changeTextPreferences = null;

Disadvantage of the confirm-dialog: There is no exit-button .

Inspiring
September 15, 2016

Thanks for the replies guys,

Your scripts are great, but now I'm trying to add functionality to it so that you can skip to the next one without the dialog closing.

To do this I'm using ScriptUI.

I'm trying to add the function clickNext to the next button but it doesn't work.

Do you know where I'm going wrong?

var w = new Window('dialog','Plain English Find and Change');

var check1 = w.add('checkbox',undefined,'ensure to make sure');
var check2 = w.add('checkbox',undefined,'utilise to use');

var find = w.add('button',undefined,'Find');
var change = w.add('button',undefined,'Change');
var next = w.add('button',undefined,'Next');
var previous = w.add('button',undefined,'Previous');

   
  

var clickNext = function () {
if (check1.value=true) {
app.findTextPreferences = app.changeTextPreferences = null; 
app.findTextPreferences.findWhat = "ensure"; 
app.changeTextPreferences.changeTo = "make sure"; 
var allFounds = app.activeDocument.findText();
  
for (var i = 0; i < allFounds.length; i++) { 
  var curFound = allFounds
  curFound.select(); 
  app.layoutWindows[0].zoomPercentage = app.layoutWindows[0].zoomPercentage;
  }

}

};
next.onClick=clickNext();