Copy link to clipboard
Copied
Newbie with a problem with probably a obvious answer. I am having a little trouble in the script snippet below. In the for statement, the alert returns the contentType, so I know that I have an object selected and I can access the contentType enumeration. The switch statement (the real code where I want to process the selection depending on the contentType) returns nothing. This probably has a simple answer, I just don’t know it.
for(var theCounter = 0; theCounter < app.selection.length ; theCounter++){
var theThing = app.selection[theCounter].contentType;
alert (theThing);
switch(theThing){
case 'GRAPHIC_TYPE' :
alert("This is a graphic.");
break;
case 'TEXT_TYPE':
alert("This is text");
break;
} //End switch item checking
TIA
1 Correct answer
Hi
Try this:
for(var theCounter = 0; theCounter < app.selection.length ; theCounter++){
var theThing = app.selection[theCounter].contentType;
alert (theThing);
switch(theThing.toString()){
case 'GRAPHIC_TYPE':
alert("This is a graphic.");
break;
case 'TEXT_TYPE':
alert("This is text");
break;
}
}
Copy link to clipboard
Copied
Hi
Try this:
for(var theCounter = 0; theCounter < app.selection.length ; theCounter++){
var theThing = app.selection[theCounter].contentType;
alert (theThing);
switch(theThing.toString()){
case 'GRAPHIC_TYPE':
alert("This is a graphic.");
break;
case 'TEXT_TYPE':
alert("This is text");
break;
}
}
Copy link to clipboard
Copied
Aaah, to (bloody) String! That did the trick.
The alert function simply converted the value to a string on the fly, so that it could be alerted, but the switch function did not know what I was talking about. So, enumerations are more than string values?
Thanks very much Sureshkumar
Copy link to clipboard
Copied
(Re-typing again in Mozilla, Jive still suxx on IE9!)
frederickyocum wrote:
The alert function simply converted the value to a string on the fly, so that it could be alerted, but the switch function did not know what I was talking about. So, enumerations are more than string values?
They are in fact numbers: http://jongware.mit.edu/idcs6js/pe_ContentType.html -- although that doesn't imply you have to get used to writing
case 1735553140: ...
in your code, you can simply use
case ContentType.GRAPHIC_TYPE: ...
etc. The 'type' before the constant is required; unfortunately, the default toString function seems to forget to add it so your valiant "alert" try lead to nothing.
Copy link to clipboard
Copied
Thanks Jongware.
This leaves me slightly confused because:
theThing is the contentType
In the switch statement I am asking for the contentType of the contentType am I not?
var theThing = app.selection[theCounter].contentType;
switch(theThing){
case ContentType.GRAPHIC_TYPE : alert("This is a graphic.");
It works I just don’t know why.
Copy link to clipboard
Copied
Don't get confused by the repetition of the phrase "contentType".
var theThing = app.selection[theCounter].contentType;
Here 'contentType' is just the name of the property. It could be called 'type' or 'content' or 'ct', or anything else. But its value is a member of the enumeration type "ContentType"; in this case, "ContentType.GRAPHIC_TYPE". This, as a single unit, is a variable with the constant value "1735553140", declared somewhere inside InDesign.
It is a common Javascript construct; you can define your own constants like this:
var myValue = { Test1:1, Test2:2 }
alert (myValue.Test1);
http://jongware.mit.edu/idcs6js/index_Enum%20Suite.html lists all default enumerations in InDesign (my favourite is NothingEnum, with just one member: NothingEnum.NOTHING; it bears the concise description "Nothing.", and its numerical value 1851876449 is, written out in ASCII, "nada").
Copy link to clipboard
Copied
Thanks Jongware. I think I have got. As usual understanding comes with practice.