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

Export Footnotes in Pages Range using Grep and Array Causing Exported Duplicates in text File!

Enthusiast ,
Feb 17, 2023 Feb 17, 2023

Copy link to clipboard

Copied

hi Experts,

Im trying to export footnotes in pages range, it works and no errors but i found the exported (text) file contain duplicates of the results!, maybe something wrong in my logic but i cant catch the logical error!, please help and thanks in advance.

//Export Footnotes in Pages Range

DoFootnotesRange();

function DoFootnotesRange(){
    
    //Array for the Collected Footnotes Text
    var myFoundTexts = [];
    //grep options
    app.findChangeGrepOptions.includeMasterPages = false; 
    app.findChangeGrepOptions.includeFootnotes = true; 
    app.findChangeGrepOptions.includeHiddenLayers = false; 
    app.findChangeGrepOptions.includeLockedLayersForFind = true; 
    app.findChangeGrepOptions.includeLockedStoriesForFind = true; 

    var myDoc = app.activeDocument;
    var pgStart = 0; 
    var pgEnd =  9;
    var pageRange = myDoc.pages.itemByRange(pgStart, pgEnd).textFrames.everyItem().paragraphs.everyItem();

    var myFind = "~F.+$";
    
    //Clear Grep Prefrences
    app.findGrepPreferences = app.changeGrepPreferences = null;
    app.findGrepPreferences.findWhat = myFind;

    try {
        var myDoc = app.activeDocument;
        } catch(e) { exit(); }
        var f = File("~/Desktop/Doc Exported Footnotes in Pages Range .txt");
        f.encoding = "UTF-8";
        f.open("w");

        var myFoundTextParagraphs = pageRange.findGrep();
        
        //First nested loop to grab all the found texts in Range of Paragraphs in Page Range
        for (var i = 0; i < myFoundTextParagraphs.length; i++) {
            for (var j = 0; j < myFoundTextParagraphs[i].length; j++) {
                myFoundTexts.push(myFoundTextParagraphs[i][j]);
            }
        }
       
    //Now Loop to Show Found Text in Array
    for (var k = 0; k < myFoundTexts.length; k++) {
         f.writeln (myFoundTexts[k].contents+"\t"+myFoundTexts[k].parentTextFrames[0].parentPage.name)
        }
        app.findGrepPreferences = app.changeGrepPreferences = null;
        f.close();
        alert("Document Footnotes Exported to Desktop File, "+myFoundTexts.length+" Footnotes Collected!", "Finished");  
}

 

Best
Mohammad Hasanin
TOPICS
Scripting

Views

1.8K

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

Enthusiast , Feb 18, 2023 Feb 18, 2023

@m1b @Robert Tkaczyk , hi all, thanks a lot for your help and suggestions,  i create a new crazy method to fix this! and it works!, here it is :

//Export Footnotes in Pages Range
DoFootnotesRange();

function DoFootnotesRange(){
    
    //Array for the Collected Footnotes Text
    var myFoundTexts = [];
    //grep options
    app.findChangeGrepOptions.includeMasterPages = false; 
    app.findChangeGrepOptions.includeFootnotes = true; 
    app.findChangeGrepOptions.includeHiddenLayers = false; 
 
...

Votes

Translate

Translate
Guide , Feb 19, 2023 Feb 19, 2023

This seems to me simpler:

 

var myDoc = app.activeDocument,
myFootnotes = myDoc.stories.everyItem().footnotes.everyItem().getElements(),
F = myFootnotes.length,  f;
//---------------------------
var myStart = 1,  myEnd = 8;
//---------------------------
var myTxt = File("~/Desktop/Doc Exported Footnotes in Pages Range .txt");
myTxt.encoding = "UTF-8";
myTxt.open("w");
for ( var f = 0; f < F; f++ ) {
    var myPage = myFootnotes[f].storyOffset.parentTextFrames[0].parentPage.name;
    if ( myPage >=
...

Votes

Translate

Translate
Community Expert ,
Feb 17, 2023 Feb 17, 2023

Copy link to clipboard

Copied

Hi @M.Hasanin, so your exported text file shows the same contents and page name multiple times? Could you post a small sample .indd file for us to test with your script? - 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
Community Expert ,
Feb 17, 2023 Feb 17, 2023

Copy link to clipboard

Copied

 

 

//First nested loop to grab all the found texts in Range of Paragraphs in Page Range

 

 

Why nested loops ? 

 

 

var myFoundTextParagraphs = pageRange.findGrep();

 

 

This should return simple array - not array of arrays ? 

 

 

myFoundTexts.push(myFoundTextParagraphs[i][j]);

 

 

??

This loop will return myFoundTextParagraphs.length² results ?? 

 

▒► ID-Tasker / ID-Tasker Server - work smart not hard ◄▒

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 ,
Feb 17, 2023 Feb 17, 2023

Copy link to clipboard

Copied

@Robert Tkaczyk, the result of

myDoc.pages.itemByRange(pgStart, pgEnd).textFrames.everyItem().paragraphs.everyItem().findGrep();

is an array of arrays. It's because of the use of everyItem().

- 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
Community Expert ,
Feb 17, 2023 Feb 17, 2023

Copy link to clipboard

Copied

Right 😞 missed "[i]" in the inside loop 😞

 

▒► ID-Tasker / ID-Tasker Server - work smart not hard ◄▒

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 ,
Feb 17, 2023 Feb 17, 2023

Copy link to clipboard

Copied

Why not go through all TextFrames on the range of pages and export their Footnotes? 

 

But then you'll need to sort them by index - or findGrep returns them in the correct order? Or you don't need them organised? 

 

▒► ID-Tasker / ID-Tasker Server - work smart not hard ◄▒

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 ,
Feb 17, 2023 Feb 17, 2023

Copy link to clipboard

Copied

i did it and it succeeded before for the all book , but the original complete book is near 180 pages and other book near 500 pages so i decide to make it in range!, and no need to sort them, i start thinking to import text file inside indesign and removing duplicates via grep or make a new script for that, thank for your suggestion

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 ,
Feb 17, 2023 Feb 17, 2023

Copy link to clipboard

Copied

@Robert Tkaczyk @m1b 

Thanks a lot for replying me, i found that some results are duplicated!, here is part of book,  the link of 30 page of the book project , download from here (Google Drive) :

True Believer 30 page 

you can found it mixed language footnotes (Arabic and English) - GIF Animation

Some Duplicated Found.gif

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 ,
Feb 18, 2023 Feb 18, 2023

Copy link to clipboard

Copied

@m1b @Robert Tkaczyk , hi all, thanks a lot for your help and suggestions,  i create a new crazy method to fix this! and it works!, here it is :

//Export Footnotes in Pages Range
DoFootnotesRange();

function DoFootnotesRange(){
    
    //Array for the Collected Footnotes Text
    var myFoundTexts = [];
    //grep options
    app.findChangeGrepOptions.includeMasterPages = false; 
    app.findChangeGrepOptions.includeFootnotes = true; 
    app.findChangeGrepOptions.includeHiddenLayers = false; 
    app.findChangeGrepOptions.includeLockedLayersForFind = true; 
    app.findChangeGrepOptions.includeLockedStoriesForFind = true; 

    var myDoc = app.activeDocument;
    var pgStart = 0; 
    var pgEnd =  9;
    var myFind = "~F.+$";
    
    //Clear Grep Prefrences
    app.findGrepPreferences = app.changeGrepPreferences = null;
    app.findGrepPreferences.findWhat = myFind;

    try {
        var myDoc = app.activeDocument;
        } catch(e) { exit(); }
        var f = File("~/Desktop/Doc Exported Footnotes in Pages Range .txt");
        f.encoding = "UTF-8";
        f.open("w");

        
        //New Method for Pages Range!
        for (var n = pgStart; n <= pgEnd; n++) {
                var pageRange = myDoc.pages[n].textFrames.everyItem();
                var myFoundTextParagraphs = pageRange.findGrep();
                
        //First nested loop to grab all the found texts in Range of Paragraphs in Page Range
        //is an array of arrays. It's because of use of everyItem().
        for (var i = 0; i < myFoundTextParagraphs.length; i++) {
            for (var j = 0; j < myFoundTextParagraphs[i].length; j++) {
                myFoundTexts.push(myFoundTextParagraphs[i][j]);
                }
            }
        }
     
    //Now Loop to Show Found Text in Array
    for (var k = 0; k < myFoundTexts.length; k++) {
         f.writeln (myFoundTexts[k].contents+"\t"+myFoundTexts[k].parentTextFrames[0].parentPage.name)
        }
        app.findGrepPreferences = app.changeGrepPreferences = null;
        f.close();
        alert("Document Footnotes Exported to Desktop File", "Finished");  
}

 

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 ,
Feb 18, 2023 Feb 18, 2023

Copy link to clipboard

Copied

@m1b I thought that each instance of everyItem() creates another dimension - in the new version there is only one - so why nested loops ??

 

@M.Hasanin you've misunderstood my idea 😞 I've suggested to just iterate through all TextFrames - and process their collections of Footnotes - not to search every TextFrame - so your code would be much simpler and quicker.

 

▒► ID-Tasker / ID-Tasker Server - work smart not hard ◄▒

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 ,
Feb 19, 2023 Feb 19, 2023

Copy link to clipboard

Copied

@Robert Tkaczyk 

Thanks for your reply but can you please give me written code example for doing that ? or algorithm steps

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 ,
Feb 19, 2023 Feb 19, 2023

Copy link to clipboard

Copied

Would prefer algorithm as JS isn't my forte 😉

 

1st loop - through pages, 

2nd loop - nested - through TextFrames,

3rd loop - nested - through Footnotes.

 

▒► ID-Tasker / ID-Tasker Server - work smart not hard ◄▒

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 ,
Feb 19, 2023 Feb 19, 2023

Copy link to clipboard

Copied

@Robert Tkaczyk Thank you, i will try to apply this algorithm and see what will happened

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
Guide ,
Feb 19, 2023 Feb 19, 2023

Copy link to clipboard

Copied

This seems to me simpler:

 

var myDoc = app.activeDocument,
myFootnotes = myDoc.stories.everyItem().footnotes.everyItem().getElements(),
F = myFootnotes.length,  f;
//---------------------------
var myStart = 1,  myEnd = 8;
//---------------------------
var myTxt = File("~/Desktop/Doc Exported Footnotes in Pages Range .txt");
myTxt.encoding = "UTF-8";
myTxt.open("w");
for ( var f = 0; f < F; f++ ) {
    var myPage = myFootnotes[f].storyOffset.parentTextFrames[0].parentPage.name;
    if ( myPage >= myStart && myPage <= myEnd ) myTxt.writeln(myFootnotes[f].contents + "\t" + myPage);
}
myTxt.close();
alert("Document Footnotes Exported to Desktop File", "Finished");

 

(^/)  The Jedi

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 ,
Feb 19, 2023 Feb 19, 2023

Copy link to clipboard

Copied

You have used everyItem() twice - but then you are referring to the items as in the single size array?

 

▒► ID-Tasker / ID-Tasker Server - work smart not hard ◄▒

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 ,
Feb 19, 2023 Feb 19, 2023

Copy link to clipboard

Copied

@Robert Tkaczyk, the getElements() method of an everyItem() result returns a flat array of the resolved elements. In OP's case they did not use getElements(), because they wanted to use findGrep() method, which Array does not have.

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 ,
Feb 19, 2023 Feb 19, 2023

Copy link to clipboard

Copied

Thanks. 

 

▒► ID-Tasker / ID-Tasker Server - work smart not hard ◄▒

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 ,
Feb 19, 2023 Feb 19, 2023

Copy link to clipboard

Copied

@FRIdNGE  genius method,  yes it is much simpler Jedi!,Thanks a lot

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 ,
Feb 19, 2023 Feb 19, 2023

Copy link to clipboard

Copied

@m1b @FRIdNGE 

Can you please Explain what this storyoffset is doing? :

 

 

var myPage = myFootnotes[f].storyOffset.parentTextFrames[0].parentPage.name;

 

i mean how to use StoryOffset correctly and what it is?

 

and thanks in advance 

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 ,
Feb 19, 2023 Feb 19, 2023

Copy link to clipboard

Copied

myFootnotes is an array of all the footnotes in your document (it was made earlier in the script)

storyOffset gets the insertionPoint of the footnote

parentTextFrames[0] gets the textFrame that contains that insertionPoint

parentPage gets the page that contains the textFrame

name is the name of the page.

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 ,
Feb 20, 2023 Feb 20, 2023

Copy link to clipboard

Copied

LATEST

@m1b 

 thanks for explaining, i know the last three lines, it just first time to concentrate with StoryOffset, not much examples around!, thanks for help, have a best day

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