5 Replies Latest reply on Jul 27, 2009 11:59 AM by [Jongware]

    [JS] A way to get enumeration names?

    [Jongware] Most Valuable Participant

      I use this small function to convert ID's enumerations to a displayable string:

       

      function enumToText (val)
      {
       var result = "";
       while (val > 0)
       {
        result = result+String.fromCharCode(val % 256);
        val = Math.floor( val/256 );
       }
       return result;
      }
      

       

      which returns, e.g., 'barb' instead of the intimidating constant 1650553442 (for an ArrowHead type). It works, because all InDesign enumerated constants are defined as sequences of 4 ASCII characters. 'none', for example, is often used, and that says a lot more than 1852796517.

      My Javascript help files lists both the number and its associated 4 letter word (and they are all suitable for minors!), but it means I have to browse the Help.

       

      Four letters are clearer than the 10 digit code, granted, but could it be possible in Javascript to get the actual name "ArrowHead.BARBED_ARROW_HEAD", when fed the constant? Other than using a dumb list of all constants?

       

      Edit

      If you were thinking, "a list doesn't sound too bad', I still have the one for CS3 on my site: http://www.jongware.com/cs3enums.html

      It's a boring read, but worse: it's several thousand entries long! Imagine inserting that into every script.

        • 1. Re: [JS] A way to get enumeration names?
          Peter Kahrel Adobe Community Professional & MVP

          Hi Jongware,

           

          Nice function! Just the other day I was wondering how to deal with enumerations, how to interpret them. I hadn't realised that they were abbreviations spelled backwards.

           

          I don't know the answer to your question, but below is a function I use to get all values of an enumeration, name and numerica value. You can use it with call like prop (SpecialCharacters) and prop (Capitalization). Maybe it's of any use.

           

          Peter

           

           

          function prop (f)
             {
             var props = f.reflect.properties;
             var array = [];
             for (var i = 0; i < props.length; i++)
                try {array.push (props[i].name + ': ' + f[props[i].name])} catch(_){}
             array.sort();
             $.writeln (array.join ('\r'));
             }
          • 2. Re: [JS] A way to get enumeration names?
            [Jongware] Most Valuable Participant

            I knew I should use reflect somewhere! I rewrote your snip as follows

             

            a = app.selection[0];
            alert (prop (ContentType, a.contentType)+"\n"+prop (ArrowHead, a.leftLineEnd)+"\n"+prop (DisplaySettingOptions, a.localDisplaySetting));
            
            function prop (base, value)
            {
               var props = base.reflect.properties;
               var array = [];
               for (var i = 0; i < props.length; i++)
               {
                if (base[props[i].name] == value) return props[i].name;
               }
               return String(value);
            }
            

             

            Call the function 'prop' with the name of the enumeration type and the actual value. If the value can be found in the enumeration, it returns its name; else, it returns its numerical value. The sample displays a few random values from your current selected object.

             

            It still requires you to know the enumeration type in advance (in this case, you should know "a.contentType" returns a value from ContentType), but that's just a little bit easier to remember!

             


            [Added]

             

            A tiny variation: feed 'prop' with a string instead. It's a bit of a dirty workaround but this allows the function to return a proper full enumeration name:

             

              ContentType.TEXT_TYPE
              ArrowHead.NONE
              DisplaySettingOptions.DEFAULT_VALUE

             

            which you can copy and paste as is into another script.

            a = app.selection[0];
            alert (prop ("ContentType", a.contentType)+"\n"+prop ("ArrowHead", a.leftLineEnd)+"\n"+prop ("DisplaySettingOptions", a.localDisplaySetting));
            function prop (baseStr, value)
            {
            var base = eval(baseStr);
            var props = base.reflect.properties;
            var array = [];
               for (var i = 0; i < props.length; i++)
               {
                if (base[props[i].name] == value) return baseStr+"."+props[i].name;
               }
               return String(value);
            }
            
            • 3. Re: [JS] A way to get enumeration names?
              Marc Autret Level 4

              Hi all,

               

              Another way to do the job through a generic Object method:

              /*str*/Object.prototype.enumName = function(/*int*/enumId)
              {
              for ( var p in this )
                if ( this[p] == enumId ) return(p);
              return(FALSE);
              }
              

               

              Examples:

               

              // Displays a SpecialCharacter's name (here "EM_SPACE") :
              alert( SpecialCharacters.enumName(0x53456D53) );
              
              // Displays the current horiz. measurements unit :
              var u = app.viewPreferences.horizontalMeasurementUnits;
              alert( MeasurementUnits.enumName(u) );
              

               

              @+

              Marc

              1 person found this helpful
              • 4. Re: [JS] A way to get enumeration names?
                Peter Kahrel Adobe Community Professional & MVP

                Very nice, Mark!

                Thanks.

                 

                Peter

                • 5. Re: [JS] A way to get enumeration names?
                  [Jongware] Most Valuable Participant

                  That comes pretty close to what I was hoping for

                  Thanks, Marc!