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

Help with targeting contentType with javascript

Community Beginner ,
Apr 29, 2013 Apr 29, 2013

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

TOPICS
Scripting
1.5K
Translate
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

Explorer , Apr 29, 2013 Apr 29, 2013

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;

       }

   }

Translate
Explorer ,
Apr 29, 2013 Apr 29, 2013

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;

       }

   }

Translate
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 Beginner ,
Apr 29, 2013 Apr 29, 2013

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

Translate
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 ,
Apr 29, 2013 Apr 29, 2013

(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.

Translate
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 Beginner ,
Apr 29, 2013 Apr 29, 2013

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.

Translate
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 ,
Apr 29, 2013 Apr 29, 2013

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").

Translate
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 Beginner ,
Apr 29, 2013 Apr 29, 2013
LATEST

Thanks Jongware. I think I have got. As usual understanding comes with practice.

Translate
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