I am trying to have an applescript run every 2 minutes that would open sound preferences, then change the audio output by pressing the up key, followed by the down key. I am using a TV for external audio and it is creating some static but if I change the audio output to a different source and then back again, the static is gone for a random period of time. I am not a Mac user and have never created an applescript before. Searching around, this is the closest I can get but still gives me an error when I try to compile. I am on Mohave.
tell application "System Events"
delay 0.5
keystroke space using command down
delay 0.5
keystroke "Sound"
delay 0.5
keystroke return
delay 1
keycode "125"
delay 1
keycode "126
end tell