Latewire
We never sleep.
 
Register (free)
Grab Twitter Feed: Latewire / Coughlin
Username: Password: NVR4GET
«· Previous Article || Next Article ·»
AppleScript: Toggle Between Audio Outputs Via Hotkey - OS X

Daniel Roe
Poster: Daniel Roe @ Sat Jan 28, 2012 6:42 am

In a previous post, I discussed using FastScripts in combination with a simple AppleScript to assign a hot-key to accomplish annoying tasks.

I frequently change my sound output to the TV I have in my other room, and back again to my main office. It gets annoying to have to use the mouse, so using FastScripts and a modified AppleScript I found online, I toggle between audio sources.

This script currently only works with toggling between two outputs: "Line Out" and "Digital Out", but is easily made to toggle between any number of outputs. To add more/different outputs to switch between, simply add to the "theOutputs" list at the top by putting the name of the output in quotes, and separated by a comma. Please note that the outputs you wish to toggle between must be written EXACTLY as they are in your Sound Preference Pane (read: Case Sensitive!), but can be in any order you choose:



Code:
set theOutputs to {"Digital Out", "Line Out"}

tell application "System Events"
   set frontmostapp to item 1 of (get name of processes whose frontmost is true)
end tell
tell application "System Preferences" to activate
tell application "System Events"
   get properties
   tell process "System Preferences"
      if (menu item "Sound" of menu "Window" of menu bar 1) exists then
      else
         click menu item "Sound" of menu "View" of menu bar 1
         delay 1
      end if
      set theRows to every row of table 1 of scroll area 1 of ¬
         tab group 1 of window "sound"
      click radio button "Output" of tab group 1 of window "Sound"
      set nextOutput to ""
      set nextIndex to 0
      (* Obtain Selected Output / Choose Next Output based on Selected*)
      repeat with aRow in theRows
         if selected of aRow is true then
            set currentlySelectedOutput to (value of text field 1 of aRow as text)
            if currentlySelectedOutput is in theOutputs then
               set selectedIndex to my getitemindex(theOutputs, currentlySelectedOutput)
               if (count of theOutputs) is equal to selectedIndex then
                  set nextIndex to 1
               else
                  set nextIndex to (selectedIndex + 1)
               end if
            else
               set nextIndex to 1
            end if
            set nextOutput to (get item nextIndex of theOutputs)
            exit repeat
         end if
      end repeat
      (* Select the chosen Output *)
      repeat with aRow in theRows
         if (value of text field 1 of aRow as text) ¬
            is equal to nextOutput then
            set selected of aRow to true
            exit repeat
         end if
      end repeat
      
   end tell
end tell
# tell application "System Preferences" to quit
tell application frontmostapp to activate


on getitemindex(this_list, this_item)
   repeat with i from 1 to the count of this_list
      if item i of this_list is this_item then return i
   end repeat
   return 0
end getitemindex

(85,819)
Keywords: Applescript  Mac Os  Mac  Apple  Macintosh  Code 
Comments: 0  •  Post Comment  •  Share Share Top
«· Previous Article || Next Article ·»

Not working? Try this.
Bio: Daniel Roe
User avatar
Site Admin


Location:
Cake City

Website:
http://www.facebook.com/latewire

Occupation:
Daniel is Medical Resident from the southwest US. Prior to medicine, he worked in IT as a consultant, programmer, web designer/developer, and technician.

Interests:
Health, music, economics, libertarianism, computers


Best Of Latewire
- 3rd Party Payer: How Gov't Made Healthcare Unaffordable
- Is Russia behind the global terrorist epidemic?
- Intern Hell
- S&P's Torrid Love Affair With The Government
- Green Subsidies Destroying Energy Market & Environment
- Why Economic Stimulus Doesn't Work (Latewire Original Video)
- The Season of Reason
- Happy Holidays from your pals at Latewire
- The Healthcare Disaster and Why Obamacare Will Make It Worse
- Video: Interest Rates, The Fed, and History Repeating
- Urban agriculture : Planning your vegetable garden part V
- Urban Agriculture : Planning your Vegetable Garden part IV
- Why The Government Wont Rescue The Dollar
- How the US Government Is Destroying the Dollar -Latewire Vid
- Governing Crazy: Broken Minds & Alcohol
- Urban Agriculture : planning your vegetable garden part III
- Urban Agriculture : Planning Your Vegetable Garden part II
- Urban agriculture for self-reliance : garden planning pt 1
- Why Bailouts Are Stupid (Illustrated Version)
- The Great Depression II, The Making of
- MySpace: A Place for The Damned. Part 1
- Happy Birthday, Latewire! I wrote you a song.
- The Worst Movie Ever!
- Best Long Island Iced Tea EVER
- Everyone loves a top 10 list.
- The Good Ol' Days
- I got your subject right here:
- Male members must represent, like you didn't know.
- Photo Radar: An Extravagant Way To Screw Ourselves
- The Inadequacy of Hope
- TV Companies WANT YOU... to Pirate
- The Gettin' Place
- 46 & ***
- The White Whale


Top 15 Keywords
- Alcohol (12)
- Apple (18)
- Bailouts (31)
- Bernanke (11)
- Economics (27)
- Food (22)
- Goth (13)
- History (12)
- Mac (12)
- Music (23)
- Obama (15)
- Poetry (19)
- Poison (11)
- Slavery (12)
- Snakes (29)

Links
- Latewire Latewear-Shirts&Stuff
- Snatchies Underthings
- Grief Brothers Band
Our Parked Domains:
- Latewire Video
- Poison-Free Diet
- Inflation Hell
- Policy Horn(DUPE)
- Policy Walk(DUPE)
- Policy Reader(DUPE)
- Faux Future(DUPE)
- Scarewire(DUPE)
- Urban Agriculture: The Road to Self-Reliance
- Urban Agriculture: The Road to Self-Reliance(DUPE)
- Urban Agriculture: The Road to Self-Reliance(DUPE)
- Urban Agriculture: The Road to Self-Reliance(DUPE)
- Interview With Dr. LSD
- Dr. LSD(DUPE)

© 2008 Latewire.com