Skip to main content
Obi-wan Kenobi
Legend
May 30, 2016
Answered

Find the first footnote of each page! …

  • May 30, 2016
  • 1 reply
  • 747 views

Hi all ( again! 😉 ),

All is in this topic title!

What I've already written:

var myDoc = app.activeDocument; 

var myTFrame = myDoc.pages[0].textFrames.everyItem().getElements(); 

var myCondition = "FirstNoteOnPage";

myTFCount = myDoc.pages[0].textFrames.count();

app.findGrepPreferences = app.changeGrepPreferences = null; 

app.findGrepPreferences.findWhat = "(?<=.)~F";

myNotes = myDoc.findGrep(); 

for (i = 0; i < myTFCount ; i++){ 

myFirstNote = myNotes[0].appliedConditions[myCondition]; 

}

It doesn't work! [of course!] 

Thanks for help!

This topic has been closed for replies.
Correct answer Laubender

Hi Obi-wan,

since a page object cannot be the natural scope for the findGrep() method we have to do a "dirty" trick to part the first footnotes on every page from the rest. And then apply a condition on the found and stored notes:

// FindFirstFootnote-of-Page-ApplyCondition.jsx

// Uwe Laubender

/**

* @@@BUILDINFO@@@ FindFirstFootnote-of-Page-ApplyCondition.jsx !Version! Tue May 31 2016 02:09:26 GMT+0200

*/

app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;

app.doScript

    (

  

    getFirstFootnoteOnPageApplyCondition,

    ScriptLanguage.JAVASCRIPT,

    [],

    UndoModes.ENTIRE_SCRIPT,

    "Get first footnote on page and apply condition | SCRIPT"

  

    );

function getFirstFootnoteOnPageApplyCondition()

{

    if(app.documents.length == 0){return};

  

    var myDoc = app.activeDocument;

    var myDocPagesArray = myDoc.pages.everyItem().getElements();

    // Array to store the found texts:

    var foundFirst = [];

    // Finding the footnotes on the pages

    // Page by page. Using an old trick:

    var myPageItems = myDoc.pageItems.everyItem().locked = true;

  

    // Looping the pages where all pageItems on the current page looped are unlocked.

    // GREP will find only texts in unlocked text frames or text paths.

    for(var n=0;n<myDocPagesArray.length;n++)

    {

        // Unlock all pageItems on the page:

        myDocPagesArray.pageItems.everyItem().locked = false;

      

        // Do the GREP Search:

        app.findGrepPreferences = app.changeGrepPreferences = null;

        app.findGrepPreferences.findWhat = "(?<=.)~F";

        myResult = myDoc.findGrep();

      

        // If no footnote is found on the page:

        if(myResult.length == 0)

        {

            // Lock all pageItems on the page:

            myDocPagesArray.pageItems.everyItem().locked = true;

            // Loop on to the next page:

            continue

        }

      

        // Otherwise save the first item of the result into an array:

        foundFirst[foundFirst.length++] = myResult[0];

      

        // Don't forget to lock all pageItems on the page:

        myDocPagesArray.pageItems.everyItem().locked = true;

    }

    // Now all results are gathered in the array.

    // Unlock all pageItems of the document:

    myDoc.pageItems.everyItem().locked = false;

    // APPLY A CONDITION TO THE FIRST FOOTNOTE ON A PAGE:

    // Add a condition, if no condition is there:

    var conditionName = "FirstNoteOnPage";

    if(!app.documents[0].conditions.itemByName(conditionName).isValid)

    {

        var footnoteCondition = app.documents[0].conditions.add

        (

            {

                name : "FirstNoteOnPage" ,

                indicatorMethod : ConditionIndicatorMethod.USE_HIGHLIGHT ,

                indicatorColor : [255,155,0] ,

                visible : true

            }

        )

    }

    else

    {

        var footnoteCondition = app.documents[0].conditions.itemByName(conditionName)

    };

    // Doing something with the found text:

    for(var n=0;n<foundFirst.length;n++)

    {

        // Run function defined below on the found text:

        whatShouldBeDoneToMyNotes(foundFirst);

    };

    function whatShouldBeDoneToMyNotes(myText)

    {

        // Do something.

        // E.g. Apply a condition

        myText.applyConditions([footnoteCondition]);

      

    };

};

Hope, that helps…

Uwe

1 reply

pixxxelschubser
Community Expert
Community Expert
May 30, 2016

Hi Obi,

sorry I do not work with footnotes in my documents.

But perhaps a step in the right direction - this will find all footnotes and apply your char style

var myDoc = app.activeDocument;

var myCondition= myDoc.characterStyles.itemByName("FirstNoteOnPage");

app.findGrepPreferences = app.changeGrepPreferences = null;

app.findGrepPreferences.findWhat = "(?<=.)~F";

app.changeGrepPreferences.appliedCharacterStyle = myCondition;

myDoc.changeGrep();

Have fun

Obi-wan Kenobi
Legend
May 30, 2016

Hi Pixxxel,

… A long time! ;-)

What I would like to find is the first "(?<=.)~F" on each page!

It could be anything else: a word, a para, a table, a graphic frame, …

LaubenderCommunity ExpertCorrect answer
Community Expert
May 31, 2016

Hi Obi-wan,

since a page object cannot be the natural scope for the findGrep() method we have to do a "dirty" trick to part the first footnotes on every page from the rest. And then apply a condition on the found and stored notes:

// FindFirstFootnote-of-Page-ApplyCondition.jsx

// Uwe Laubender

/**

* @@@BUILDINFO@@@ FindFirstFootnote-of-Page-ApplyCondition.jsx !Version! Tue May 31 2016 02:09:26 GMT+0200

*/

app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;

app.doScript

    (

  

    getFirstFootnoteOnPageApplyCondition,

    ScriptLanguage.JAVASCRIPT,

    [],

    UndoModes.ENTIRE_SCRIPT,

    "Get first footnote on page and apply condition | SCRIPT"

  

    );

function getFirstFootnoteOnPageApplyCondition()

{

    if(app.documents.length == 0){return};

  

    var myDoc = app.activeDocument;

    var myDocPagesArray = myDoc.pages.everyItem().getElements();

    // Array to store the found texts:

    var foundFirst = [];

    // Finding the footnotes on the pages

    // Page by page. Using an old trick:

    var myPageItems = myDoc.pageItems.everyItem().locked = true;

  

    // Looping the pages where all pageItems on the current page looped are unlocked.

    // GREP will find only texts in unlocked text frames or text paths.

    for(var n=0;n<myDocPagesArray.length;n++)

    {

        // Unlock all pageItems on the page:

        myDocPagesArray.pageItems.everyItem().locked = false;

      

        // Do the GREP Search:

        app.findGrepPreferences = app.changeGrepPreferences = null;

        app.findGrepPreferences.findWhat = "(?<=.)~F";

        myResult = myDoc.findGrep();

      

        // If no footnote is found on the page:

        if(myResult.length == 0)

        {

            // Lock all pageItems on the page:

            myDocPagesArray.pageItems.everyItem().locked = true;

            // Loop on to the next page:

            continue

        }

      

        // Otherwise save the first item of the result into an array:

        foundFirst[foundFirst.length++] = myResult[0];

      

        // Don't forget to lock all pageItems on the page:

        myDocPagesArray.pageItems.everyItem().locked = true;

    }

    // Now all results are gathered in the array.

    // Unlock all pageItems of the document:

    myDoc.pageItems.everyItem().locked = false;

    // APPLY A CONDITION TO THE FIRST FOOTNOTE ON A PAGE:

    // Add a condition, if no condition is there:

    var conditionName = "FirstNoteOnPage";

    if(!app.documents[0].conditions.itemByName(conditionName).isValid)

    {

        var footnoteCondition = app.documents[0].conditions.add

        (

            {

                name : "FirstNoteOnPage" ,

                indicatorMethod : ConditionIndicatorMethod.USE_HIGHLIGHT ,

                indicatorColor : [255,155,0] ,

                visible : true

            }

        )

    }

    else

    {

        var footnoteCondition = app.documents[0].conditions.itemByName(conditionName)

    };

    // Doing something with the found text:

    for(var n=0;n<foundFirst.length;n++)

    {

        // Run function defined below on the found text:

        whatShouldBeDoneToMyNotes(foundFirst);

    };

    function whatShouldBeDoneToMyNotes(myText)

    {

        // Do something.

        // E.g. Apply a condition

        myText.applyConditions([footnoteCondition]);

      

    };

};

Hope, that helps…

Uwe