11 Replies Latest reply on Nov 1, 2010 12:43 PM by Fred Goldman

    [JS  CS3] Apply Italics with scripting

    Haakenlid Level 3

      There's a keyboard shortcut called "Apply Italics", which by default is [cmd]+[shift]+[i] on Mac, and  [ctrl]+[shift]+[i] on Windows. I use it often because it will change text to the correct italic style. For instance text with the style "85 Heavy" will change to "86 Heavy Italic" or "Semibold Display" to "Semibold Display Italic".

       

      Is there any way to access "Apply Italics" with scripting? The action is not available in any menu as far as I know.

        • 1. Re: [JS  CS3] Apply Italics with scripting
          [Jongware] Most Valuable Participant

          Not that I know of!

           

          It's very strange, and quite annoying at times. I'd love to see this added to the Scripting model ...

          1 person found this helpful
          • 2. Re: [JS  CS3] Apply Italics with scripting
            Peter Kahrel Adobe Community Professional & MVP

            Join the many people who have filed requests fot this here:

             

            http://www.adobe.com/support/feature.html

             

            Peter

            • 3. Re: [JS  CS3] Apply Italics with scripting
              Harbs. Level 6

              Yes it's possible.

               

              Normal:

              app.menuActions.itemByID(119609).invoke()

               

              Bold:

              app.menuActions.itemByID(119610).invoke()

               

              Italic:

              app.menuActions.itemByID(119611).invoke()

               

              Underline:

              app.menuActions.itemByID(119612).invoke()

               

              Except for normal these are all toggles...

               

              Harbs

              • 4. Re: [JS  CS3] Apply Italics with scripting
                Harbs. Level 6

                Just to clarify further:

                 

                If you want to make sure italic is applied you should use both the normal and italic action:

                 

                app.menuActions.itemByID(119609).invoke()

                app.menuActions.itemByID(119611).invoke()

                 

                Of course this will clear any other formatting as well...

                 

                Harbs

                1 person found this helpful
                • 5. Re: [JS  CS3] Apply Italics with scripting
                  [Jongware] Most Valuable Participant

                  Oooh tricky! You lil' rascal!

                  • 6. Re: [JS  CS3] Apply Italics with scripting
                    Haakenlid Level 3

                    Thanks a lot!

                     

                    Is it possible to invoke the action on a Paragraph object or only on app.selection ?

                    • 7. Re: [JS  CS3] Apply Italics with scripting
                      Haakenlid Level 3

                      I made this script which will change all text with the character style "I" into proper italics, and creates corresponding character styles. "I" is just a character style with no properties. It gets applied when I import content into InDesign.

                       

                      I will end up with styles called "I 37 Thin Condensed Oblique", "I Italic Display" and so on according to the various fonts.

                       

                      function applyItalic(){ // Finds all text with Character Style "I" applied in the current document and changes it to proper italic character styles
                           var mySelection = app.selection; 
                           var myDoc = app.activeDocument; 
                           var myRanges = myDoc.stories.everyItem().textStyleRanges.everyItem().getElements(); // gets every textStyleRange in the document;
                           var italicStyle = myDoc.characterStyles.itemByName ("I"); // this character styles has no attributes;
                           if (italicStyle == null){
                                return; 
                           }
                           var myRange;
                           var myFont;
                           var newStyle;
                           var newStyleName;
                           var n;
                           for (n=0; n<myRanges.length; n++){
                                myRange = myRanges[n];
                                if (myRange.appliedCharacterStyle==italicStyle){
                                     app.select(myRange);
                                     app.menuActions.itemByID(119611).invoke(); // apply italics - same as [cmd]+[shift]+[i]
                                     myFont=myRange.appliedFont;
                                     newStyleName="I "+myFont.fontStyleName;
                                     newStyle=myDoc.characterStyles.itemByName(newStyleName); // if the style is created
                                     if (newStyle==null){
                                          newStyle=myDoc.characterStyles.add({name: newStyleName, fontStyle: myFont.fontStyleName}); // if the style does not exist yet
                                     }
                                     myRange.appliedCharacterStyle=newStyle;
                                }
                           }
                           app.select(mySelection); // restores the original selection
                      }
                      applyItalic();
                      • 8. Re: [JS  CS3] Apply Italics with scripting
                        [Jongware] Most Valuable Participant

                        Thank you for posting a complete script!

                         

                        Given time, I might look into my own preptext script (which sort of does the reverse of yours but misses out on not-properly-called "Italic" italics).

                        • 9. Re: [JS  CS3] Apply Italics with scripting
                          Harbs. Level 6

                          Just FYI,

                           

                          Using arrays on Text objects is slower than collections. (Resolving text objects is way more expensive than page items because of all the smoke an mirrors behind the scenes...)

                           

                          You should drop the "everyItem().getElements()"...

                           

                          Harbs

                          • 10. Re: [JS  CS3] Apply Italics with scripting
                            Fred Goldman Level 3

                            Harbs,

                             

                            Are you sure about that? From the testing that I've done I have found everyItem().getElements() to be much faster even on text elements (i.e. textStyleRanges, paragraphs etc.).

                            • 11. Re: [JS  CS3] Apply Italics with scripting
                              Harbs. Level 6

                              I haven't tested in a while, but I definitely saw significantly longer times in CS2 and CS3.

                               

                              Harbs