Copy link to clipboard
Copied
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
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
Copy link to clipboard
Copied
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
for(c=0;c<words.length;c++){
if(words
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
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 😃
Copy link to clipboard
Copied
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 .
Copy link to clipboard
Copied
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();
Copy link to clipboard
Copied
If you want to keep the window open you need a palette:
var w = new Window('palette','Plain English Find and Change');
Copy link to clipboard
Copied
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 steven rocket mention​​ed, to have the window stay open you will need a palette which will need a target engine specified. Looking at your script i really can't think of a use for the "find" button so i have removed it. Here is what I've come up with:
#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
next.onClick = function(){c++; app.selection = allFounds
prev.onClick = function(){c--; app.selection = allFounds
Though, I'm questioning as to why you need this as a script rather than going ahead and using the Find/Change dialog box?
Copy link to clipboard
Copied
This looks good but I have the same problem that I had with my scripts-the buttons do not work.
Any ideas?
Copy link to clipboard
Copied
try adding this bit of code to set user interaction before the dialog.
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;
Copy link to clipboard
Copied
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!
(^/)
Copy link to clipboard
Copied
Thanks Jarek!
(^/)