Skip navigation

applescript to switch audio output

2747 Views 5 Replies Latest reply: Feb 22, 2014 3:13 PM by Steve HC19 RSS
mbierman Level 1 Level 1 (0 points)
Currently Being Moderated
Dec 25, 2012 6:24 PM

I am having a little trouble with this applescript which is intended to present a dialog with some choices and switch the audio output accordingly. Any help would be greatly appreciated. I've started a gist too.

 

tell application "System Preferences" to activate

tell application "System Events"

          get properties

          set asrc to choose from list {"Internal Speakers", "Living Room", "Girl's Room", "Master Bedroom"} with title "Sound Picker" default items {"Internal Speakers"}

          display dialog "src is: " & asrc

 

 

          tell process "System Preferences"

                    click menu item "Sound" of menu "View" of menu bar 1

  delay 2

                    set theRows to every row of table 1 of scroll area 1 of ¬

                              tab group 1 of window "sound"

 

                    set theOutputs to {} as list

                    repeat with aRow in theRows

                              if (value of text field 1 of aRow as text) ¬

                                        is equal to asrc then

                                        set selected of aRow to true

                                        exit repeat

                              end if

                    end repeat

          end tell

end tell

tell application "System Preferences" to quit

  • Pierre L. Level 4 Level 4 (3,860 points)
    Currently Being Moderated
    Dec 26, 2012 5:31 AM (in response to mbierman)

    Hi,

     

    The following script, tested under OS X 10.8.2, should do the trick.

     

     

    set asrc to (choose from list {"Internal Speakers", "Living Room", "Girl's Room", "Master Bedroom"} with title "Sound Picker" default items {"Internal Speakers"}) as text

    if result is "false" then return

     

    display dialog "src is: " & asrc

     

    tell application "System Preferences"

        reveal anchor "output" of pane id "com.apple.preference.sound"

        activate

     

        tell application "System Events"

            tell process "System Preferences"

                select (row 1 of table 1 of scroll area 1 of tab group 1 of window "Sound" whose value of text field 1 is asrc)

            end tell

        end tell

     

        quit

    end tell


     

    To have the script run in the background, just comment out the “activate” line above.

     

    You might also have a look at this web page.

     

     

    Message was edited by: Pierre L. (fixed the web link)

  • Pierre L. Level 4 Level 4 (3,860 points)
    Currently Being Moderated
    Dec 26, 2012 8:57 AM (in response to mbierman)

    My pleasure.

  • martoony Level 1 Level 1 (0 points)
    Currently Being Moderated
    Jan 17, 2013 4:04 PM (in response to mbierman)

    I'm having a similar, but slightly different issue.

     

    I'd like a script to run that automatically picks "display audio" for the speakers and "display audio" for the mic when I connect my Macbook Pro to my Thunderbolt display. It doesn't seem to do this on its own. So frustrating.

     

    Can we make scripts run automatically upon detecting something like a Thunderbolt monitor?

     

    If not, I already have a program called ControlPane that does detect things like the monitor. It can also run scripts upon detecting, so I would just need two scripts to activate/deactivate these prefs (i.e. one to change both mic and output to "display audio" when Thunderbolt connected, and one to change both back to "Internal speakers/mic" when I disconnect the monitor).

     

    Can somebody help with the script portion at least? Thanks!

  • Steve HC19 Level 1 Level 1 (0 points)
    Currently Being Moderated
    Feb 22, 2014 3:13 PM (in response to Pierre L.)

    Pierre,

    As you seem the guy to speak to in regards to Apple script, do you have any idea why the second part of this script does not work?  The goal of the script is to be able to switch between 2 different input/output , one being the Internal and the other being an USB headset.

    The non working part is after --USB

    Any help would be greatly appreciated

    Merci d'avance

     

     

     

    property cancel : "Cancel"

    tell application "System Preferences" to activate

    activate --making dialogue box frontmost

     

    set answer to button returned of (display dialog "

    Select Sound Input / Output

    " with icon file ((path to me) & "Contents:Resources:icn:SoundPrefcopy.icns" as string) with title "Select Sound Input/Output " buttons {"iMac", "USB", "Cancel"} default button 2)

     

    -- Internal

     

    if answer is equal to "iMac" then

        tell application "System Preferences"

            reveal anchor "input" of pane id "com.apple.preference.sound"

            tell application "System Events" to tell process "System Preferences"

                tell table 1 of scroll area 1 of tab group 1 of window 1

                    select (row 1 where value of text field 1 is "Internal microphone")

                end tell

            end tell

        end tell

        tell application "System Preferences"

            reveal anchor "output" of pane id "com.apple.preference.sound"

            tell application "System Events" to tell process "System Preferences"

                tell table 1 of scroll area 1 of tab group 1 of window 1

                    select (row 1 where value of text field 1 is "Internal speakers")

                end tell

            end tell

        end tell

        -- USB (Non working)

        if answer is equal to "USB" then

            tell application "System Preferences"

                reveal anchor "input" of pane id "com.apple.preference.sound"

                tell application "System Events" to tell process "System Preferences"

                    tell table 1 of scroll area 1 of tab group 1 of window 1

                        select (row 2 where value of text field 1 is "Microsoft LifeChat LX-3000")

                    end tell

                end tell

            end tell

            tell application "System Preferences"

                reveal anchor "output" of pane id "com.apple.preference.sound"

                tell application "System Events" to tell process "System Preferences"

                    tell table 1 of scroll area 1 of tab group 1 of window 1

                        select (row 2 where value of text field 1 is "Microsoft LifeChat LX-3000")

                    end tell

                end tell

            end tell

           

            --Cancel

            if answer is equal to "Cancel" then

                cancel

            end if

        end if

    end if

    tell application "System Preferences" to quit

Actions

More Like This

  • Retrieving data ...

Bookmarked By (1)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.