9 Replies Latest reply on Jun 4, 2015 9:29 AM by zalmanf

    Updating Dave Saunders find style/change case script

    ashleeadobe1234

      How can I update a Dave Saunders script from CS1 and CS2 to CS5?

       

      he wrote a brilliant script which searches for a paragraph style and changes the case to upper or lower.

       

      However, it was for CS1 and CS2.   I can 't get it to run on CS5.

      it trips up on

       

        app.findPreferences = null;
      object does not support 'findPreferences'

       

      here is the script.  thanks for your help!

       

      //DESCRIPTION: Converts text in designated parastyle to designated case

       

      if ((app.documents.length != 0) && (app.selection.length != 0)) {
      myDoc = app.activeDocument;
      myStyles = myDoc.paragraphStyles;
      myStringList = myStyles.everyItem().name;
      myCaseList = ["Uppercase","Lowercase", "Title case", "Sentence case"];
      myCases = [ChangecaseMode.uppercase, ChangecaseMode.lowercase, ChangecaseMode.titlecase, ChangecaseMode.sentencecase];

       

      var myDialog = app.dialogs.add({name:"Case Changer"})
      with(myDialog){
        with(dialogColumns.add()){
         with (dialogRows.add()) {
          with (dialogColumns.add()) {
           staticTexts.add({staticLabel:"Paragraph Style:"});
          }
          with (dialogColumns.add()) {
           myStyle = dropdowns.add({stringList:myStringList,selectedIndex:0,minWidth:133});
          }
         }
         with (dialogRows.add()) {
          with (dialogColumns.add()) {
           staticTexts.add({staticLabel:"Change Case to:"});
          }
          with (dialogColumns.add()) {
           myCase = dropdowns.add({stringList:myCaseList,selectedIndex:0,minWidth:133});
          }
         }
        }
      }
      var myResult = myDialog.show();
      if (myResult != true){
        // user clicked Cancel
        myDialog.destroy();
        errorExit();
      }
        theStyle = myStyle.selectedIndex;
        theCase = myCase.selectedIndex;
        myDialog.destroy();

       

        app.findPreferences = null;
        app.changePreferences = null;
        myFinds = myDoc.search('',false,false,undefined,{appliedParagraphStyle:myStyles[theStyle]});
        myLim = myFinds.length;
        for (var j=0; myLim > j; j++) {
         myFinds[j].texts[0].changecase(myCases[theCase]);
        }

       

      } else {
      errorExit();
      }

       

      // +++++++ Functions Start Here +++++++++++++++++++++++

       

      function errorExit(message) {
      if (arguments.length > 0) {
        if (app.version != 3) { beep() } // CS2 includes beep() function.
        alert(message);
      }
      exit(); // CS exits with a beep; CS2 exits silently.
      }

        • 1. Re: Updating Dave Saunders find style/change case script
          [Jongware] Most Valuable Participant

          Did you try this?

           

          http://indesignsecrets.com/using-old-scripts-in-cs3.php

           

          -- it mentions "CS3" in the title but the same trick also works for newer versions.

          • 2. Re: Updating Dave Saunders find style/change case script
            Manan Joshi Level 4

            The "findPreferences" method does not exist on the app object in CS5

             

            Instead of this you  can use "app.changeTextPreferences" for specifying the search parameter that will be used for searching and replacing in the "doc.changeText()" method. If you only want to search and not replace you can use "app.findTextPreferences" and "doc.findText()".

             

            Since you are searching for the paragraph style you need to use the text preferences for searching and replacing.

             

            The basic steps you could use for this operation would be

             

            1. Clear the find/change text preferences.

             

            2.  Set the find options

             

            3.  Perform the search-and-replace operation

             

            Manan Joshi

            www.metadesignsolutions.com

            • 3. Re: Updating Dave Saunders find style/change case script
              Haakenlid Level 3

              Change this:

               

               app.findPreferences = null;
               app.changePreferences = null;
               myFinds = myDoc.search('',false,false,undefined,{appliedParagraphStyle:myStyles [theStyle]});
              

               

              To this:

               

              app.findTextPreferences = NothingEnum.NOTHING;
              app.changeTextPreferences = NothingEnum.NOTHING;
              app.findTextPreferences.appliedParagraphStyle = myStyles [theStyle];
              var myFinds = myDoc.findText();
              
              • 4. Re: Updating Dave Saunders find style/change case script
                seddybell

                I have been wanting this script to work for ages as well, but I just started studying javascript in InDesign several weeks ago reading "Scripting InDesign CS3/4 with Javascript" by Peter Kahrel. Even though it says CS3/4, it does go into CS5. Not sure if Peter updated it recently or not.

                 

                Anyway, thanks to that and Haakenlid and Manan's help, I was able to update the script and get it to work successfully. I had gotten as far as changing "app.findPreferences = null;" to "app.findTextPreferencess = NothingEnum.NOTHING;", but I was stuck on line 43 (myFinds = myDoc.search...). I had to change one thing in Haakenlid's code: "appliedParagraphStyle" should be "appliedCharacterStyle". Without further ado, here is the fixed script:

                 

                //DESCRIPTION: Update of Dave Saunder's ChangeCaseOfSelectedStyle script for CS5 and later

                 

                if ((app.documents.length != 0) && (app.selection.length != 0)) {

                myDoc = app.activeDocument;

                myStyles = myDoc.characterStyles;

                myStringList = myStyles.everyItem().name;

                myCaseList = ["Uppercase","Lowercase", "Title case", "Sentence case"];

                myCases = [ChangecaseMode.uppercase, ChangecaseMode.lowercase, ChangecaseMode.titlecase, ChangecaseMode.sentencecase];

                 

                var myDialog = app.dialogs.add({name:"Case Changer"})

                with(myDialog){

                  with(dialogColumns.add()){

                   with (dialogRows.add()) {

                    with (dialogColumns.add()) {

                     staticTexts.add({staticLabel:"Character Style:"});

                    }

                    with (dialogColumns.add()) {

                     myStyle = dropdowns.add({stringList:myStringList,selectedIndex:0,minWidth:133});

                    }

                   }

                   with (dialogRows.add()) {

                    with (dialogColumns.add()) {

                     staticTexts.add({staticLabel:"Change Case to:"});

                    }

                    with (dialogColumns.add()) {

                     myCase = dropdowns.add({stringList:myCaseList,selectedIndex:0,minWidth:133});

                    }

                   }

                  }

                }

                var myResult = myDialog.show();

                if (myResult != true){

                  // user clicked Cancel

                  myDialog.destroy();

                  errorExit();

                }

                  theStyle = myStyle.selectedIndex;

                  theCase = myCase.selectedIndex;

                  myDialog.destroy();

                 

                 

                  app.findTextPreferences = NothingEnum.NOTHING;

                  app.changeTextPreferences = NothingEnum.NOTHING;

                  app.findTextPreferences.appliedCharacterStyle = myStyles [theStyle]; var myFinds = myDoc.findText();

                  myLim = myFinds.length;

                  for (var j=0; myLim > j; j++) {

                   myFinds[j].texts[0].changecase(myCases[theCase]);

                  }

                 

                 

                } else {

                errorExit();

                }

                 

                 

                // +++++++ Functions Start Here +++++++++++++++++++++++

                 

                 

                function errorExit(message) {

                if (arguments.length > 0) {

                  if (app.version != 3) { beep() }

                  alert(message);

                }

                exit();

                }

                • 5. Re: Updating Dave Saunders find style/change case script
                  ashleeadobe1234 Level 1

                  thanks folks. seddybell's fix did the trick. much appreciated!

                  • 6. Re: Updating Dave Saunders find style/change case script
                    meluseene

                    Hello ^^

                    This new version was great to convert existing text in InDesign.

                     

                    I wonder if it's possible to use it directly in a style, so that in the future, anything I type in that particular style always appears in lowercase?

                    • 7. Re: Updating Dave Saunders find style/change case script
                      jbl0ndie

                      I've adapted seddybell's new script so that it works with paragraph styles instead of character styles. Hope that's useful to someone.

                       

                       

                      //DESCRIPTION: Update of Dave Saunder's ChangeCaseOfSelectedStyle script for CS5 and later

                      //jblondie: Fork of seddybell's update so that you can select by Paragraph Style instead of Character Style.

                       

                      if ((app.documents.length != 0) && (app.selection.length != 0)) {

                      myDoc = app.activeDocument;

                      myStyles = myDoc.paragraphStyles; //jblondie: replaced character with paragraph

                      myStringList = myStyles.everyItem().name;

                      myCaseList = ["UPPERCASE","lowercase", "Title Case", "Sentence case"]; //jblondie: Changed myCaseList so it's consistent with InDesign's built in Change Case tool

                      myCases = [ChangecaseMode.uppercase, ChangecaseMode.lowercase, ChangecaseMode.titlecase, ChangecaseMode.sentencecase];

                       

                      var myDialog = app.dialogs.add({name:"Case Changer"})

                      with(myDialog){

                        with(dialogColumns.add()){

                         with (dialogRows.add()) {

                          with (dialogColumns.add()) {

                           staticTexts.add({staticLabel:"Paragraph Style:"}); //jblondie: replaced Character with Paragraph

                          }

                          with (dialogColumns.add()) {

                           myStyle = dropdowns.add({stringList:myStringList,selectedIndex:0,minWidth:133}) ;

                          }

                         }

                         with (dialogRows.add()) {

                          with (dialogColumns.add()) {

                           staticTexts.add({staticLabel:"Change Case to:"});

                          }

                          with (dialogColumns.add()) {

                           myCase = dropdowns.add({stringList:myCaseList,selectedIndex:0,minWidth:133});

                          }

                         }

                        }

                      }

                      var myResult = myDialog.show();

                      if (myResult != true){

                        // user clicked Cancel

                        myDialog.destroy();

                        errorExit();

                      }

                        theStyle = myStyle.selectedIndex;

                        theCase = myCase.selectedIndex;

                        myDialog.destroy();

                       

                       

                        app.findTextPreferences = NothingEnum.NOTHING;

                        app.changeTextPreferences = NothingEnum.NOTHING;

                        app.findTextPreferences.appliedParagraphStyle = myStyles [theStyle]; var myFinds = myDoc.findText(); //jblondie: replaced Character with Paragraph

                        myLim = myFinds.length;

                        for (var j=0; myLim > j; j++) {

                         myFinds[j].texts[0].changecase(myCases[theCase]);

                        }

                       

                       

                      } else {

                      errorExit();

                      }

                       

                       

                      // +++++++ Functions Start Here +++++++++++++++++++++++

                       

                       

                      function errorExit(message) {

                      if (arguments.length > 0) {

                        if (app.version != 3) { beep() }

                        alert(message);

                      }

                      exit();

                      }

                      • 8. Re: Updating Dave Saunders find style/change case script
                        RobertKyle Level 2

                        meluseene,

                         

                        I think the answer is "kind of"

                         

                        This will change a style so that everything you type in the future will appear in uppercase:

                         

                        app.activeDocument.paragraphStyles.item("StylenameHere").capitalization = Capitalization.ALL_CAPS;

                         

                        (Of course, you can always edit the style to the same thing).

                         

                        But the opposite of ALL_CAPS is NORMAL, not lowercase. NORMAL means that newly typed text will appear as it is typed. If the shift key is up, it will be lowercase.

                         

                        Once you have type in a text frame, you can do something like this:

                         

                        someTextObject.changecase(ChangecaseMode.LOWERCASE);

                         

                        which lowercases the underlying characters. If the style is enforcing ALL_CAPS, you won't see any difference unless you also change the styling:

                         

                        someTextObject.capitalization=Capitalization.NORMAL;

                         

                        or maybe

                         

                        someTextObject.appliedParagraphStyle.capitalization=Capitalization.NORMAL;

                         

                        if you want to change the underlying style for all instances.

                         

                        But I don't think there is a way to always get lowercase in the future...

                        • 9. Re: Updating Dave Saunders find style/change case script
                          zalmanf

                          Hi All, thanks for updating this script.

                           

                          I updated the script to include the option of BOTH character and paragraph styles to choose from, this way you only need one script.

                           

                          I did not fully test it, just tested the paragraph option (which i needed). If you find any issues, post back here.

                           

                          //DESCRIPTION: Update of Dave Saunder's ChangeCaseOfSelectedStyle script to replace either Paragraph and Character Styles

                           

                          if ((app.documents.length != 0) && (app.selection.length != 0)) {

                          myDoc = app.activeDocument;

                          myCStyles = myDoc.characterStyles;

                          myCStringList = myCStyles.everyItem().name;

                          myPStyles = myDoc.paragraphStyles;

                          myPStringList = myPStyles.everyItem().name;

                          myCaseList = ["Uppercase","Lowercase", "Title case", "Sentence case"];

                          myCases = [ChangecaseMode.uppercase, ChangecaseMode.lowercase, ChangecaseMode.titlecase, ChangecaseMode.sentencecase];

                           

                          var myDialog = app.dialogs.add({name:"Case Changer"})

                          with(myDialog){

                            with(dialogColumns.add()){

                             with (dialogRows.add()) {

                              with (dialogColumns.add()) {

                               staticTexts.add({staticLabel:"Character Style:"});

                              }

                              with (dialogColumns.add()) {

                               myCStyle = dropdowns.add({stringList:myCStringList,selectedIndex:0,minWidth:133});

                              }

                             }

                             with (dialogRows.add()) {

                              with (dialogColumns.add()) {

                               staticTexts.add({staticLabel:"Paragraph Style:"});

                              }

                              with (dialogColumns.add()) {

                               myPStyle = dropdowns.add({stringList:myPStringList,selectedIndex:0,minWidth:133});

                              }

                             }

                             with (dialogRows.add()) {

                              with (dialogColumns.add()) {

                               staticTexts.add({staticLabel:"Change Case to:"});

                              }

                              with (dialogColumns.add()) {

                               myCase = dropdowns.add({stringList:myCaseList,selectedIndex:0,minWidth:133});

                              }

                             }

                            }

                          }

                          var myResult = myDialog.show();

                          if (myResult != true){

                            // user clicked Cancel

                            myDialog.destroy();

                            errorExit();

                          }

                            theCStyle = myCStyle.selectedIndex;

                            thePStyle = myPStyle.selectedIndex;

                            theCase = myCase.selectedIndex;

                            myDialog.destroy();

                           

                           

                            app.findTextPreferences = NothingEnum.NOTHING;

                            app.changeTextPreferences = NothingEnum.NOTHING;

                            if (theCStyle > 0) {

                            app.findTextPreferences.appliedCharacterStyle = myCStyles [theCStyle];

                            }

                            if (thePStyle > 0) {

                             app.findTextPreferences.appliedParagraphStyle = myPStyles [thePStyle];

                            }

                            var myFinds = myDoc.findText();

                            myLim = myFinds.length;

                            for (var j=0; myLim > j; j++) {

                             myFinds[j].texts[0].changecase(myCases[theCase]);

                            }

                           

                           

                          } else {

                          errorExit();

                          }

                           

                           

                          // +++++++ Functions Start Here +++++++++++++++++++++++

                           

                           

                          function errorExit(message) {

                          if (arguments.length > 0) {

                            if (app.version != 3) { beep() }

                            alert(message);

                          }

                          exit();

                          }