Esa, this should log you in if required, and also remove the focus from the Search window.
on run
tell application "System Events"
if not UI elements enabled then
display dialog "Enable Access for assistive devices (found in the Universal Access System Preference) must be on for this software to correctly work. This program will enable this setting for you" giving up after 5
set UI elements enabled to true
display dialog "Access for assistive devices in now on" giving up after 5
end if
end tell
# Your part modified
tell application "iTunes"
activate
tell application "Finder"
repeat until exists process "iTunes"
delay 0.1
end repeat
delay 0.1
set theEnabled to exists window "iTunes"
tell application "System Events" to tell process "iTunes"
if not theEnabled then
keystroke "1" using {option down, command down}
delay 2
end if
try
keystroke "7" using command down
end try
try
click button 11 of window "iTunes"
end try
delay 1
keystroke "r" using command down
end tell
end tell
end tell
# My part
tell application "iTunes"
activate
tell application "Finder"
tell application "System Events" to tell process "iTunes"
delay 1
if exists button "Create Apple ID" of window 1 then
select text field 1 of window 1
keystroke "EnterYouriTunesAccountNameHere"
keystroke tab
delay 1
keystroke "EnterYouriTunesPasswordHere"
delay 1
keystroke return
end if
set flag to true
set x to 0
delay 2
repeat until exists UI element 1 of splitter group 1 of window "iTunes"
set x to x + 1
if x = 300 then # only wait 30 seconds maximum
set flag to false
exit repeat
end if
delay 0.1
end repeat
delay 0.4
if flag then
keystroke "4" using {option down, command down} # opens downloads window
tell window "iTunes"
set buttonCount to (count of (buttons of UI element 1 of splitter group 1)) - 1
say buttonCount & " items to update." as text
end tell
repeat with eachButton from 1 to buttonCount
click button eachButton of UI element 1 of splitter group 1 of window "iTunes"
click
delay 1
end repeat
else
say "Updates Window did not open"
end if
end tell
end tell
end tell
end run