Reputation: 1113
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:
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
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.
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:
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
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...).
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?
[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.]
Upvotes: 1
Views: 801
Reputation: 11238
Try this ...
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
tell me to wait_for_sync()
eject ipodName
end repeat
end tell
on wait_for_sync()
tell application "System Events" to tell application process "iTunes"
set theStatusText to ""
repeat until theStatusText is "iPod sync is complete."
set theStatusText to value of static text 1 of scroll area 1 of window "iTunes"
delay 1
end repeat
end tell
end wait_for_sync
Upvotes: 1