Skip to main content
Jongware
Community Expert
Community Expert
July 22, 2009
Answered

[JS] A way to get enumeration names?

  • July 22, 2009
  • 3 replies
  • 5680 views

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.

This topic has been closed for replies.
Correct answer Peter Kahrel

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.name + ': ' + f[props.name])} catch(_){}
   array.sort();
   $.writeln (array.join ('\r'));
   }

3 replies

Community Expert
October 21, 2021

My take is this:

var e = SpecialCharacters;
for( x in e )
{
	$.writeln( x );
};

 

Or I use x.toString() when not using $.writeln().

On the other hand, just look up all options in the DOM documentation compiled by Gregor Fellenz:

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#about.html

 

Regards,
Uwe Laubender

( ACP )

Inspiring
October 21, 2021

Thanks, Uwe,

 

A quick test revealed that x.toString() and String(x) are functionally identical. Gave me the name of the enumeration as a string for output in a report of special characters used in a text. Only used the $writeln() function to view the result while testing.

 

Tim

Marc Autret
Legend
July 27, 2009

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

== 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

Community Expert
July 27, 2009

Very nice, Mark!

Thanks.

Peter

Peter KahrelCommunity ExpertCorrect answer
Community Expert
July 23, 2009

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.name + ': ' + f[props.name])} catch(_){}
   array.sort();
   $.writeln (array.join ('\r'));
   }
Inspiring
October 21, 2021

To get name of enumeration just use the JavaScript String() function:

for (x in SpecialCharacters) {
    outStr = "[" + String(x) + "]";
    $.writeln(outStr);
}
 
Returns the name of the enumeration as a string! In my case I wanted the square brackets for display purposes in a preflight script I was writing.
Took me 3 days to find this solution but it is incredibly easy and elegant.
Inspiring
October 21, 2021

Small modification to get the number value of the enumeration as well:

 

for (x in SpecialCharacters) {
    outStr = String(x);
    y = SpecialCharacters[x];
    $.writeln(outStr + " " + y);
}