4 Replies Latest reply on Jun 10, 2012 7:25 PM by rick_schwein

    Toggle font style

    rick_schwein Level 1

      I'm pretty new to Javascript and InDesign scripting, so following this forum has been very helpful to me.

       

      The newspaper company I work for uses InDesign and InCopy as the production tools for our CMS. We have two problems: 1) users want to be able to use Ctrl-B and Ctrl-I to toggle in and out of bold and italic text, and 2) we need to be able to pass character styles of bold and italic to our Web CMS. 

      It looks like we can fix that with a combination of character styles (affecting only the bold or italic font style) and javascripts mapped to keyboard shortcuts.

       

      The basic script follows.  Does anyone see any pitfalls?  Also, is there a way to pass an argument along with the script name through a keyboard shortcut (so the script could be called with the value 'Bold' or 'Italic' instead of having a copy for each style needed?)

       

      Thanks!

       

      // toggle character style

      // requires character style name to match script name: Bold, Italic, etc.

      // checks applied FONT style, then applies CHARACTER style or clears both char and font styles.

      //  written 6/8/12, Rick Schwein, Philadelphia Media Network

        var myStyle = 'Bold';

      //Make certain that user interaction (display of dialogs, etc.) is turned on.

      app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;

      if(app.documents.length > 0){

          if(app.selection.length > 0){

              switch(app.selection[0].constructor.name){

                  case "InsertionPoint":

                  case "Character":

                  case "Word":

                  case "Line":

                  case "Paragraph":

                  case "Text":

                  case "TextColumn":

                  case "TextStyleRange":

                      myToggleStyle(app.selection[0]);

                      break;

                  default:

              }

          }

      }     // do nothing if these conditions not met

       

      function myToggleStyle(mySelection){

          var myFStyle = app.selection[0];

          if (myFStyle.fontStyle != myStyle ){  //check the current font style to see what char style needed

              try {

                  app.selection[0].appliedCharacterStyle = myStyle;

              }

              catch (e) { alert(myStyle + " character style is not defined"); }

          } else {

      // more elaborate styling combinations need more careful treatment here

              app.selection[0].fontStyle = '';  //resets to either normal, roman or regular

      // too bad i can't just say app.selection[0].appliedCharacterStyle = '[None]' ...

               app.selection[0].appliedCharacterStyle = app.documents.item(0).characterStyles.item(0);

          }

      }   

        • 1. Re: Toggle font style
          Dave Saunders Level 4

          Random thoughts in the order I noticed them:

           

          That long-winded switch to determine if the selection is text is not necessary. Just use:

           

          if (app.selection[0].hasOwnProperty("baseline")) { // is selection text?

           

          Ole uses that switch into the samples because he needed to use techniques that work in all three languages. The hasOwnProperty method is a JavaScript special.

           

          Why pass "mySelection" into your function if you then don't use it.

           

          The values of the properties returned to your script when the selection is a range of text are those of the first character. They might be all the same, but you might have to dig into the selection and process each TextStyleRange within it.

           

          Dave

          1 person found this helpful
          • 3. Re: Toggle font style
            Dave Saunders Level 4

            Thanks Harbs. That approach certainly works on the desktop and makes the process of what to do to the selection a lot easier. Still, you have to be careful to examine the selection properly so you don't give the whole selection blanket treatment based on the settings of the first character.

             

            Dave

            • 4. Re: Toggle font style
              rick_schwein Level 1

              Harbs, I had not seen the posting at 3239392#3239392

               

              I'd use this solution in a minute (it appeals to me on the basis of fewer lines of code is always better), except that my implementation needs to apply a character style so it gets exported to our website.

               

              Thanks!