title: How to tell when iPod sync is complete via AppleScript
body: <p>I am trying to make an AppleScript that will sync and eject my iPod without me having to provide any input after the script starts.  The basic script is:</p>
  
  <pre><code>set ipodList to {}
  tell application "iTunes"
      try
          set ipodList to (name of every source whose kind is iPod)
      end try
      repeat with ipodName in ipodList
          update ipodName
          eject ipodName
      end repeat
  end tell
  </code></pre>
  
  <p>The problem with this script is that it causes iTunes to immediately try to eject the iPod after the sync'ing begins, so the sync'ing is not finished yet.  This causes sometimes one and sometimes two dialog windows to appear.  One window says that "iTunes is syncing the iPod. Are you sure you want to eject it?" and has two buttons, "Cancel" and "Eject." The other window says "The iPod “nanoBot” cannot be ejected because it contains files that are in use by another application." and has only an "OK" button.  Just clicking "Eject" and "OK" causes iTunes to finish sync'ing the iPod and then to eject it.</p>
  
  <p>I would like to use UI scripting to click these two buttons so that iTunes will do what I want.  In fact, the following script seems to do what I want:</p>
  
  <pre><code>tell application "System Events"
      tell process "iTunes"
          try
              click button "Eject" of window 1
          end try
          try
              click button "OK" of window 1
          end try
      end tell
  end tell
  </code></pre>
  
  <p>However, I tested that second bit of code by running it from a second AppleScript.  The first AppleScript's execution hangs on the eject command until I click through the two dialogs (if only it hung on the update command, all of this annoyance would be solved...).  </p>
  
  <p>Is there any way to click through the dialogs in the same script as the eject command?  Or some way of launching a second Applescript from the first and having it keep checking for these dialog windows to come up and then click through them?  Or is there just some simpler solution to this problem?</p>
  
  <p>[Edit: I should add that what I currently do is just sync the iPod with the update command, pause for a few seconds, and then eject the iPod with Finder (I have it set up as a disk drive).  The problem with this method is that there is no way to tell when the sync'ing is complete and if it is not then the script does not eject the iPod and raises an error.]</p>