Copy link to clipboard
Copied
Only between certain pages
How can i make changes
var myDoc = app.activeDocument;
var offset = Number(myDoc.pages[0].name);
var myW = new Window ("dialog", "page text change", undefined, /*{borderless: true}*/);
var brans = myW.add ("edittext", [0, 0, 60, 25], "AA");
brans.active = true;
var gr = myW.add ("group");
var fpage = gr.add ("edittext", [0, 0, 30, 25], offset);
var tire = gr.add ("statictext", undefined, " - ");
var lpage = gr.add ("edittext", [0, 0, 30, 25], (offset + myDoc.pages.length - 1).toString());
var tire = gr.add ("statictext", undefined, "pages");
var start = Number(fpage.text) - Number(app.activeDocument.pages[0].name);
var finish = Number(lpage.text) - Number(app.activeDocument.pages[0].name);
var buttons = myW.add ("group")
buttons.add ("button", undefined, "OK", {name: "ok"});
buttons.add ("button", undefined, "Cancel", {name: "cancel"}).onClick = function() {
myW.close(0);
};
if(myW.show () == 1) {
for (p = start; p <= finish; p++)
{
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = brans.text;
app.changeGrepPreferences.changeTo = "BB";
myDoc.pages
.changeGrep(); //??????????????????????
app.findGrepPreferences = app.changeGrepPreferences = null;
}
}
Hi hamdifem,
prepare for the occassion that your found array contains an empty item.
For that do the following:
1. Add a new document with 3 pages.
2. Add a text frame on every page.
3. Type in one single word in every text frame.
But not the string you like to find.
Do not leave the text frames empty.
4. Now add the string "AA" to the text frame on page 2.
Put it into a paragraph of its own after the word that is already there!
Now run your snippet with the following for-loop, that would check some cases
...Copy link to clipboard
Copied
Change your if-statement to:
if (myW.show () == 1) {
var pageRange = myDoc.pages.itemByRange(fpage.text, lpage.text).textFrames.everyItem().paragraphs.everyItem();
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = brans.text;
app.changeGrepPreferences.changeTo = "BB";
pageRange.changeGrep();
app.findGrepPreferences = app.changeGrepPreferences = null;
}
Kai
Copy link to clipboard
Copied
Ok
But I get an error when I use it like this
var found = pageRange.findGrep(); //???
var founds = pageRange.findGrep(true); //???
for(var i = 0; i < founds.length; ++i) {
var old = founds.contents;
if(old == ...
or
var found = pageRange.findGrep();
var sno = 0;
for(var i = 0; i < found.length; ++i) {
found.contents = (++sno) + "\t" + found.contents;
}
Copy link to clipboard
Copied
Hi hamdifem,
prepare for the occassion that your found array contains an empty item.
For that do the following:
1. Add a new document with 3 pages.
2. Add a text frame on every page.
3. Type in one single word in every text frame.
But not the string you like to find.
Do not leave the text frames empty.
4. Now add the string "AA" to the text frame on page 2.
Put it into a paragraph of its own after the word that is already there!
Now run your snippet with the following for-loop, that would check some cases.
And writes the results to the JavaScript Console of the ESTK:
var found = pageRange.findGrep();
for(var n=0;n<found.length;n++)
{
// If empty array item, loop immediately on:
if(found
== "") {
$.writeln(n+"\t"+"EMPTY ARRAY ITEM");
continue
};
// Write the found object to
// the JavaScript Console of the ESTK:
$.writeln(n+"\t"+found
); $.writeln(n+"\t"+found
.constructor.name); $.writeln(n+"\t"+found
.length); $.writeln(n+"\t"+found
[0].constructor.name); };
The results should be something like that:
0 EMPTY ARRAY ITEM
1 EMPTY ARRAY ITEM
2 [object Word]
2 Array
2 1
2 Word
3 EMPTY ARRAY ITEM
You see that the total number of pages is 3, but the array length is 4.
How can the array length be explained?
That's because you are looking for paragraphs.everyItem().
( Kai's suggestion above. )
And the number of paragraphs in the document is actually 4.
On page one and two, respectively index 0 and 1 of the array found InDesign has stored an empty item.
Two paragraphs were visited, nothing was found.
Index 2 is showing a hit. Yes. That's the second paragraph on page two. Index 2 out of the total of all visited paragraphs.
Then on page 3 is yet another paragraph, that does contain no match. So again, an empty item is added to the found array.
Also see, what exactly is found.
found is storing an array of:
A. empty items or:
B. arrays with length 1 containing a Word
found
[0].contents
should give you access to the contents of the found text.
Regards,
Uwe
// EDIT: Did some edits to clarify the code.
// Changed "EMPTY ARRAY" to "EMPTY ARRAY ITEM"