Job For Automator?

Discussion in 'Mac Programming' started by nim6us, Jul 5, 2014.

  1. nim6us macrumors member

    nim6us

    Joined:
    Nov 20, 2012
    #1
    Okay I've got an issue I'm hoping someone here will have solution for. I've got a MacBook Pro that I use for personal and work. It's only got 1 Thunderbolt port on it, but I need two monitors at the office. The best solution I found was hooking one monitor to the Thunderbolt, and then using AirPlay on an Apple TV to extend my desktop. This has worked out great!

    BUT

    There is one tiny annoyance. Whenever I turn on AirPlay on the MacBook I lose sound. The computer thinks I want the sound output to be the AppleTV. I've got pretty quick at it using the hotkeys, but essentially I have to open the System Preferences and change Sound Output back to the Headphone Port. In my years working w/ computers I've found if there's a repetitious task you find yourself having to endure, there's more than likely a way to automate the operation.

    What I want to accomplish is when I do AirPlay from my MacBook to my AppleTV to extend the monitors, have an action that will be triggered to change the Sound Output back to Headphone Port. Also my AppleTV at the office is called "Office AppleTV", if I could make this task exclusive to only when I AirPlay to this specific AppleTV that would be ideal!

    My thought is to use Automator, but I have zero experience with the app and wanted to get some pointers on how to set this up. Or if perhaps there's a better way to make this work I'm all ears.
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    So you have two things you need:
    1 - Detect when the Sound Output has changed.
    2 - Change the Sound Output

    I'm not sure of a way to do #1 without polling (maybe you could have a file listener on the proper plist file?) I don't think Automator can do this - AppleScript may be able to. You might have to actually write a small program (in C or Obj-C or C++ or Java or Python or any other language, probably,) to do this.

    #2 I don't think can be done by Automator, either, but AppleScript can probably handle it. Or any actual programming language. Again, you may have to modify the plist file directly instead of going through some nice APIs to set the preference.
     
  3. numero macrumors regular

    Joined:
    Jul 23, 2002
    Location:
    OR
    #3
    I've got this working except for the last step. I can't get the row to click. On my system I only have 1 row (Internal Speakers) so I don't have a good test anyway.

    My thought was this would be saved as an application and put in your dock. When you connect up the Apple TV you would have to manually click on the item in the dock to launch the script, but from there on out it would be automatic.

    Maybe someone can finish the last step and let me know their solution.

    The link at the top of the script is to turn on GUI Scripting. You need this done or nothing will work.

    Code:
    --http://www.macosxautomation.com/mavericks/guiscripting/
    
    tell application "System Events" to tell process "System Preferences"
    	set frontmost to true
    	delay 1
    	tell window 1
    		tell toolbar 1
    			tell group 2
    				tell group 1
    					click button 1
    				end tell
    			end tell
    		end tell
    		tell scroll area 1
    			click button 16
    		end tell
    		tell tab group 1
    			click radio button 2
    			tell scroll area 1
    				tell table 1
    					-- this part is broken
    					--tell table row 1
    					--perform action "AXShowMenu" of table row 1
    					--end tell
    					--click (text field where attribute "AXvalue" is "Internal Speakers")	
    				end tell
    			end tell
    		end tell
    	end tell
    end tell
    
    -- When the rest works uncomment these
    --tell application "System Preferences"
    --	quit
    --end tell
     
  4. nim6us thread starter macrumors member

    nim6us

    Joined:
    Nov 20, 2012
    #4
    Wow thanks! I'm on the road but when I get back to my desktop setup I'll test and let you know. Looks like you put a decent amount of work into this, thank you for taking the time to help :D
     
  5. kryten2, Jul 12, 2014
    Last edited: Jul 13, 2014

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #5
    Try this :

    Code:
    tell application "System Preferences"
    	--activate
    	set current pane to pane id "com.apple.preference.sound"
    end tell
    
    tell application "System Events" to tell process "System Preferences"
    	click radio button "Output" of tab group 1 of window "Sound"
    	set selected of row 4 of table 1 of scroll area 1 of tab group 1 of window "Sound" to true
    end tell
    Note : As you only have 1 row (Internal Speakers) on your system row 4 would be row 1.

    Note : Change row 4 in the code segment to the row the Headphone is on e.g. if it's on the third row that would be row 3. If you want to see what is going on then uncomment the --activate statement by removing the two dashes. With the statement commented it will run unobtrusively.

    Testing was done on Mavericks 10.9.4. YMMV.
     
  6. numero macrumors regular

    Joined:
    Jul 23, 2002
    Location:
    OR
    #6
    Nice! Thanks for improving. I didn't know how to get that row to be selected.
     

Share This Page