Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

find/change script highlight found text

Engaged ,
Sep 14, 2016 Sep 14, 2016

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

TOPICS
Scripting
2.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Sep 14, 2016 Sep 14, 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();

  }

}

a

...
Translate
Engaged ,
Sep 14, 2016 Sep 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 😃

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Sep 14, 2016 Sep 14, 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 .

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 15, 2016 Sep 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();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 15, 2016 Sep 15, 2016

If you want to keep the window open you need a palette:

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 15, 2016 Sep 15, 2016

I'm not sure if this is the best way to get to your desired result but i will put in my opinion anyway. 😃

If you are wanting to loop through each instance of the word manually then there really is no need for a for() loop. Also, as

#targetengine "session";

var dialog = new Window('palette {orientation:"column"}', "Find and Change")

with(dialog){

    var change = add('button', undefined, "Change");

    var next = add('button', undefined, "Next");

    var prev = add('button', undefined, "Previous");

}app.findTextPreferences.findWhat = "found";

app.changeTextPreferences.changeTo = "changed";

var allFounds = app.activeDocument.findText();

var c=0;

app.selection = allFounds;

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

dialog.show()

change.onClick = function(){allFounds.changeText(); c++; app.selection = allFounds; app.layoutWindows[0].zoomPercentage = app.layoutWindows[0].zoomPercentage};

next.onClick = function(){c++; app.selection = allFounds; app.layoutWindows[0].zoomPercentage = app.layoutWindows[0].zoomPercentage};

prev.onClick = function(){c--; app.selection = allFounds; app.layoutWindows[0].zoomPercentage = app.layoutWindows[0].zoomPercentage};

Though, I'm questioning as to why you need this as a script rather than going ahead and using the Find/Change dialog box?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 16, 2016 Sep 16, 2016

This looks good but I have the same problem that I had with my scripts-the buttons do not work.

Any ideas?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 16, 2016 Sep 16, 2016

try adding this bit of code to set user interaction before the dialog.

app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 16, 2016 Sep 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!

(^/) 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 16, 2016 Sep 16, 2016
LATEST

Thanks Jarek! 

(^/)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines