0

I've seen answers like these but what I'm trying to do is programmatically ALWAYS set the audio output to built-in speakers regardless of which row item the Mac is currently tuned to.

In the code below (cited from the above link), the user selects between items 3 and 4. We know that built in speakers are always item 1, so is there a simpler way to achieve this where you send a shell command to always set the audio output to Built-in speakers?

(*
 Applescript to toggle between two sound outputs by Line number, ¬
 as they appear in the Sound Control Panel. Based on code by ¬
 Arthur Hammer http://apple.stackexchange.com/a/209434/85275
 *)

set internal to 1 --internal speakers set appletv to 3 --Follows internal speakers and separator line for Airplay devices

tell application "System Preferences" activate set current pane to pane "com.apple.preference.sound" end tell

tell application "System Events" tell application process "System Preferences" repeat until exists tab group 1 of window "Sound" end repeat tell tab group 1 of window "Sound" click radio button "Output" if (selected of row internal of table 1 of scroll area 1) then set selected of row appletv of table 1 of scroll area 1 to true else set selected of row internal of table 1 of scroll area 1 to true end if end tell end tell end tell tell application "System Preferences" to quit

IconDaemon
  • 19,234
  • I've actually been working with switchaudio-osx but I'm trying to figure out a solution that's independent of that :/ – Mansidak Jan 24 '23 at 14:15
  • There is no built in way to switch audio via command line – Allan Jan 24 '23 at 14:16
  • I see. Do you know how the code that I referenced in the question achieved that? they're clearly not using switchaudio-osx – Mansidak Jan 24 '23 at 14:17
  • That code just programmatically controls the GUI. As long as your device is on the row that the script references, it will work. If yours is different, you’ll get different results. (I’m not a fan as I prefer Bash scripting) – Allan Jan 24 '23 at 14:21
  • Yes exactly! I just want to know how can I use that code to ALWAYS select built in speaker regardless of which row the output is currently. Does that make sense? Like regardless of if the current selection is at #3 item or #2, I want it to come back to built in speakers every single time. I hope I was able to explain myself? – Mansidak Jan 24 '23 at 14:23
  • 1
    You make complete sense and I agree with you. I’m sure there is a way in AppleScript, but I’m not the guy to do it. I learned to program in C, then C++ and finally Java. AppleScript is a frustrating language for me so I avoided it. – Allan Jan 24 '23 at 14:27
  • Gotchu. Well anywyas, thanks for the input regardless. – Mansidak Jan 24 '23 at 14:29
  • @Allan Ah interesting. I didn't realize that AppleScript actually shows you what's going on (manipulating GUI as you said). I didn't know that it couldn't run in the background. Do you know bash always runs in the background? – Mansidak Jan 24 '23 at 14:36
  • Bash scripts run in a sub-shell (non-interactive). They can in either the foreground or background. – Allan Jan 24 '23 at 14:39

0 Answers0