Script works well, but looks messy

This is my first AppleScript that works well, but looks messy. How can I clean this up?


-- Activate USB to RS232 serial device in terminal screen.
tell application "Terminal"
	activate
do script "screen /dev/cu.usbserial"
end tell
delay 2
-- Turn on TV power from standby mode 1 only, go to computer, and quit terminal screen.
tell application "System Events"
	keystroke "POWR1   "
	keystroke return
	delay 1
	keystroke "IAVD5   "
	keystroke return
	delay 1
	keystroke "a" using control down
	keystroke "k"
	keystroke "y"
	delay 1
	quit application "Terminal"
end tell
delay 1
-- Activate VirtualDVHS HD recorder.
tell application "VirtualDVHS" to activate
-- Make sure HD recorder is already stopped if previously playing.
tell application "System Events"
	tell application process "VirtualDVHS"
		click button 11 of window "VirtualDVHS - Version 2.15"
	end tell
end tell
delay 1
-- Activate USB to RS232 serial device in terminal screen again.
tell application "Terminal"
	activate
do script "screen /dev/cu.usbserial"
end tell
delay 2
-- Change to Channel 4.1, and quit terminal screen.
tell application "System Events"
	keystroke "DA2P0401"
	keystroke return
	delay 1
	keystroke "a" using control down
	keystroke "k"
	keystroke "y"
	delay 1
	quit application "Terminal"
end tell
delay 1
-- Start recording on channel 4.1 for 30 minutes.
tell application "System Events"
	tell application process "VirtualDVHS"
		click button 12 of window "VirtualDVHS - Version 2.15"
	end tell
end tell
delay 1800
-- Stop recording on channel 4.1 after 30 minutes.
tell application "System Events"
	tell application process "VirtualDVHS"
		click button 11 of window "VirtualDVHS - Version 2.15"
	end tell
end tell
delay 1
-- Activate USB to RS232 device in terminal screen again.
tell application "Terminal"
	activate
do script "screen /dev/cu.usbserial"
end tell
delay 2
-- Go to computer, turn off TV power from standby 1 mode, and quit terminal screen.
tell application "System Events"
	keystroke "IAVD5   "
	keystroke return
	delay 1
	keystroke "POWR0   "
	keystroke return
	delay 1
	keystroke "a" using control down
	keystroke "k"
	keystroke "y"
	delay 1
	quit application "Terminal"
end tell

I can’t test any of these comments, but:

tell application "Terminal"
	activate
do script "screen /dev/cu.usbserial"
end tell

Doesn’t have to be done through the terminal. The usual AppleScript way is is to use a do shell script construction:

do shell script "screen /dev/cu.usbserial"

Key strokes that don’t need a delay between them can be concatinated: eg keystroke "POWR1 " & return

Using control keys is usually done this way: keystroke “a” using {control down, shift down}, ie using should be followed by a list and the list can contain all the control keys.

I don’t know how to interact with screen /dev/cu.usbserial, but I’ll bet all the keystrokes can be built into a single command using screen’s options. See man screen.

Finally, if I would be inclined to put this:


tell application "System Events"
	keystroke "IAVD5   "
	keystroke return
	delay 1
	keystroke "POWR0   "
	keystroke return
	delay 1
	keystroke "a" using control down
	keystroke "k"
	keystroke "y"
	delay 1
end tell

which you repeat in a handler:

to sendKeystrokes(with arguments like Powr1, IAVD5, etc) – make it generic and pass the special stuff to it.

You can combine some of those keystrokes:

tell application "System Events"
	keystroke "POWR1   " & return
	delay 1
	keystroke "IAVD5   " & return
	delay 1
	keystroke "a" using control down
	keystroke "ky"
end tell