Sudo Reboot apple tv from applescript automatically

Every now and then my ATV (flash installed) stalls up and requires a reboot. This requires me going downstairs to the ATV and unplugging / re plugging the thing.
This annoys me a great deal.
Alternatively you can sudo reboot your ATV through via ssh with terminal.

SO i have written a BASIC applescript to automatically logs in using ssh with password and SUDO REBOOT’s your ATV.
I have posted this here because i could not find this solution on the net. Had i found it this could have saved me countless hours learning applescript!

Here you go copy and paste the following txt into a new applescript window. Note you can save this AS an APPLICATION which i have on my desktop for the wife to access.
Note the finder message at the end can be changed to your liking SATIS is work aviation talk for satisfactory!

[color=#FF0000]------------------------------ copy below here[/color]

property commandString : “ssh frontrow@appletv.local”
property commandString1 : “frontrow”
property commandString2 : “sudo reboot”

tell application “Terminal”
launch
set visible of window 1 to false
if (count of windows) is 1 then
do script commandString in window 1
delay 1
do script commandString1 in window 1
delay 1
do script commandString2 in window 1
delay 1
do script commandString1 in window 1
else
set frontmost of window 1 to true
do script commandString in window 1
delay 1
do script commandString1 in window 1
delay 1
do script commandString2 in window 1
delay 1
do script commandString1 in window 1
end if
end tell
delay 1
tell application “Terminal”
if (count of windows) is 1 then
quit
else
set frontmost of window 1 to true
end if
end tell
tell application “Finder”
display dialog “ATV Rebooted Satis”
end tell

[color=#FF0000]------------------------------end of copy[/color]

Hope this helps anyone interested

New applescript as there were a couple of errors if terminal was already running with no windows open.

[color=#FF0000]Script follows here -------------------------------------------------------------------------[/color]

property commandString : “ssh frontrow@appletv.local”
property commandString1 : “frontrow”
property commandString2 : “sudo reboot”

tell application “System Events”
if not (exists process “Terminal”) then
tell application “Terminal”
launch
get count of windows
set visible of window 1 to false
do script commandString in window 1
delay 3
do script commandString1 in window 1
delay 1
do script commandString2 in window 1
delay 1
do script commandString1 in window 1
delay 1
quit
end tell
else
tell application “Terminal”
activate
tell application “System Events”
tell process “Terminal”
keystroke “n” using command down
delay 1
tell application “Terminal”
get count of windows
do script commandString in window 1
delay 3
do script commandString1 in window 1
delay 1
do script commandString2 in window 1
delay 1
do script commandString1 in window 1
delay 1
close window 1
get count of windows
if (count of windows) is 0 then
quit
else
set frontmost of window 1 to true
end if
end tell
end tell
end tell
end tell
end if
end tell
delay 1
tell application “Finder”
activate
display dialog “ATV Rebooted Satis” with title “ATV Reboot” buttons “Wicked” default button “Wicked” giving up after 2
end tell

[color=#FF0000]End of Script ------------------------------------------------------------------------------------------------------
[/color]