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

How to Access Text inside Array Generated from (ItemByRange) of Document Pages!

Enthusiast ,
Aug 11, 2021 Aug 11, 2021

Copy link to clipboard

Copied

Hi Experts, Im Still Learning JavaScript and Try to Explore its Myths!, I tried hard to understand how to extract text from (itemByRange) Array but i failed!, Im Simply trying to execute GREP to find and replace anything between brackets and change it to ABC (as an Example)  in only Range of Pages (1-5) but i want to show the final result found in the pages Using (ShowText) method to the final user, so this is my try :

//itemByRange and Show Text
var myDoc = app.activeDocument;
var pageRange = myDoc.pages.itemByRange(1, 5).textFrames.everyItem().paragraphs.everyItem();
var myFind = "\\(.+?\\)";
var myChange = "ABC";
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;
app.findGrepPreferences.properties = ({findWhat: myFind});
app.changeGrepPreferences.properties = ({changeTo: myChange});
myFoundText = pageRange.findGrep(); 
for(i=0;i<myFoundText.length;i++){
    myFoundText[i] = pageRange.changeGrep();
    $.writeln(i); //Document Page Numbers
    ShowMeText();
}

function ShowMeText() {
    var myTextArray = [];
    var myDoc = app.activeDocument;
    var myText = myDoc.pages.itemByRange(1, 5).textFrames.everyItem().paragraphs.everyItem();
    for (var i = 0; i < myText.length; i++) {
        myTextArray.push(myText.name);
    }    
    var textfound = myTextArray.join(",")
    for(i=0;i<textfound.length;i++){
            $.writeln(i);
            textfound[i] = textfound[i].showText(); 
        }
}

I understand ofcousrse the other part of code (the Function) have many  problems (Result: undefined), this is logic because i didnt understand how to extract the GREP founded text from the itemByRange to Show it Pages!, its just there to make the idea more closer, Please Help and Thanks in Advance.

Best
Mohammad Hasanin
TOPICS
Scripting

Views

354

Translate

Translate

Report

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 2 Correct answers

Community Expert , Aug 11, 2021 Aug 11, 2021

Hi @M.Hasanin, try putting this in after "myFoundText = pageRange.findGrep();"

 

for (var i = 0; i < myFoundText.length; i++) {
    for (var j = 0; j < myFoundText[i].length; j++) {
        var item = myFoundText[i][j];
        $.writeln(
            'i = ' + i
            + ' j = ' + j
            + ' obj = ' + item.constructor.name
            + ' contents = ' + item.contents
            + ' page = ' + item.parentTextFrames[0].parentPage.name
        );
    }
}

 

It will print to console some deta

...

Votes

Translate

Translate
Community Expert , Aug 12, 2021 Aug 12, 2021

I've adjusted your script (just for learning purposes!) so you can see a few things about your situation. One thing I have done is named the variables a bit differently. I find that it is often really helpful to more precisely name variables, because it forces me to understand what type of object that thing is. So the object you called pageRange wasn't a page range, but was an every-Item-Of-TextFrame-Paragraphs-In-Page-Range (not a real object name; technically it seems to be a special indesign

...

Votes

Translate

Translate
Community Expert ,
Aug 11, 2021 Aug 11, 2021

Copy link to clipboard

Copied

Hi @M.Hasanin, try putting this in after "myFoundText = pageRange.findGrep();"

 

for (var i = 0; i < myFoundText.length; i++) {
    for (var j = 0; j < myFoundText[i].length; j++) {
        var item = myFoundText[i][j];
        $.writeln(
            'i = ' + i
            + ' j = ' + j
            + ' obj = ' + item.constructor.name
            + ' contents = ' + item.contents
            + ' page = ' + item.parentTextFrames[0].parentPage.name
        );
    }
}

 

It will print to console some details about the myFoundText object that I think will help you. (It might fail if found text is in table or footnote, not sure.)

- Mark

Votes

Translate

Translate

Report

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 ,
Aug 12, 2021 Aug 12, 2021

Copy link to clipboard

Copied

Thanks a lot @m1b 

it Works perfect after Adding two lines at the end :

 

 

var myDoc = app.activeDocument;
var pageRange = myDoc.pages.itemByRange(1, 5).textFrames.everyItem().paragraphs.everyItem();
var myFind = "\\(.+?\\)";
var myChange = "ABC";
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;
app.findGrepPreferences.properties = ({findWhat: myFind});
app.changeGrepPreferences.properties = ({changeTo: myChange});
myFoundText = pageRange.findGrep(); 
for (var i = 0; i < myFoundText.length; i++) {
    for (var j = 0; j < myFoundText[i].length; j++) {
        var item = myFoundText[i][j];
        $.writeln(
            'i = ' + i
            + ' j = ' + j
            + ' obj = ' + item.constructor.name
            + ' contents = ' + item.contents
            + ' page = ' + item.parentTextFrames[0].parentPage.name
        );
        myFoundText[i][j].showText();
        myFoundText[i][j] = pageRange.changeGrep();
    }
}

 

 

 

 

Best
Mohammad Hasanin

Votes

Translate

Translate

Report

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
Community Expert ,
Aug 12, 2021 Aug 12, 2021

Copy link to clipboard

Copied

The object you get from the findGrep() is an Array of  the found texts (eg. Words, Texts, etc) grouped into arrays by paragraph. So in the code above, when i=0 all the objects in myFoundText[0] are found in the same paragraph, and myFoundText[1][0] is the first found Word or Text (or whatever) that was found in another paragraph.

- Mark

Votes

Translate

Translate

Report

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 ,
Aug 12, 2021 Aug 12, 2021

Copy link to clipboard

Copied

Thanks alot @m1b , i leaened  a lot from you, thanks again

Best Wishes

Best
Mohammad Hasanin

Votes

Translate

Translate

Report

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 ,
Aug 12, 2021 Aug 12, 2021

Copy link to clipboard

Copied

Eng @m1b , Thanks alot for help, But Can you help me little extra, 

I just need to know how to count the final changed words with the grep? with the same code above, i tried :

 

item.words.length

 

 

and

 

item.length

 

 

also i tried to add this two lines at the last :

 

 

mChange+=item.length; //for Count
alert("Total Changed is : "+Number(mChange)+" GREP's","Finito");

 

 

it Shouid work but it doesn't,, it gives NaN (Not a Number!) maybe i miss something!

Nan.jpg

 

 

 

Thanks

Best
Mohammad Hasanin

Votes

Translate

Translate

Report

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
Community Expert ,
Aug 12, 2021 Aug 12, 2021

Copy link to clipboard

Copied

I've adjusted your script (just for learning purposes!) so you can see a few things about your situation. One thing I have done is named the variables a bit differently. I find that it is often really helpful to more precisely name variables, because it forces me to understand what type of object that thing is. So the object you called pageRange wasn't a page range, but was an every-Item-Of-TextFrame-Paragraphs-In-Page-Range (not a real object name; technically it seems to be a special indesign Array). What you called myFoundTexts were actually arrays of paragraphs of found texts. See what you think. These changes are just for showing—you don't need to adopt them. 🙂

var myDoc = app.activeDocument;
var paragraphsInPageRange = myDoc.pages.itemByRange(1, 5).textFrames.everyItem().paragraphs.everyItem();

// this next line shows that you have an 'everyItem' object
// because length shows the length of each element of the array
// it is not a normal list!
$.writeln('paragraphsInPageRange.length = ' + paragraphsInPageRange.length);

// the findWhat string in the next line needs to find
// characters in between backslashes \.+?\
// but we need to escape the backslashes for a regex so
// a regex literal would be /\\.+?\\/g
// *but* we need to supply a string not a regex, so we need
// to escape backslashes again!
var myFindWhat = '\\\\.+?\\\\';
var myChangeTo = "ABC";
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;
app.findGrepPreferences.properties = ({ findWhat: myFindWhat });
app.changeGrepPreferences.properties = ({ changeTo: myChangeTo });

// myFoundTextParagraphs on the next line will
// be an Array of Arrays. The inner array contains
// find results per paragraph because you are doing
// .findGrep() on paragraphsInPageRange, which is
// the result of: paragraphs.everyItem() in line 2
var myFoundTextParagraphs = paragraphsInPageRange.findGrep();

// a new array to store the found texts
var myFoundTexts = [];

// nested loop to grab all the found texts in a new "flat" array
for (var i = 0; i < myFoundTextParagraphs.length; i++) {
    for (var j = 0; j < myFoundTextParagraphs[i].length; j++) {
        // add the actual found text item to the new array
        myFoundTexts.push(myFoundTextParagraphs[i][j]);
    }
}

// now the array is 'flat' you can count it easily
$.writeln('myFoundTexts.length = ' + myFoundTexts.length);

// and you can iterate over it in a single loop:
for (var i = 0; i < myFoundTexts.length; i++) {
    //myFoundTexts[i].showText(); // not necessary here?
    myFoundTexts[i].changeGrep();
}

 

- Mark

Votes

Translate

Translate

Report

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 ,
Aug 12, 2021 Aug 12, 2021

Copy link to clipboard

Copied

LATEST

Thanks a lot @m1b , This Example is Very Very Clear, and Simplifed the Myth a lot, I can't tell you how I'm Thankful, I Started Study javaScript from two books in the same time! one for general and one for indesign! , but indesign have a very special things belong to him!. thanks again.

Best Wishes

Best
Mohammad Hasanin

Votes

Translate

Translate

Report

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
Community Expert ,
Aug 11, 2021 Aug 11, 2021

Copy link to clipboard

Copied

As you wrote, there are some flaws in the code.
You need to clear the previous problem before you can complete the method "showText ()".

-Line 2:
findGrep () and changeGrep () are also valid for the textFrames class. If you want to run it on all paragraphs, you don't need "paragraphs.everyItem ()".

-Line 21
The variable myText probably doesn't have the property "name". Should return undefined.
Also, this would result in all the elements of the array myTextArray having the same content. You forgot to use the variable i for "myTest".

I think that whole method "ShowMeText" is unnecessary.

It also seems useless to execute the method showText () on all replacement results. It's best to run only the last one.

I fixed Line 2 and Line 9 and later.

 

var myDoc = app.activeDocument;
var pageRange = myDoc.pages.itemByRange(1, 5).textFrames.everyItem();
var myFind = "\\(.+?\\)";
var myChange = "ABC";
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;
app.findGrepPreferences.properties = ({findWhat: myFind});
app.changeGrepPreferences.properties = ({changeTo: myChange});
myChangedText = pageRange.changeGrep(); 
for(i=0;i<myChangedText.length;i++){
    //myFoundText[i] = pageRange.changeGrep();
    $.writeln(i); //Document Page Numbers
}
myChangedText[i - 1][0].showText();

 

Votes

Translate

Translate

Report

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 ,
Aug 12, 2021 Aug 12, 2021

Copy link to clipboard

Copied

Thanks a lot @ajabon grinsmith 

it Works Partially and Stopped Showing this error Message :

Error Number 21.jpg

Best
Mohammad Hasanin

Votes

Translate

Translate

Report

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
Community Expert ,
Aug 12, 2021 Aug 12, 2021

Copy link to clipboard

Copied

@M.Hasanin 

I'm sorry, it seemed that exception handling was needed if the last text didn't replace.
It seems to work with the code @m1b , so please forget about my code.


Regards 

Votes

Translate

Translate

Report

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 ,
Aug 12, 2021 Aug 12, 2021

Copy link to clipboard

Copied

@ajabon grinsmith 

No need to Sorry my Friend, Thanks a lot for your reply, you helped me a lot

Best Wishes

Best
Mohammad Hasanin

Votes

Translate

Translate

Report

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