4 Replies Latest reply on Jul 23, 2015 7:04 AM by Johnwhite

    How to add a character style in active doc

    Johnwhite Level 1

      Hi experts

       

      How to create a character style in active doc.

       

      how to make the script as be works.

       

      Thanks

      Regard

      John

       

      var myCharacterStyle;

      var myDocument = app.activeDocument;

      var myCharacterStyle = myDocument.characterStyles.add({name:"c"});

      myCharacterStyle.strikeThru = true;

      myCharacterStyle.strokeColor = "Black";

          myCharacterStyle.add()

        • 1. Re: How to add a character style in active doc
          Loic.Aigon Adobe Community Professional

          There isn't much to say about your code. Only the last line would cause an issue. Also a possible hiccup lies on the fact that the style may already exist when you want to add it.

           

          var myDocument = app.activeDocument;

          var myCharacterStyle = myDocument.characterStyles.item ("c" );

          !myCharacterStyle.isValid && myCharacterStyle = myDocument.characterStyles.add({name:"c"});

          myCharacterStyle.strikeThru = true;

          myCharacterStyle.strokeColor = "Black";

          • 2. Re: How to add a character style in active doc
            Johnwhite Level 1

            Thank you Loic.Aigon

            thank you so much

             

            but how to create 2 character styles

             

            script as below how to make it work?

             

            var myDocument = app.activeDocument;
            var myCharacterStyle = myDocument.characterStyles.item ("c" );
            !myCharacterStyle.isValid && myCharacterStyle myCharacterStyle = myDocument.characterStyles.add({name:"StrikeThru"});
            myCharacterStyle.strikeThru = true;
            myCharacterStyle.strikeThroughColor = "Black";
            myCharacterStyle.strikeThroughWeight = 0.75;
            !myCharacterStyle.isValid && myCharacterStyle = myDocument.characterStyles.add({name:"Underline"});
            myCharacter.underline = true
            myCharacterStyle.underline = "Black";
            myCharacter.underlineWeight = 0.75;

             

             

            Regard

            John

            • 3. Re: How to add a character style in active doc
              Loic.Aigon Adobe Community Professional

              Your problem is that you override the same character style reference while trying to add a new one. You have to understand the concept or references and variable declaration here:

               

              function doStyle ( doc, styleProps ) {
                   var cs = doc.characterStyles.item ( styleProps.name );
                   !cs.isValid && cs = doc.characterStyles.add();
                   cs.properties = styleProps;
              }
              
              function main() {
              
                   var doc;
                   if ( !app.documents.length) return;
                   doc  =app.activeDocument;
              
                   var cs1 = {
                             name:"StrikeThru",
                             strikeThru:true,
                             strikeThroughColor:"Black",
                             strikeThroughWeight :0.75,         
                   };
              
              
                   
                   var cs2 = {    
                             name:"Underline",
                             underline:true,
                             underlineColor:"Black",
                             underlineWeight :0.75,         
                   };
              
              
              
              
                   doStyle (doc,  cs1 );
                   doStyle (doc, cs2 ); 
              
                             
              }
              
              main();
              
              • 4. Re: How to add a character style in active doc
                Johnwhite Level 1

                Thank you Loic.Aigon

                thank you so much

                 

                John