Change Output of AppleScript to Variable

Hi,

I've found this apple script - I am novice, so i want help in saving the output to a variable. I think it occurs, at the marked line, i've appended an image the text and the macro it self.

get sound output.kmmacros (2.2 KB)

Crossing my fingers :crossed_fingers:

It can be done in the script, for example:

tell application "System Preferences"
    reveal pane id "com.apple.preference.sound"
end tell

tell application "System Events"
    tell application process "System Preferences"
        tell tab group 1 of window "Sound"
            click radio button "Output"
            tell table 1 of scroll area 1
                set selected_row to (first UI element whose selected is true)
                set currentOutput to value of text field 1 of selected_row as text
                
                tell application "Keyboard Maestro Engine"
                    setvariable "outputDevice" to currentOutput
                end tell
                
                -- display dialog currentOutput
            end tell
        end tell
    end tell
end tell

if application "System Preferences" is running then
    tell application "System Preferences" to quit
end if

but it might be simpler:

  • to just return the value from the script
  • choose the option (on the Execute Script Action) to save its result to a variable.

get sound output 2.kmmacros (2.6 KB)

1 Like

Thanks - much appreciated!

If you need or want more direct control over your Mac's sound devices, and want to avoid UI scripting (which can be fragile), you might consider this:

switchaudio-osx -- GitHub

Note: I have NOT tested this personally, but it seems to be recommended by a lot of folks at StackExchange.com)

SwitchAudioSource [-a] [-c] [-t type] [-n] -s device_name

  • -a : shows all devices
  • -c : shows current device
  • -t type : device type (input/output/system). Defaults to output.
  • -n : cycles the audio device to the next one
  • -s device_name : sets the audio device to the given device by name

The price is that you have to have, or install, Xcode and Xcode's command line tools.
There are other tools that need these, and Xcode is free, so it may be work having in the long run.

@JMichaelTX Thanks for the thought, currently i am having issues with downloading xcode, why i am looking for other options.

However, it is a work in progress, so hopefully i'll get it up and running

1 Like