title: Applescript to output "unread" mail (y/n) to serial port -- won't compile
body: <p>To start, I'm fairly new to Applescript. At the expense of buying an ethernet shield for my Arduino, I decided to dedicate a serial port to it (over usb). </p>
  
  <p>This way, if I write an Applescript to check my mail and output an "N" for no new mail or an "M" for new mail to the serial port that it is connected to, it will respond accordingly. (More actions to be added later).</p>
  
  <p>Anyway, I have been writing my script, and found another that seems to do what I want...</p>
  
  <pre><code>set use_port to "/dev/cu.usbmodemfa131"
  
  if (get serialport list) contains use_port then
  
      set onMode to true
      set myPort to serialport open use_port bps rate 9600 data bits 8 parity 0 stop bits 1 handshake 0
  
      if myPort ≠ -1 then -- check the port opened correctly
  
          repeat while onMode is true
  
              tell application "Mail"
                  set x to unread count of inbox
                  check for new mail
                  delay 600
                  set y to unread count of inbox
              end tell
  
              if x = y then
                  serialport write "N" to myPort
              else
                  serialport write "M" to myPort
              end if
  
              -- add some logic here to determine whether to loop again, e.g.
  
              if foo = bar then set onMode to false
  
           end repeat
  
           serialport close myPort
  
      end if
  
  end if
  </code></pre>
  
  <p>The issue, though, is that this does not compile. As I'm not familiar with Applescript enough to debug this myself, I'm asking for help.</p>
  
  <p>(Note: I have a fully functional Arduino sketch that responds to M, N, and a few other commands over serial. I would just like to automate the sending of these commands, without having a "tell" (no apps open, just communication over serial). Any help would be appreciated!)</p>
  
  <p>//New adjusted code follows</p>
  
  <pre><code>global myPort
  
  
  
  on run
  
  set use_port to "/dev/tty.usbmodem1d11"
  
  if (get serialport list) contains use_port then
  
      set myPort to serialport open use_port bps rate 9600 data bits 8 parity 0 stop bits 1 handshake 0
  
      if myPort = -1 then -- check the port opened correctly
  
  display alert "Ooops, unable to open the serial port " &amp; use_port
  
  tell me to quit
  
  end if
  
  else
  
      display alert "Oops. Serial port not found"
  
      tell me to quit
  
  end if
  
  end run
  
  
  
  
  
  on idle
  
  tell application "Mail"
  
      set x to unread count of inbox
  
  end tell
  
  
  
  if x &gt; 0 then
  
      set mailStatus to "a"
  
  else
  
      set mailStatus to "b"
  
  end if
  
  serialport write mailStatus to myPort
  
  
  
  return 3 -- check again in 3 seconds
  
  end idle
  
  
  
  
  
  on quit
  
  try
  
      serialport close myPort
  
  end try
  
  continue quit
  
  end quit
  </code></pre>