6 Replies Latest reply on Apr 29, 2013 9:02 AM by frederickyocum

    Help with targeting contentType with javascript

    frederickyocum

      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. Re: Help with targeting contentType with javascript
          SureshRaji Level 2

          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;
          
                 }
             }
          
          • 2. Re: Help with targeting contentType with javascript
            frederickyocum Level 1

            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

            • 3. Re: Help with targeting contentType with javascript
              [Jongware] Most Valuable Participant

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

              • 4. Re: Help with targeting contentType with javascript
                frederickyocum Level 1

                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.

                • 5. Re: Help with targeting contentType with javascript
                  [Jongware] Most Valuable Participant

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

                  • 6. Re: Help with targeting contentType with javascript
                    frederickyocum Level 1

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