17 Replies Latest reply on Jul 8, 2014 11:55 AM by Team Steam

    Applying [None] character style

    Ken Krugh Level 1

      I have a script that finds text with a certain character style, cuts that text from the main story and anchors it in a frame that sits in the margin.

       

      Everything works fine except that I'd like to have the option of removing the character style. However, when I apply the [None] character style InD shows [None] in the character style lists but the formatting for the character style that was just removed remains on the text.

       

      Is there a way to have it work as if the [None] was manually selected from the character style list? i.e.: where the character style formatting is removed as well?

       

      Thanks,

      Ken

        • 1. Re: Applying [None] character style
          grefel Level 2

          hey,

           

          i think this is a bug in indesign. for my scripts i simply create a new characterStyle based on [None] and apply this

           

          var _basedOnNone = app.activeDocument.characterStyles.add({name:"None", basedOn:app.activeDocument.characterStyles[0]})
          app.selection[0].appliedCharacterStyle = _basedOnNone

           

          i would be interested in any other solution!

          1 person found this helpful
          • 3. Re: Applying [None] character style
            Harbs. Level 6

            I was sure I responded this morning...

             

            Use text.applyCharacterStyle() instead which allows you to clear overrides in the process.

             

            You can also just do clearOverrides() on the text after applying the [None] style.

             

            Harbs

            • 4. Re: Applying [None] character style
              [Jongware] Most Valuable Participant

              Eh, Harbs, isn't that the problem when applying character styles?

               

              void applyCharacterStyle (using: CharacterStyle)
              Apply a character style.

               

              No 'clearingOverrides' parameter. It was the first thing I thought of checking.

               

              You can apply a [None] style and then re-apply its own paragraph style, clearing overrides, but that has the potentially nasty side effect it removes all overrides in the entire paragraph.

               

              [Edit] Ah -- good point on the clearOverrides. You can then do a clearOverrides(OverrideType.CHARACTER_ONLY) on just the selection you applied [None] to. Now that oughta work.

              1 person found this helpful
              • 5. Re: Applying [None] character style
                Harbs. Level 6

                Yeah. Sorry about that. I replied from memory, and forgot that applyCharacterStyle doesn't have the "clear overrides" parameter. (applyParagraphStyle does have one.)

                 

                I guess it's only option 2 (which is the way I've done it in the past)...

                 

                Harbs

                1 person found this helpful
                • 6. Re: Applying [None] character style
                  Ken Krugh Level 1

                  Thanks for all the input guys. Unfortunately I think I'm trying to have my cake and eat it too. I wanted to use the character style to mark the text with something obvious (like an outline color) then be able to remove that character style and all of its formatting while retaining any underlying/local text formatting, just like when you select the [None] character style manually. Disappointing that it doesn't work that way.

                   

                  I tried grefel's suggestion of creating another style based on the none but that does the same thing as clearing the overrides as soon as it is applied, of course.

                   

                  Thanks again,

                  Ken

                  • 7. Re: Applying [None] character style
                    Harbs. Level 6

                    If you know the formatting applied to the style, you can just remove it manually (i.e. text.underline = false).

                     

                    Harbs

                    • 8. Re: Applying [None] character style
                      Kasyan Servetsky Level 5

                      Hi all,

                       

                      I just run into this problem and solved it thanks to the grefel's advice. In case someone is interested, here is a script that removes character styles from selection -- can be either a text frame or some text selected -- and keeps local formatting.

                      if (app.documents.length == 0) ErrorExit("Please open a document and try again.");
                      
                      var gScriptName = "Apply character style None keeping overrides";
                      var gScriptVersion = "1.0";
                      var doc = app.activeDocument;
                      var sel = doc.selection[0];
                      if (doc.selection.length != 1) ErrorExit("One text frame or some text should be selected, or the cursor should be inserted into the text.");
                      
                      if (sel.constructor.name == "TextFrame") { // a text frame is selected
                           ApplyNone(sel.texts[0]);
                      }
                      else if (sel.constructor.name == "Character" || // some text is selected 
                           sel.constructor.name == "Word" ||
                           sel.constructor.name == "TextStyleRange" ||
                           sel.constructor.name == "Line" ||
                           sel.constructor.name == "Paragraph" ||
                           sel.constructor.name == "TextColumn" ||
                           sel.constructor.name == "Text")
                           {
                                ApplyNone(sel);
                           }
                      else {
                           ErrorExit("One text frame or some text should be selected.");
                      }
                      
                      //===================================== FUNCTIONS  ======================================
                      function ApplyNone(sel) {
                           var noneStyle = doc.characterStyles.item("[None]");
                           var basedOnNone = app.activeDocument.characterStyles.add({name:"None", basedOn:noneStyle});
                           sel.applyCharacterStyle(basedOnNone);
                           basedOnNone.remove(noneStyle);
                      }
                      //--------------------------------------------------------------------------------------------------------------------------------------------------------
                      function ErrorExit(error, icon) {
                           alert(error, gScriptName + " - " + gScriptVersion, icon);
                           exit();
                      }
                      //--------------------------------------------------------------------------------------------------------------------------------------------------------
                      
                      • 9. Re: Re: Applying [None] character style
                        Team Steam Level 1

                        Hello!

                         

                        I'm trying to start understanding this javascript thing... Does anyone know how I can modify this so it will also have the option of removing all the character styles in all the text frames, or all the stories in the document? I have several documents with 10 stories each that will need to have the character styles removed before we continue formatting.

                         

                        I took a swing at it myself, but haven't been able to get it to work. I really don't know much about scripting! Here's what I tried:

                         

                        if (app.documents.length == 0) ErrorExit("Please open a document and try again.");    
                        var gScriptName = "Apply character style None keeping overrides";  
                        var gScriptVersion = "1.0";  
                        var doc = app.activeDocument;  
                        var sel = doc.selection[0];  
                        var all = app.documents[0].stories.everyItem()
                        if (doc.selection.length != 1) ErrorExit("One text frame or some text should be selected, or the cursor should be inserted into the text.");  
                          
                        if (sel.constructor.name == "TextFrame") { // a text frame is selected  
                             ApplyNone(sel.texts[0]);  
                        }  
                        else if (sel.constructor.name == "Character" || // some text is selected   
                             sel.constructor.name == "Word" ||  
                             sel.constructor.name == "TextStyleRange" ||  
                             sel.constructor.name == "Line" ||  
                             sel.constructor.name == "Paragraph" ||  
                             sel.constructor.name == "TextColumn" ||  
                             sel.constructor.name == "Text")  
                             {  
                                  ApplyNone(sel);  
                             }  
                        else {  
                             ApplyNone(all);  
                        }  
                          
                        //===================================== FUNCTIONS  ======================================  
                        function ApplyNone(sel) {  
                             var noneStyle = doc.characterStyles.item("[None]");  
                             var basedOnNone = app.activeDocument.characterStyles.add({name:"None", basedOn:noneStyle});  
                             sel.applyCharacterStyle(basedOnNone);  
                             basedOnNone.remove(noneStyle);  
                        }  
                        //--------------------------------------------------------------------------------------------------------------------------------------------------------  
                        function ErrorExit(error, icon) {  
                             alert(error, gScriptName + " - " + gScriptVersion, icon);  
                             exit();  
                        }  
                        //--------------------------------------------------------------------------------------------------------------------------------------------------------  
                        

                         

                        I also tried this, but to no avail.

                        ar gScriptName = "Apply character style None keeping overrides";  
                        var gScriptVersion = "1.0";  
                        var doc = app.activeDocument;  
                        var sel = doc.selection[0];  
                        var all = app.documents[0].stories.everyItem()
                        
                        
                        ApplyNone(all);  
                          
                        //===================================== FUNCTIONS  ======================================  
                        function ApplyNone(sel) {  
                             var noneStyle = doc.characterStyles.item("[None]");  
                             var basedOnNone = app.activeDocument.characterStyles.add({name:"None", basedOn:noneStyle});  
                             all.applyCharacterStyle(basedOnNone);  
                             basedOnNone.remove(noneStyle);  
                        }  
                        //--------------------------------------------------------------------------------------------------------------------------------------------------------  
                        function ErrorExit(error, icon) {  
                             alert(error, gScriptName + " - " + gScriptVersion, icon);  
                             exit();  
                        }  
                        //--------------------------------------------------------------------------------------------------------------------------------------------------------  
                        

                         

                        Any thoughts? I'm thinking that app.documents[0].stories.everyItem() probably isn't right...

                         

                        Thanks for your help!

                        • 10. Re: Applying [None] character style
                          [Jongware] Most Valuable Participant

                          THank you for posting what you tried! (A welcome change from the recent flurry of "here are my demands. Now write something for me" )

                           

                          Can you tell why your attempts are not working? This so it will not be necessary to construct a test document (of hopefully the same structure as your own, and containing similar objects), copy the script, run it and hope to get the exact same errors as you do.

                           

                          ONe thing you can check: indeed I see a problem with your stories.everyItem syntax. What it returns is an array of stories, which is what you want. However, in the function you *call* it with -- ApplyNone -- you don't allow for the fact it *receives* an array. Try something like looping over all of the array's elements, calling the routine for each one separately.

                          • 11. Re: Applying [None] character style
                            Obi-wan Kenobi Adobe Community Professional

                            Aha! I totally agree! 

                             

                            [Jongware] wrote:

                             

                            Thank you for posting what you tried! (A welcome change from the recent flurry of "here are my demands. Now write something for me" )

                            • 12. Re: Re: Re: Applying [None] character style
                              Laubender Adobe Community Professional & MVP

                              @Team Steam – there is one thing in your code that can go wrong. And two things that will go wrong.

                               

                              What can go wrong:

                              If you are on a localized version of InDesign you cannot call the "[None]" character style like this. I'm on a German version and the script will fail.

                              Do it like Gregor did in his answer #1:

                               

                              Instead of:

                              var noneStyle = doc.characterStyles.item("[None]");
                              

                               

                              Write this:

                              var noneStyle = doc.characterStyles[0];
                              

                               

                              What will go wrong:

                               

                              1. What Jongware already said. In your example all is an array. You could loop through that array.

                               

                              But there is also:

                              2. applyCharacterStyle() does not work on the Story object. It is available for Text Objects. Not for Story.

                               

                              How would you get all texts objects of all story objects?

                              Try it like that:

                               

                              var all = doc.stories.everyItem().texts.everyItem().getElements();
                              

                               

                              This all now is an array you can loop through:

                               

                              for(var n=0;n<all.length;n++){
                                  ApplyNone(all[n]);
                                  };
                              

                               

                              Ah. In your second attempt, there is a 3rd problem.

                              Your function ApplyNone() takes one argument, sel, and you are calling all instead of sel in the function.

                               

                              I would rewrite it like that:

                               

                              function ApplyNone(inputText) {
                                  
                                  var myNewNoneStyleName = "MyNoNameStyle";    
                                  var noneStyle = doc.characterStyles[0];
                                  var basedOnNone = app.activeDocument.characterStyles.add({name:myNewNoneStyleName, basedOn:noneStyle});    
                                  inputText.applyCharacterStyle(basedOnNone);
                                  basedOnNone.remove(noneStyle);    
                              };
                              

                               

                              Uwe

                              • 13. Re: Re: Re: Re: Applying [None] character style
                                Laubender Adobe Community Professional & MVP

                                There is one thing that can go wrong with function ApplyNone(inputText). If the character style you want to add is already there, the function would fail. We could test, if the character style with that name already exists. Or we could try to use a name that is made of the current date every time when you are using the script. Date is changing so the name will change with the date.

                                 

                                var myNewNoneStyleName = Date.now().toString();
                                

                                 

                                Maybe there are better means doing that, but this is one possibility. And it's very unlikely to fail.

                                 

                                Uwe

                                • 14. Re: Re: Re: Re: Re: Applying [None] character style
                                  Kasyan Servetsky Level 5

                                  Maybe there are better means doing that, but this is one possibility. And it's very unlikely to fail.

                                  I use the following approach (also for paragraph styles, swatches, etc.): first check whether the style exists, and if it doesn't, create it optionally with certain properties.

                                  var doc = app.activeDocument;
                                  var style = MakeCharStyle("Pop-up", {appliedFont: "Arial", fontStyle: "Regular"});
                                  
                                  function MakeCharStyle(name, properties) {
                                      var charStyle = doc.characterStyles.item(name);
                                      if (!charStyle.isValid) {
                                          charStyle = doc.characterStyles.add({name: name});
                                          if (properties != undefined) charStyle.properties = properties;
                                      }
                                      return charStyle;
                                  }
                                  
                                  • 15. Re: Applying [None] character style
                                    Laubender Adobe Community Professional & MVP

                                    @Kasyan – hm, yes.

                                    But in the case we need a character style based on characterStyles[0] without any other properties defined, it would be better to make sure creating a totally new one. Not using one that is already there.

                                     

                                    Uwe

                                    • 16. Re: Re: Applying [None] character style
                                      Laubender Adobe Community Professional & MVP

                                      We could also do the following:

                                       

                                      var myDoc = app.documents[0];
                                      var basicCharStyle = myDoc.characterStyles[0];
                                      var newCharStyle = myDoc.characterStyles.add( { name:Date.now().toString() , basedOn:basicCharStyle } );
                                      myDoc.stories.everyItem().texts.everyItem().applyCharacterStyle(newCharStyle);
                                      newCharStyle.remove(basicCharStyle);
                                      

                                       

                                       

                                      Uwe

                                      • 17. Re: Re: Applying [None] character style
                                        Team Steam Level 1

                                        @Laubender: Thanks for explaining so clearly!

                                         

                                        Amy