10 Replies Latest reply on Jun 15, 2011 7:58 AM by OlivierBerquin

    [AS-CS5] Comparison between text style range and character style

    OlivierBerquin

      Hi,

       

      It's been a while since I'm not coming on this forum ...

      I'm looking to make a comparison between a "text style range" and a character style.
      I have no problems, except for the comparison of "fonts".
      myCurrentText = text for char…
      theStyle = character style id…
      I try:
             if (applied font of myCurrentText ≠ applied font of theStyle) then set theCharDifference to 1

       

      The selected text give me:
             font "Minion Pro        Italic" of application "Adobe InDesign CS5.5"
      and the character style:
             "Minion Pro"
      I try "set theTextFont to font of applied font of myCurrentText" -> Error
      This is my script (it's too long to post here) <http://db.tt/BLoGPiN>.
      Loot at the "on CheckCharacterDifference(myCurrentText, theStyle)"
      TIA.
      Oli.


        • 1. Re: [AS-CS5] Comparison between text style range and character style
          Harbs. Level 6

          applied font sometimes returns a Font, and sometimes a string.

           

          You'll need to compare both.

           

          Here's a snippet of the kind of code you'll need in js:

           

          var textFont = text.appliedFont;
          var inFont = inProps.appliedFont;
          var textFontStyle = textFont instanceof Font ? textFont.fontStyleName : text.fontStyle;
          var inFontStyle = inFont instanceof Font ? inFont.fontStyleName : inProps.fontStyle;
          if(textFontStyle != inFontStyle){outProps.fontStyle = textFontStyle}
          var textFontFamily = textFont instanceof Font ? textFont.fontFamily : textFont;
          var inFontFamily = inFont instanceof Font ? inFont.fontFamily : inFont;
          if(textFontFamily != inFontFamily){outProps.appliedFont = textFontFamily}
          
          
          

           

          Harbs

          • 2. Re: [AS-CS5] Comparison between text style range and character style
            OlivierBerquin Level 1

            Thanks, Harbs…

            As always, you are fast and precise in your answers.
            I will try this later.

             

            Oli.

            • 3. Re: [AS-CS5] Comparison between text style range and character style
              OlivierBerquin Level 1

              Hi,

               

              Could you please explain these syntax?

               

                                  var inFont = inProps.appliedFont;

              var textFontStyle =     textFont      instanceof Font ? textFont.fontStyleName     : text.fontStyle;

               

               

               

              For the first one, you "load" the properties of the applied font (of what?)

              And for the second one, you try to "load" the font style name or … don't know.

               

              TIA.

               

              Oli.

              • 4. Re: [AS-CS5] Comparison between text style range and character style
                Harbs. Level 6

                "inProps" is an object containing the properties I'm comparing against.

                "text" is the text that we're comparing

                "outProps" contains the diferences.

                 

                Basically, I check the applied font property of both inProps and text. If it's a font, I grab the properties of that and all the comparison is done with strings.

                 

                Clear as mud?

                 

                Harbs

                • 5. Re: [AS-CS5] Comparison between text style range and character style
                  OlivierBerquin Level 1

                  Yes, it's clearer…

                   

                  But sometimes style does nor font, neither style ... it's just a change in "tracking" or "colour", eg.

                  How to tell if this style could be the same as the selected text?

                   

                  Oli.

                  • 6. Re: [AS-CS5] Comparison between text style range and character style
                    Harbs. Level 6

                    This is just the font part of it. Most of the other comparisons are straight one to one comparisons...

                    • 7. Re: [AS-CS5] Comparison between text style range and character style
                      Harbs. Level 6

                      Here's the whole function:

                       

                      GetCharOverrides = function (text,doc,charStyle){
                          var x;
                          var text = text.properties;
                          var paraStyle = text.appliedParagraphStyle.properties;
                          var inProps = {};var outProps = {};
                          if(doc.characterStyles.length<2){var tempStyle = doc.characterStyles.add();}
                          else{var tempStyle = null;}
                          var a = doc.characterStyles[1].properties;
                          for(x in a){inProps[x] = paraStyle[x];}
                          if(charStyle&&charStyle!=doc.characterStyles[0]){
                              charStyle = charStyle.properties;
                              for(x in charStyle){
                                  if(charStyle[x]&&charStyle[x]!=NothingEnum.NOTHING){
                                      inProps[x] = charStyle[x];
                                  }
                              }
                          }
                          delete inProps.basedOn;
                          delete inProps.name;
                          delete inProps.id;
                          delete inProps.parent;
                          delete inProps.index;
                          delete inProps.imported;
                          delete inProps.label;
                          delete inProps.properties;
                          delete inProps.gradientStrokeStart;
                          delete inProps.gradientFillStart;
                          for(x in inProps){
                              if(text[x]!=inProps[x]){//text.hasOwnProperty(x) && 
                                  if(x=='appliedFont'){
                                      try{
                                          var textFont = text.appliedFont;
                                          var inFont = inProps.appliedFont;
                                          var textFontStyle = textFont instanceof Font ? textFont.fontStyleName : text.fontStyle;
                                          var inFontStyle = inFont instanceof Font ? inFont.fontStyleName : inProps.fontStyle;
                                          if(textFontStyle != inFontStyle){outProps.fontStyle = textFontStyle}
                                          var textFontFamily = textFont instanceof Font ? textFont.fontFamily : textFont;
                                          var inFontFamily = inFont instanceof Font ? inFont.fontFamily : inFont;
                                          if(textFontFamily != inFontFamily){outProps.appliedFont = textFontFamily}
                                      }catch(e){}
                                  }
                                  else{outProps[x] = text[x];}
                              }
                          }
                          if(tempStyle){tempStyle.remove();}
                          return outProps;
                      }
                      • 8. Re: [AS-CS5] Comparison between text style range and character style
                        OlivierBerquin Level 1

                        Funny…

                        I discuss about this kind of function in AppleScript, and it appears that it is not possible to do so.

                        Because, in AS, the properties of a character style is a record, and we can not compare two items of a record without knowing his name ... 

                         

                        Tooooooo bad.

                         

                        Maybe I should consider making javascript ...

                         

                        Thanks Harbs, for your kindness.

                         

                        Oli.

                        • 9. Re: [AS-CS5] Comparison between text style range and character style
                          Harbs. Level 6

                          Heck,

                           

                          You can probably just use my function as-is, and call it using a doScript()...

                           

                          You might need to return it as an array instead of an object, I'm not sure how AS will swallos a JS object...

                          1 person found this helpful
                          • 10. Re: [AS-CS5] Comparison between text style range and character style
                            OlivierBerquin Level 1

                            I've written this, but I do not know if it covers all cases ...

                            Someone?

                             

                            Thanks.

                             

                            Oli.

                             

                            set theText to item 1 of selection

                            set theStyleChar to properties of character style 2

                             

                             

                            -- analysis of the properties of the text

                            set myTextFontApplied to applied font of theText

                            set myTextFontFamily to font family of applied font of theText

                            set myTextFontStyle to font style of theText

                             

                             

                            -- analysis of the character style

                            set myStyleCharFontStyle to applied font of theStyleChar

                            if myStyleCharFontStyle = "" then

                                 set myStyleCharFontStyle to font style of theStyleChar

                                 if myStyleCharFontStylemyTextFontStyle then set theDifference to true

                            else

                                 if myStyleCharFontStylemyTextFontFamily then set theDifference to true

                            end if


                             

                            Message was edited by: OlivierBerquin