5 Replies Latest reply on May 7, 2009 3:08 PM by Thomas Silkjaer

    [JS] Character properties vs. character style properties.

    Thomas Silkjaer Level 1

      I am trying to define differences between a characters properties and the paragraph it is inside.


      I am doing this if(the_character.appliedFont != the_paragraph.appliedFont) but it doesn't work - it seems they return the values differently. As if the character returns the appliedFont as an object, and the paragraph returns it as a string. Any ideas?

        • 1. Re: [JS] Character properties vs. character style properties.
          [Jongware] Most Valuable Participant

          If I have my terminology right, JavaScript is a weakly typed language.


          It is weakly (weekly?) typed because each variable can hold just about any type of object -- you don't have to specify 'int' or 'string' or even 'DocumentPreference'. A drawback is that any function can return any reasonable type as well. Font, string, whatever ... If you compare two items, JS has to consider the type of the two, which is fairly reasonable, after all.


          Enter the font functions. It seems some of them return the font name as a string, others as a Font object (capital 'F' because it's a genuine InDesign Object Model object). And in the case you mention, it seems two likewise functions return two different types. Ole Kvern might want to comment on that


          There are several ways to 'get' a font, and you probably can find one that works with both functions. This thread http://forums.adobe.com/thread/420107?tstart=90 mentions a few ways to get/set fonts/font names. You will have to excuse littlemookie's posts, he's just learning At least he provided me and Harbs with fresh stuff to discuss.

          • 2. Re: [JS] Character properties vs. character style properties.
            Harbs. Level 6

            This topic has come up a number of times in the past. I have a function which I use for this purpose. It represents not a small amount of work...


            GetCharOverrides = function (text,doc,charStyle){
              var x,e;
              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 = false;}
              var a = doc.characterStyles[1].properties;
              for(x in a){inProps[x] = paraStyle[x];}
                charStyle = charStyle.properties;
                for(x in charStyle){
                    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){
                      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}
                  else{outProps[x] = text[x];}
              return outProps;
            1 person found this helpful
            • 3. Re: [JS] Character properties vs. character style properties.
              Thomas Silkjaer Level 1

              Thanks a lot, both of you!


              In the mean time, I came up with a solution much alike Harbs, creating a temporary style and removing it. Comparing properties in a character style with another character style didn't seem to make conflicts.  I spelled out each property though, which now seems of way to much work after seeing your snippet

              • 4. Re: [JS] Character properties vs. character style properties.
                Harbs. Level 6

                Aw, I thought that answer was a whole answer and not just a helpful one!


                BTW, The function optionally accepts a character style. If a character style is passed, it will find overrides on both the character style and the paragraph style. If not, it will find overrides on the paragraph style. (i.e. styling applied via a character style will be considered overridden styling)



                • 5. Re: [JS] Character properties vs. character style properties.
                  Thomas Silkjaer Level 1

                  It was a correct answer, of course, sorry Just not 100% what i was going with