title: AWK, Applescript, String/Int Parsing
body: <p>This is a bit in depth, and I'm in a bit over my head.  I've never used AWK, and the more I try to learn about it (perhaps it's the lateness of the hour) the more frustrating it's getting to be.</p>
  
  <p>I'm trying to write an applescript... script to resize windows across my monitors.  Now, I COULD take the easy route and hardcode my monitor dimensions into the script, but instead I decided to try and gather them dynamically.  I've managed to find a code snippet that parses a plist and spits out ALMOST-useable info, here's the applescript:</p>
  
  <pre><code>set screenInfo to do shell script "defaults read /Library/Preferences/com.apple.windowserver | awk '
          BEGIN { FS=\"= \" }
          /Active/  { screens++ }
          { gsub(\";\",\"\",$2) }
          /^ *OriginX/ { ox[screens] = $2 }
          /^ *OriginY/ { oy[screens] = $2 }
          /^ *Width/   { w[screens]  = $2 }
          /^ *Height/  { h[screens]  = $2 }
          END       {
                  for (si=1;si&lt;=screens;si++) {
                      print ox[si],oy[si],w[si],h[si]
                      }
              }'"
      return screenInfo
  end getScreenInfo
  </code></pre>
  
  <p>That's working pretty well - in my "result" window, I get the following:</p>
  
  <pre><code>"0 0 1920 1080  
  \"-1600\" 0 1600 1200  
  1920 0 1680 1050  
  0 0 1920 1080  
  \"-1600\" 0 1600 1200  
  1920 0 1680 1050  
  3600 1050 1024 768  "
  </code></pre>
  
  <p>The first number pair is the coordinates for the top left corner of the monitor, the second number pair is the resolution of that monitor.  Such is the case for each monitor - though there are duplicates of monitors 1-3, plus a ghost monitor.  I don't really care about that, it should be parse-able.</p>
  
  <p>My problem comes when I try to parse those numbers as ints so I can use them - using something like <code>set theVar to word 1 of paragraph 2 of theString as integer</code>, it parses as <code>1600</code>, not the <code>-1600</code> it needs to be.  I tried stepping through the entire string, 1 character at a time, to remove the escaping backslashes, but I think those only actually show up in the results window - when I <code>display dialog</code>, they don't show.  Even when I don't try to parse it as an integer, just <code>word 1 of paragraph 2</code> it ignores the <code>-</code> and returns <code>"1600"</code>.  How else can I parse this?  Goodness, I'm not a fan of applescript right now.</p>