4 Replies Latest reply on Apr 13, 2009 1:22 AM by dhishok

    Apply Clear Overrides after creating such character styles...

    dhishok Level 1

      Hi everyone,

       

      After importing the Word document into InDesign CS3 in some of the paragraphs there is some style overrides. The InDesign styles formatting are not intented with the paragraphs properly. Eventhough the word styles are mapped with InDesign styles exactly. If i select all the paragrahs and click the option "Clear Overrides" from the paragraph styles palette, all the InDesign styles formatting are applied properly but the character style formatting get removed. So i have planned to create script to character style for each font style and each formatting of the characters. After doing this i can clear the overrides of paragraph styles without hassle. I have created the script to create character styles for character formatting and its working fine but when i tried to run this script for 2nd time in the same document it showing some error in it. Please find my script below and it is too big,

       

      ------------------------------------------------------------------------------------------ -------------------

      //To find Font styles and character formatting and replacing it with character styles
      //Script for CS3

       

      #target InDesign
      var myDoc = app.activeDocument;
      if (myDoc.characterStyles.item("Itl") == null){
      var Itl = myDoc.characterStyles.add({name: "Itl", fontStyle: "Italic"});}
      if (myDoc.characterStyles.item("bl") == null){
      var bl = myDoc.characterStyles.add({name: "bl", fontStyle: "Bold"});}
      if (myDoc.characterStyles.item("bItl") == null){
      var bItl = myDoc.characterStyles.add({name: "bItl", fontStyle: "BoldItalic"});}
      if (myDoc.characterStyles.item("bItal") == null){
      var bItal = myDoc.characterStyles.add({name: "bItal", fontStyle: "Bold Italic"});}
      if (myDoc.characterStyles.item("sb") == null){
      var sb = myDoc.characterStyles.add({name: "sb", fontStyle: "Semibold"});}
      if (myDoc.characterStyles.item("sbItal") == null){
      var sbItal = myDoc.characterStyles.add({name: "sbItal", fontStyle: "Semibold Italic"});}
      if (myDoc.characterStyles.item("blk") == null){
      var blk = myDoc.characterStyles.add({name: "blk", fontStyle: "Black"});}
      if (myDoc.characterStyles.item("obl") == null){
      var obl = myDoc.characterStyles.add({name: "obl", fontStyle: "Oblique"});}
      if (myDoc.characterStyles.item("li") == null){
      var li = myDoc.characterStyles.add({name: "li", fontStyle: "Light"});}
      if (myDoc.characterStyles.item("liItal") == null){
      var liItal = myDoc.characterStyles.add({name: "liItal", fontStyle: "Light Italic"});}
      if (myDoc.characterStyles.item("sup") == null){
      var sup = myDoc.characterStyles.add({name: "sup", position: Position.superscript});}
      if (myDoc.characterStyles.item("sub") == null){
      var sub = myDoc.characterStyles.add({name: "sub", position: Position.subscript});}

       

      app.findTextPreferences = NothingEnum.nothing;
      app.changeTextPreferences = NothingEnum.nothing;

       

      app.findTextPreferences = null;
      app.changeTextPreferences = null;

       

      app.findTextPreferences.fontStyle = "Italic";
      app.changeTextPreferences.appliedCharacterStyle = Itl;
      app.documents.item(0).changeText();
      app.findTextPreferences.fontStyle = "Bold";
      app.changeTextPreferences.appliedCharacterStyle = bl;
      app.documents.item(0).changeText();
      app.findTextPreferences.fontStyle = "BoldItalic";
      app.changeTextPreferences.appliedCharacterStyle = bItl;
      app.documents.item(0).changeText();
      app.findTextPreferences.fontStyle = "Bold Italic";
      app.changeTextPreferences.appliedCharacterStyle = bItal;
      app.documents.item(0).changeText();
      app.findTextPreferences.fontStyle = "Semibold";
      app.changeTextPreferences.appliedCharacterStyle = sb;
      app.documents.item(0).changeText();
      app.findTextPreferences.fontStyle = "Semibold Italic";
      app.changeTextPreferences.appliedCharacterStyle = sbItal;
      app.documents.item(0).changeText();
      app.findTextPreferences.fontStyle = "Black";
      app.changeTextPreferences.appliedCharacterStyle = blk;
      app.documents.item(0).changeText();
      app.findTextPreferences.fontStyle = "Oblique";
      app.changeTextPreferences.appliedCharacterStyle = obl;
      app.documents.item(0).changeText();
      app.documents.item(0).changeText();
      app.findTextPreferences.fontStyle = "Light";
      app.changeTextPreferences.appliedCharacterStyle = li;
      app.documents.item(0).changeText();
      app.findTextPreferences.fontStyle = "Light Italic";
      app.changeTextPreferences.appliedCharacterStyle = liItal;

       

      app.findTextPreferences = NothingEnum.nothing;
      app.changeTextPreferences = NothingEnum.nothing;

       

      app.findTextPreferences.position = Position.superscript;
      app.changeTextPreferences.appliedCharacterStyle = sup;
      app.documents.item(0).changeText();
      app.findTextPreferences.position = Position.subscript;
      app.changeTextPreferences.appliedCharacterStyle = sub;
      app.documents.item(0).changeText();

       

      function setupFindText(find, change, foots, hidLayers, lockedLayers, lockedStories, masters, caseSensitive) {
        app.findTextPreferences = null;
        app.changeTextPreferences = null;
        try { app.findTextPreferences.findWhat = find } catch(e) {};
        try {app.changeTextPreferences.changeTo = change } catch(e) {};
        app.findChangeTextOptions.properties = {
          includeFootnotes:(foots == null ? false : foots),
          includeHiddenLayers:(hidLayers == null ? false : hidLayers),
          includeLockedLayersForFind:(lockedLayers == null ? false : lockedLayers),
          includeLockedStoriesForFind:(lockedStories == null ? false : lockedStories),
          includeMasterPages:(masters == null ? false : masters),
          includeCaseSensitive:(caseSensitive == null ? false : caseSensitive)
        }
      } // end setupFindText
      alert ("DONE");

      ------------------------------------------------------------------------------------------ -------------------

       

      please suggest how to simply this script and make it run faster can anyone suggest me. Also suggest me to how can add this "Clear Override" syntax with this script. I want to make this script very effective. Thanks in advance to all.

       

      Regards

      Thiyagu

        • 1. Re: Apply Clear Overrides after creating such character styles...
          [Jongware] Most Valuable Participant

          The reason for that error is easy.

           

          if (myDoc.characterStyles.item("Itl") == null){
          var Itl = myDoc.characterStyles.add({name: "Itl", fontStyle: "Italic"});}

           

          means "if the style does not exist, create it and assign it to the variable". What happens when it does exist? (I'll let you think of an answer here.)

           

          Why don't overrides work? Well, let me ask you, does it work when you do the same directly in the program? No: to clear overrides, replace a style with itself. Does that mean you should run the script twice? Well, no, a brief search through the CS3 JS help reveals this tantalizing function (for any text type element):

           

          void clearOverrides ([overridesToClear:OverrideType=ALL])
          Clears the specified types of override.

          ParameterTypeDescription
          overridesToClearOverrideType:
          OverrideType.ALL
          OverrideType.CHARACTER_ONLY
          OverrideType.PARAGRAPH_ONLY
          The types of override to clear. (Optional) (default: ALL)

           

          -- and I remember someone posting this rather nifty one-liner

           

          myStory.paragraphs.everyItem().clearOverrides();

           

          The script appears a bit Frankensteinian to me. That function setupFindText is never used; you use two different ways of clearing the find and change preferences ('null' and 'nothing' -- I believe there were some strong points against using 'null', but, anyway, you only need to set them to nothing once).

          Notice it doesn't take heed of combinations of a type style and superior/inferior -- the latter will overwrite the former.

           

          The usual way of speeding up a script is by putting repeated elements into an array, but I doubt this will help much here. On the other hand, it's just a handful of replaces, and it should be very fast already. Even if it takes as much as 30 seconds, it's still worth that time.

          1 person found this helpful
          • 2. Re: Apply Clear Overrides after creating such character styles...
            dhishok Level 1

            Hi Jongware,

             

            Thanks for your kind support. I have tried with your syntax [myStory.paragraphs.everyItem().clearOverrides();] to clear overrides for all and its working fine. I have tried but i am unable to differentiate the clear types like CHARACTER_ONLY, PARAGRAPH_ONLY, ALL in the script with below things.

             

            #void clearOverrides ([overridesToClear:OverrideType=ALL])
            Clears the specified types of override.

            ParameterTypeDescription
            overridesToClearOverrideType:
            OverrideType.ALL
            OverrideType.CHARACTER_ONLY
            OverrideType.PARAGRAPH_ONLY
            The types of override to clear. (Optional) (default: ALL)

             

            Can you please suggest how to use this syntax with my script. In the existing script i have added the below syntax for clear overrides.

             

            //

            var myStory = app.activeDocument.stories.everyItem().getElements();
            for (var j = myStory.length - 1; j >= 0; j--)
            {
            var myOvClr = myStory[j].clearOverrides();
            }

            //

             

            Please check and help me for this. Thanks in advance

             

            Thiyagu

            • 3. Re: Apply Clear Overrides after creating such character styles...
              [Jongware] Most Valuable Participant

              Pretty basic syntax, really.

              First of all, as the documentation says, the argument is optional; second, its default behavior is "All". You are getting 'all', because you supplied no argument, and you expected something else. Apparently, you want to clear paragraph formatting only. Just use

               

              myStory[j].clearOverrides(OverrideType.PARAGRAPH_ONLY);


              Notice the documentation states the return type of this function is void; that means you don't get any return value, and it's of no use to 'store' the result in a variable myOvClr.

              • 4. Re: Apply Clear Overrides after creating such character styles...
                dhishok Level 1

                Hi Jongware,

                 

                Sorry for my late reply, past 5 days i was out of station. The problem was occured in the script due to my confusion. Now its working fine. Thanks for your valuable support & explanation.

                 

                Regards

                Thiyagu