3 Replies Latest reply on Mar 17, 2016 6:37 AM by ChocoBeast

    Character Styles from Swatches?

    ChocoBeast Level 1

      I'm looking to write a script that will create character styles based on the swatch color of the text. If the text already has a character style applied to it (say, for instance, Bold) I'd want it to make the new style based on the style already applied (Bold + [Swatch Name]). Is there a way to do this? I was thinking it'd somehow grab the swatch name and base it off of those values, but I'm not sure how to do about doing that. In my case, all of the swatches will have already been converted to CMYK, so that may make it easier to grab (it'd exclude the [Black] and [Paper] swatches, etc.). I'm using JongWare's PrepText to apply the character styles before this would run - maybe there's a way to write it into that script instead?

        • 1. Re: Character Styles from Swatches?
          elansezhianr72877736

          Hi,

           

          Please find the below code:

           

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

           

          #target Indesign

           

          var mydoc = app.activeDocument;

          var mypages = mydoc.pages;

           

          for (i=0; i<mypages.length; i++){

              var docPageItems = mypages[i].pageItems;

              for (dp=0; dp<docPageItems.length; dp++){

                  app.select (docPageItems[dp]);

                  var pSel = app.selection[0];

                  if (pSel.constructor.name == "TextFrame" && pSel.itemLayer.visible && pSel.contents != ""){

                      var mypara = pSel.paragraphs;

                      for (j=0; j<mypara.length; j++){

                          var myword = mypara[j].words;

                          for (k=0; k<myword.length; k++){

                          if((myword[k].fillColor.name != "Black") && (myword[k].fillColor.name != "Paper") && (myword[k].appliedCharacterStyle.name == '[None]')){

                              try{

                                  var myCSstyle = myword[k].appliedCharacterStyle.name;

                                  var mycstyle = (myword[k].fillColor.name);

                                  mydoc.characterStyles.add({name: mycstyle});

                                  }catch(e){}

                                  }               

                          if((myword[k].fillColor.name != "Black") && (myword[k].fillColor.name != "Paper") && (myword[k].appliedCharacterStyle.name != '[None]')){

                              try{

                                  var myCSstyle = myword[k].appliedCharacterStyle.name;

                                  var mycstyle = (myword[k].fillColor.name);

                                  mydoc.characterStyles.add({name: myCSstyle + "_"+ mycstyle});

                                  }catch(e){}

                                  }               

                          }

                      }           

                  }

              }

          }

           

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

          • 2. Re: Character Styles from Swatches?
            Obi-wan Kenobi Adobe Community Professional

            Hi,

             

            I've tested your script with no real result! 

            • 3. Re: Character Styles from Swatches?
              ChocoBeast Level 1

              I deleted my last reply because I was a moron and was testing the wrong file. Funny how changes don't reflect in a script if you're running an older version of it...

               

              Anyway - I've modified the script to only run in the current story, and I've also set it to base the new style off a preexisting one and also pull the swatch into the style. Your code pulled the names just fine, but the character styles themselves were empty. I've also adjusted it to apply the character styles to the text.

               

              The problem I run into now is that it's only applying the character style to the first word in an instance. I'm not sure how to modify it to apply it to all words.

               

              var myDoc = app.activeDocument;
              var myStory = myDoc.selection[0].paragraphs[0].parentStory;
              var myPara = myStory.paragraphs;
              
              
              function getParagraphStyle(styleName, targetDoc) {
                if (targetDoc == null) {
                targetDoc = app.activeDocument;
                }
              
                if (targetDoc.paragraphStyles.itemByName(styleName) != null) {
                return targetDoc.paragraphStyles.itemByName(styleName);
                } else {
                var max = targetDoc.paragraphStyleGroups.count()
                for (var i = 0; i < max; i++) {
                if (targetDoc.paragraphStyleGroups[i].paragraphStyles.itemByName(styleName) != null) {
                return targetDoc.paragraphStyleGroups[i].paragraphStyles.itemByName(styleName);
                }
                }
                }
              }
              
              
              for (j=0; j<myPara.length; j++){
                  var myWord = myPara[j].words;
                  for (k=0; k<myWord.length; k++){
                      if((myWord[k].fillColor.name != "Black") && (myWord[k].fillColor.name != "Paper") && (myWord[k].appliedCharacterStyle.name == '[None]')){
                          try{
                              var originalCharStyle = myWord[k].appliedCharacterStyle.name;
                              var swatchCharStyle = (myWord[k].fillColor.name);
                              var newSwatchStyle = myDoc.characterStyles.add({name: swatchCharStyle});
                              newSwatchStyle.basedOn = getCharacterStyle(originalCharStyle);
                              newSwatchStyle.fillColor = myDoc.colors.item(swatchCharStyle);
                              myWord[k].applyCharacterStyle (newSwatchStyle);
                              }catch(e){}
                              }               
                          if((myWord[k].fillColor.name != "Black") && (myWord[k].fillColor.name != "Paper") && (myWord[k].appliedCharacterStyle.name != '[None]')){
                              try{
                                  var originalCharStyle = myWord[k].appliedCharacterStyle.name;
                                  var swatchCharStyle = (myWord[k].fillColor.name);
                                  var newSwatchStyle = myDoc.characterStyles.add({name: originalCharStyle + " + "+ swatchCharStyle});
                                  newSwatchStyle.basedOn = getCharacterStyle(originalCharStyle);
                                  newSwatchStyle.fillColor = myDoc.colors.item(swatchCharStyle);
                                  myWord[k].applyCharacterStyle (newSwatchStyle);
                                  }catch(e){}
                                  }
                              }
                          }