4 Replies Latest reply on Feb 4, 2009 5:37 PM by dhishok

    Find some characters and replace with character style...

    dhishok Level 1
      Hi everyone,

      The below script is to find some space characters and replacing with character style. Its working fine with InDesignCS2 MAC.

      //InDesign CS2//

      var myDoc = app.activeDocument;
      with(app.activeDocument){
      myNewCharStyle = characterStyles.add();
      with(myNewCharStyle){
      name= "x"
      fontStyle = "Roman"
      }}

      app.findPreferences = null;
      app.changePreferences = null;

      myDoc.search("^>", false, false, undefined, undefined, {appliedCharacterStyle:"x"});
      myDoc.search("^m", false, false, undefined, undefined, {appliedCharacterStyle:"x"});

      ////////

      Now i am currently working in InDesignCS3, and i have also changed the script somewhat but its not working with CS3 MAC...

      //InDesign CS3//

      var myDoc = app.activeDocument;
      if (myDoc.characterStyles.item ('x') == null)
      myDoc.characterStyles.add ({name: 'x'});
      var myNewCharStyle = myDoc.characterStyles.item ('x');

      with(myNewCharStyle){
      name= "x"
      fontStyle = "Roman"
      }

      function setupFindText(myFindTextPreferences, myChangeTextPreferences, myNewCharStyle) {
      app.findTextPreferences = null;
      app.changeTextPreferences = null;
      app.findTextPreferences.findWhat = "^>";
      app.changeTextPreferences.appliedCharacterStyle = myNewCharStyle;
      app.findTextPreferences.findWhat = "^m";
      app.changeTextPreferences.appliedCharacterStyle = myNewCharStyle;

      //

      can anyone help me for this. Thanks in advance.

      Regards
      Thiyagu
        • 1. Re: Find some characters and replace with character style...
          Level 1
          Hi Thiyagu,

          You're not telling InDesign CS3 to do anything here:

          app.findTextPreferences.findWhat = "^>";
          
          app.changeTextPreferences.appliedCharacterStyle = myNewCharStyle;
          app.findTextPreferences.findWhat = "^m";
          app.changeTextPreferences.appliedCharacterStyle = myNewCharStyle;

          You've set the find and change text preferences, but you haven't issued a command to change the text. What you need looks something more like this:

          app.findTextPreferences.findWhat = "^>";
          
          app.changeTextPreferences.appliedCharacterStyle = myNewCharStyle;
          //Add a line to perform the change:
          app.documents.item(0).changeText();
          app.findTextPreferences.findWhat = "^m";
          app.changeTextPreferences.appliedCharacterStyle = myNewCharStyle;
          //Add a line to perform the change:
          app.documents.item(0).changeText();

          It's also not clear to me that this will work:

          if (myDoc.characterStyles.item ('x') == null)
          

          I think that this will always be true, even if the style does not exist.

          Thanks,

          Ole
          • 2. Re: Find some characters and replace with character style...
            dhishok Level 1
            Hi Ole,

            Thanks for your kind support. Actually what i need is, find en space & em space characters in the InDesign CS3 document and apply character style for those characters. I have tested your script also its running but not performing the change action. Please look into this and help for this.

            Regards
            Thiyagu
            • 3. Re: Find some characters and replace with character style...
              Level 1
              Hi Thiyagu,

              If it's not working, it's most likely that the character style does not exist, or, if it does exist, it's inside a style group. Could that be the problem?

              Thanks,

              Ole
              • 4. Re: Find some characters and replace with character style...
                dhishok Level 1
                Hi Ole,

                What you said is correct. I am creating the character style but it is not inside the style group. Then finding those characters and apply character style for it. Unfortunately the find & change process defined as a function, then only it doesn't run properly. Now below script is working fine.

                ===============
                var myDoc = app.activeDocument;
                with(app.activeDocument){
                myNewCharStyle = characterStyles.add();
                with(myNewCharStyle){
                name= "x"
                fontStyle = "Roman"
                }}

                app.findTextPreferences.findWhat = "^>";
                app.changeTextPreferences.appliedCharacterStyle = myNewCharStyle;
                //Add a line to perform the change:
                app.documents.item(0).changeText();
                app.findTextPreferences.findWhat = "^m";
                app.changeTextPreferences.appliedCharacterStyle = myNewCharStyle;
                //Add a line to perform the change:
                app.documents.item(0).changeText();

                ==============

                Thanks for your kind support.

                Regards
                Thiyagu