12 Replies Latest reply on Feb 1, 2016 10:09 AM by pixxxel schubser

    Formatting Javascript code

    Chuck Uebele Adobe Community Professional & MVP

      Is there an easy way to format javascript code in an ID document much like this forum formats the code below? I'm thinking of putting together a guide on scripting and would like to use live text, but have it formatted rather than screen shots of ESTK.

       

      var doc = activeDocument;
      var base = doc.activeLayer;
      
      
      for(var i = 0;i>-11;i--){
          doc.activeLayer = base;
          
          var idCpTL = charIDToTypeID( "CpTL" );
          executeAction( idCpTL, undefined, DialogModes.NO );
          
          desat (i*10)
      
          }
      
      function desat(val){
          var idvibrance = stringIDToTypeID( "vibrance" );
              var desc4 = new ActionDescriptor();
              var idStrt = charIDToTypeID( "Strt" );
              desc4.putInteger( idStrt, val );
          executeAction( idvibrance, desc4, DialogModes.NO );
      }
      
        • 1. Re: Formatting Javascript code
          Test Screen Name Most Valuable Participant

          A useful buzzword may be "prettyprinting" ( one word).

          1 person found this helpful
          • 3. Re: Formatting Javascript code
            Peter Spier Most Valuable Participant (Moderator)

            Line numbers might be a problem without a script (I think there's one at ID-Extras.com) unless every line ends with a hard return and you make it a numbered list, but the formatting itself is just a matter of defining a paragraph style for code.

            1 person found this helpful
            • 4. Re: Formatting Javascript code
              Obi-wan Kenobi Adobe Community Professional

              Hi Peter,

               

              About this point, not sure (… but it's a end of WE!  ;-) )!

              A para style, 2 char styles A and B (of course Grey/White underlined), 2 nested line styles, 2 regex (XB is the same than XA with Char style B) and a small anchored text bloc with an auto numbering.

               

              Capture d’écran 2016-01-31 à 21.12.28.png

              1 person found this helpful
              • 5. Re: Formatting Javascript code
                Chuck Uebele Adobe Community Professional & MVP

                Thanks, Peter, I was think along the same lines. The lines would all have hard returns, so that would be one way of creating the line numbers. Other formatting might be more trouble than it's worth. I was just hoping that ID might have some built-in preset for syntax highlighting, but it's not looking too favorable in that regard.

                 

                Thanks, Obi, for the screen shot. That gives me some more ideas.

                • 6. Re: Formatting Javascript code
                  Peter Spier Most Valuable Participant (Moderator)

                  Pretty easy to add GREP styles to format reserved words or strings in quotes if you want to...

                   

                  And if you want alternating shading, you could use two paragraph styles set to alternate with each other as Next Style. The Numbers could have a custom underline to change the color as part of the character style assigned, and the divider could be handled the same way if you're willing to enter some whitespace character at the beginning of each line (Find/Change?).

                  1 person found this helpful
                  • 7. Re: Formatting Javascript code
                    Obi-wan Kenobi Adobe Community Professional

                    Peter,

                     

                    As we have simple 1-line paras, it could be done very easily adding a tab at the beginning of each para and converting all text in a table (including an auto-numbering first column)!

                     

                     

                    1 person found this helpful
                    • 8. Re: Formatting Javascript code
                      pixxxel schubser Level 5

                      Hi Chuck Uebele,

                      you can simple use 2 paragraph styles.

                      code_Formatting_1.png

                      code_Formatting_2.png

                      code_Formatting_3.png

                      code_Formatting_4.png

                       

                      Have fun

                       

                      1 person found this helpful
                      • 9. Re: Formatting Javascript code
                        [Jongware] Most Valuable Participant

                        When using GREP styles, make sure the Single Line comment one is at the very bottom. This prevents reserved words, strings, numbers, and other 'syntax coloured' items in commented-out code and regular comments from highlighting. (Well not 'prevent', the last GREP style in a list will just overwrite whatever comes above it.)

                         

                        Note that GREP styles will not work with multi-line comments, unless you use shift-returns throughout (which I actually would not recommend, as it is bad for a number of reasons).

                         

                        Additional tips: - make sure Typographic Quotes are off when placing or typing your scripts; make sure ligatures are off as well! (This depends on what font you use, but there are monospaced fonts out there that contain "fi" and "fl" ligatures; and unless told otherwise, InDesign will happily apply them).

                        1 person found this helpful
                        • 10. Re: Formatting Javascript code
                          Chuck Uebele Adobe Community Professional & MVP

                          Thanks everyone for all your great advise!

                          • 11. Re: Formatting Javascript code
                            Obi-wan Kenobi Adobe Community Professional

                            Hi Pixxxel,

                             

                            You're totally right! Cooler way! Play with 2 para rules and 1 shading is a clever approach.

                            So just apply one para style to all text then play a simple regex as: (^.+\r)\K(?1) to apply the 2nd para style! 

                            • 12. Re: Formatting Javascript code
                              pixxxel schubser Level 5

                              Hi Obi-wan Kenobi,

                              we don't need the sandbox today.

                               

                              I like Grep, but in this case Grep is not needed.

                              Select the whole text and [Strg]+rightclick at the name of paragraphstyle "linie1" in the palette and than click: use "linie1" and next style.

                               

                              And there is no shading in use. The document was made with ID CS5.0

                              I create a gradient with 95% grey and 5% green (to see at the top of the document) for the background of the numbering which is used in paragraph rules. That's why I have one limit: the style works live as „selfformatting“ with every single line paragraphs – but only in text frames with the same width. Otherwise you have to change the gradient for the new text frame width. And like [Jongware] said: only single line comments are possible (to formatting multi line comments, this is only possible for the first or the last line)

                               

                              Kindly regards