Skip to main content
M.Hasanin
Inspiring
February 17, 2023
Answered

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

  • February 17, 2023
  • 4 replies
  • 3145 views

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");  
}

 

This topic has been closed for replies.
Correct answer FRIdNGE

@Robert at ID-Tasker 

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


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

4 replies

M.Hasanin
M.HasaninAuthor
Inspiring
February 18, 2023

@Robert at ID-Tasker @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

Mohammad Hasanin
M.Hasanin
M.HasaninAuthor
Inspiring
February 18, 2023

@m1b @Robert at ID-Tasker , 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");  
}

 

Mohammad Hasanin
Robert at ID-Tasker
Legend
February 18, 2023

@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.

 

Robert at ID-Tasker
Legend
February 17, 2023

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? 

 

M.Hasanin
M.HasaninAuthor
Inspiring
February 18, 2023

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

Mohammad Hasanin
Robert at ID-Tasker
Legend
February 17, 2023

 

 

//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 ?? 

 

m1b
Community Expert
Community Expert
February 17, 2023

@Robert at ID-Tasker, 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

Robert at ID-Tasker
Legend
February 17, 2023

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

 

m1b
Community Expert
Community Expert
February 17, 2023

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