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