• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

[JS] A way to get enumeration names?

Community Expert ,
Jul 22, 2009 Jul 22, 2009

Copy link to clipboard

Copied

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.

TOPICS
Scripting

Views

4.8K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jul 23, 2009 Jul 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 = [
...

Votes

Translate

Translate
Community Expert ,
Jul 23, 2009 Jul 23, 2009

Copy link to clipboard

Copied

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'));
   }

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 23, 2009 Jul 23, 2009

Copy link to clipboard

Copied

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.name] == value) return props.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.name] == value) return baseStr+"."+props.name;
   }
   return String(value);
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 21, 2021 Oct 21, 2021

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 21, 2021 Oct 21, 2021

Copy link to clipboard

Copied

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);
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 27, 2009 Jul 27, 2009

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 27, 2009 Jul 27, 2009

Copy link to clipboard

Copied

Very nice, Mark!

Thanks.

Peter

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 27, 2009 Jul 27, 2009

Copy link to clipboard

Copied

That comes pretty close to what I was hoping for

Thanks, Marc!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 21, 2021 Oct 21, 2021

Copy link to clipboard

Copied

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 )

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 21, 2021 Oct 21, 2021

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines