11 Replies Latest reply on May 31, 2016 3:48 AM by Obi-wan Kenobi

    Find the first footnote of each page! …

    Obi-wan Kenobi Adobe Community Professional

      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!

        • 1. Re: Find the first footnote of each page! …
          pixxxel schubser Level 5

          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

           

          • 2. Re: Find the first footnote of each page! …
            Obi-wan Kenobi Adobe Community Professional

            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, …

            • 3. Re: Find the first footnote of each page! …
              Laubender Adobe Community Professional & MVP

              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[n].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[n].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[n].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[n]);
                  };
              
              
                  function whatShouldBeDoneToMyNotes(myText)
                  {
                      // Do something.
                      // E.g. Apply a condition
                      myText.applyConditions([footnoteCondition]);
                     
                  };
              
              };
              

               

              Hope, that helps…

               

              Uwe

              2 people found this helpful
              • 4. Re: Find the first footnote of each page! …
                Obi-wan Kenobi Adobe Community Professional

                Wow! …

                 

                My script was almost ... good! Thank you for these little corrections!

                 

                Of course, it is a joke!

                 

                Uwe, thank you very much!

                Your script, well commented, is truly amazing! 

                A real reference for playing find/replace on page! … A so beautiful code and so apparently simple to be read and understood!

                • 5. Re: Find the first footnote of each page! …
                  Laubender Adobe Community Professional & MVP

                  Hi Obi-wan,

                  I already commented on my "dirty" trick.

                   

                  I call it "dirty" because if you have locked items in the document and want to have locked the same items after running the script, you have to check for locked items before locking all items, store the locked ones (or their IDs) to an array and lock them after the search. Or still lock them, if the loop hits a page with formerly locked items.

                   

                  I leave it up to you to write a function for this ;-)

                   

                  Uwe

                  • 6. Re: Find the first footnote of each page! …
                    Obi-wan Kenobi Adobe Community Professional

                    Uwe,

                     

                    Your code is truly amazing!

                    We can play it with a lot of Grep code!

                    I'm working on an UI: a double Grep F/R! First, we define the target and find it, then we define what we do with what we have found!! Cool!

                    If I success to finish it (??), I'll post it for everyone! [some on this forum say I never give anything!] 

                     

                    Thanks again (a lot)!

                     

                    [ Don't forget I'm a newbie!  ]

                     

                    FindReplaceFirstOnPage_UI.png

                    • 7. Re: Find the first footnote of each page! …
                      [Jongware] Most Valuable Participant

                      Uwe, why is that locking and unlocking required? Just wrote this quickie and it seems to work:

                       

                      var myDoc = app.activeDocument;
                      for (pg=0; pg<myDoc.pages.length; pg++)
                      {
                      for (tf=0; tf<myDoc.pages[pg].textFrames.length; tf++)
                      {
                        firstNote = myDoc.pages[pg].textFrames[tf].footnotes[0];
                        if (firstNote.isValid)
                        {
                        firstNote.parent.characters[firstNote.storyOffset.index].select();
                        app.layoutWindows[0].zoomPercentage = app.layoutWindows[0].zoomPercentage;
                        alert ("oi found one!");
                        }
                      }
                      }
                      

                       

                      Obi-wan: what if a change on a page makes the text run differently? This means you must restart your search on the next page, after making a change.

                      1 person found this helpful
                      • 8. Re: Find the first footnote of each page! …
                        Laubender Adobe Community Professional & MVP

                        Hi Jongware,

                        it's not required, but I am using this trick as an implicit shortcut to access deeper nested text frames (e.g. in states of MSOs that are not in the active state).

                         

                        Uwe

                        • 9. Re: Find the first footnote of each page! …
                          Obi-wan Kenobi Adobe Community Professional

                          Hi Jongware,

                           

                          I'll insert a "reset": if the layout has moved, the script will search the text with the condition [text modified], delete it, search "one" ~j and replace by 2!

                           

                          I know it's a dangerous way!

                           

                          … but can the script keep "in mind" the "initial" situation of each target?

                          • 10. Re: Find the first footnote of each page! …
                            Laubender Adobe Community Professional & MVP

                            Obi-wan Kenobi wrote:

                             

                            …I know it's a dangerous way!

                             

                            … but can the script keep "in mind" the "initial" situation of each target?

                             

                            Yes. You could store the properties of the already applied conditions into an array at the start.
                            Just storing the applied conditions maybe would not be enough, if you do changes to the conditions present in the document further down the road.

                             

                            Note, that I did not use text.appliedConditions = [ Array of Condition(s) ], but the method text.applyConditions( [ Array of Condition(s) ] ). That's a difference. Do some tests with assigning conditions vs. using the method. Then you'll see.

                             

                            Uwe

                            • 11. Re: Find the first footnote of each page! …
                              Obi-wan Kenobi Adobe Community Professional

                              Uwe,

                               

                              Not only the condition already applied but the changes done! So, not sure we can do it!

                               

                              In my mind, the idea is to use the script to make simple things!

                              Samples:

                              I make a header with the 3 first words on the 2nd para of each page! Doable! If the layout moves, easy to reset!

                              I want to "modify" each first footnote – especially: if the footnote is: "29. Ibid. p. 124.", referencing to an article of "The ID Best Scripters, May 2016" revue, above (on the previous page), the script'll convert ithe footnote to: 29. "The ID Best Scripters, May 2016. p. 124."

                              Doable [2 grep styles]! If the layout moves, easy to reset!