title: How to use app.selection[0] for scripts in Adobe InDesign
body: <p>I would like to run code by testing only the current selection (not the whole document) and I'm having difficulty understanding exactly how the array "app.selection" and its methods work.  To start, I use a "for" loop to cycle through each item selected by using:</p>
  
  <pre><code>for(loop = 0; loop &lt; app.selection.length; loop++){
      var sel = loop;
  }
  </code></pre>
  
  <p>This works okay, but when I want to get into determining what each item IS, it gets a little weird.  For example,</p>
  
  <pre><code>for(txt = 0; txt &lt; app.selection[sel].textFrames.length; txt++){
      // do something to each text frame in the selection here.
  }
  </code></pre>
  
  <p>does not work as expected, but</p>
  
  <pre><code>for(img = 0; img &lt; app.selection[sel].allGraphics.length; img++){
      // do something to each graphic in the selection here
  }
  </code></pre>
  
  <p>seems to work fine, regardless if the selection includes more than just graphics alone, or whether it is inside or outside a group.</p>
  
  <p>At times, it seems like app.selection[0] is the only way to access the item by itself.  In other words, if a text frame is selected, app.selection[0] might be the same as app.document.textFrames[0], in which case it would be redundant (and incorrect) to say </p>
  
  <pre><code>app.document.textFrames[0].textFrames[0]
  </code></pre>
  
  <p>And yet the same concept on different page items works like a charm.  It is quite puzzling to follow.  Furthermore, it seems impossible to determine what kind of object the item is.  I want to say something along the lines of:</p>
  
  <pre><code>if (app.selection[0] == [object TextFrame])
  </code></pre>
  
  <p>but that does not seem to work for me.  Is there a way to clearly test if the current item is a group, a graphic or a text frame and do different things depending on the result?</p>