9 Replies Latest reply on Aug 11, 2016 4:14 AM by Obi-wan Kenobi

    Cross-reference paragraph number! …

    Obi-wan Kenobi Adobe Community Professional

      Hi all,

       

      Some issues with that code:

       

      var myDoc = app.activeDocument;
      
      
      app.findGrepPreferences = app.changeGrepPreferences = null;  
      app.findGrepPreferences.appliedParagraphStyle = "P1 - Blue";  
        myFound = app.findGrep();  
        
      for (i = 0; i < myFound.length; i++) {
      myCRFormat = myDoc.crossReferenceFormats.item ('Paragraph Number 1');  
      myTargetParagraph = myFound[i];  
      myReference = myTargetParagraph.insertionPoints[-1];
      
      
      Target = myDoc.paragraphDestinations.add (myTargetParagraph);  
      Ref = myDoc.crossReferenceSources.add (myReference, myCRFormat);  
      myDoc.hyperlinks.add (Ref, Target); 
      }
      

       

      I would like to insert a CR at the end of all the paras with "P1 - Blue" para style applied! 

       

      As you see, It works but something weird happens: the CR is placed with "incrementation"! 

       

      Capture d’écran 2016-08-06 à 12.44.04.png

       

      Any deep though will be appreciated! 

      Thanks in advance!

        • 1. Re: Cross-reference paragraph number! …
          Peter Kahrel Adobe Community Professional & MVP

          You found your paragraphs, but then when you add CRs, you mess up the paragraphs: the indexes go all over the place. Probably iterating from end to start through the paragraphs will solve it:

           

          for (var i < myFound.length-1; i >= 0; i--) { 

           

          Peter

          • 2. Re: Cross-reference paragraph number! …
            Obi-wan Kenobi Adobe Community Professional

            Hi Peter,

             

            I'm going to test this! Thanks! 

             

            "Bonus" question: How could I do to insert the CR at the beginning of the paras?

             

            Best regards!

            • 3. Re: Cross-reference paragraph number! …
              Peter Kahrel Adobe Community Professional & MVP

              Maybe something like this:

               

              myTargetParagraph.insertionPoints[0].contents = '\r';

               

              P.

              • 4. Re: Cross-reference paragraph number! …
                Obi-wan Kenobi Adobe Community Professional

                Hi Peter,

                 

                It doesn't work! … And you're right:  The Cross-References insertion creates a "beautiful mess"! 

                 

                ... But I had an idea:

                Since only the first insertion runs correctly, I "loop on a first insertion", that is to say, my script searches every occurrence of the "P1 - Blue" paragraph style [myFound_0 ], then it launches a new research, find: ^[^~j], which gives me "myFound_1" and the script only treats its first occurrence "myFound_1 [0]".

                As it inserts a CR whose format is delimited by two non-joiners [ "Paragraph Number" format = ^j<paraNum />^&gt;^8^&gt;^j ], the new search (loop) won't return the first treated and will directly jump to the next, and so on! …

                 

                Really twisted [as me!] but it works! Surely a better and more "orthodox" solution! 

                [don't forget I'm a really [JS] newbie! ]

                 

                The only things to finally do is create:

                • the "1 Invisible" char style [to make the CR invisible];

                • the Cross-Reference format [to catch the "P1 - Blue" para style auto-numbering];

                • the Header variable "P1 - Blue".

                 

                So, in 1 click, you can create complete auto-numbering headers!  Cool! 

                 

                Capture d’écran 2016-08-07 à 11.11.40.png

                 

                Capture d’écran 2016-08-07 à 11.11.55.png

                 

                Capture d’écran 2016-08-07 à 12.20.29.png

                 

                var myDoc = app.activeDocument;
                var myStyle = "P1 - Blue";
                
                
                app.findTextPreferences = app.changeTextPreferences = null;
                
                
                app.findTextPreferences.findWhat = "<FEFF>";
                app.findTextPreferences.appliedParagraphStyle = myStyle;  
                app.changeTextPreferences.changeTo = "";
                myDoc.changeText();
                
                
                app.findGrepPreferences = app.changeGrepPreferences = null;
                app.findGrepPreferences.findWhat = "~j[^~j]+~j";
                app.findGrepPreferences.appliedParagraphStyle = myStyle;  
                app.changeGrepPreferences.changeTo = "";
                myDoc.changeGrep();
                
                
                app.findGrepPreferences = app.changeGrepPreferences = null;
                app.findGrepPreferences.appliedParagraphStyle = myStyle;  
                myFound_0 = app.findGrep();
                
                
                for (i = 0; i < myFound_0.length; i++) {
                app.findGrepPreferences = app.changeGrepPreferences = null;
                app.findGrepPreferences.findWhat = "^[^~j]";
                app.findGrepPreferences.appliedParagraphStyle = myStyle;  
                myFound_1 = app.findGrep();
                
                
                myTargetParagraph = myFound_1[0];  
                myCRFormat = myDoc.crossReferenceFormats.item ('Paragraph Number');  
                myReference = myTargetParagraph.insertionPoints[-1];
                Target = myDoc.paragraphDestinations.add (myTargetParagraph);  
                Ref = myDoc.crossReferenceSources.add (myReference, myCRFormat);  
                myDoc.hyperlinks.add (Ref, Target); 
                }
                
                
                • 5. Re: Cross-reference paragraph number! …
                  Obi-wan Kenobi Adobe Community Professional

                  I forgot! To "refresh": Ctrl-B

                  It's the shortcut I've assigned to this script:

                   

                  app.activeDocument.spreads.everyItem().textFrames.everyItem().move(undefined, [5, 0]);
                  app.activeDocument.spreads.everyItem().textFrames.everyItem().move(undefined, [-5, 0]);
                  

                   

                   

                  Cool! 

                  • 6. Re: Cross-reference paragraph number! …
                    Trevorׅ Adobe Community Professional

                    Hi Obi

                     

                    I was surprised to see that you marked a question you asked as answered when you so much hate to do so.

                    After seeing you marked your own answer as correct and not someone else I was not so surprised.

                     

                    Peter answered your question (both of them) correctly. Show a bit of appreciation and mark his answer as correct.

                    I know you wrote you are twisted but it's not too late to change.

                     

                    Regards

                     

                    Trevor

                    • 7. Re: Cross-reference paragraph number! …
                      Obi-wan Kenobi Adobe Community Professional

                      Hi Trevor,

                       

                      A little sorry if I've not marked as correct an answer you gave me by the past!

                      I don't think this situation is very important even if it's really helpful to do it!

                      About my decision to mark my answer as correct, it was more to alert you [the experts!]! I'm a [JS] newbie but I'm learning quickly! If there's a (surely) better way to code my idea, I'll totally appreciate comments, especially from you!  ;-)

                      About Peter, he totally knows how much I respect him and study all his comments with a great interest!

                       

                       

                      • 8. Re: Cross-reference paragraph number! …
                        Kai Rübsamen Adobe Community Professional

                        Michel, I read your commentary with head shake. To mark your own comments as correct, is no good forum practice!

                        • 9. Re: Cross-reference paragraph number! …
                          Obi-wan Kenobi Adobe Community Professional

                          … So! Is this better?

                           

                          "Hi all,

                          Waiting for someone who could give you an answer that I eventually could mark as "correct", you could take a look at my post#4 where you could find something that could work! At your own risks! ;-)"