Set Sound Output via AppleScript

When playing music I usually change my office MacBook’s sound output to a Sonos speaker, which is an AirPlay device. Sometimes the connection freezes and I have to reset output my default device and back to the office speaker. I wanted to automate both of these processes, so I found an AppleScript as a starting point. I modified it and created an Alfred Workflow with a keyword trigger. Here’s my version of the AppleScript. Feel free to modify it for your own use.

-- This script can be used to set/reset the sound output
-- Two devices because sometimes the AirPlay device loses connection
set myDevices to {"LG UltraFine Display Audio", "Office"}

tell application "System Settings"
	-- sometimes it is already open to Sound, which causes an error
	quit
	delay 0.3
	activate
	delay 0.3
	
	tell application "System Events"
		tell process "System Settings"
			set theWindow to first window
			delay 0.3
		end tell
		
		keystroke "Sound"
		delay 1
		
		tell application process "System Settings"
			tell radio button 1 of tab group 1 of group 2 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window "Sound"
				click
			end tell
			
			delay 0.5
			
			set theRows to (every row of table 1 of scroll area 1 of group 2 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window "Sound")
			
			repeat with myDevice in myDevices
				set device to myDevice as string
				repeat with aRow in theRows
					try
						if name of first item of static text of group 1 of UI element 1 of aRow is equal to device then
							set selected of aRow to true
							
							exit repeat
						end if
					on error
						display dialog "Error setting output sound to " & device
					end try
				end repeat
			end repeat
		end tell
	end tell
	
	quit
	
end tell

Set Mac Volume to a Specific Percentage with an Alfred Workflow

The Touch Bar on the MacBook is a pain in the ass. I’ve been getting sick of fighting with it to adjust volume and wanted an alternative to using the icon in the Mac OS menu bar. I already had some AppleScript code I use to reset volume to start my work day, so I ran with it to make a simple Alfred Workflow.

I didn’t realize how awesome this workflow would be. I’m using it all the time, even on my other Mac, which has the keyboard volume control buttons.

mac-vol-alfred-workflow-screenshot.png

Get it on GitHub.

Lock Screen and Sleep Display with Quicksilver

Looking for a quick way to lock your screen and put the display to sleep on Mac OS X using Quicksilver?

  1. Download Sleep Display.
  2. Extract the zip file and move SleepDisplay to your Applications folder.
  3. Run the SleepDisplay application at least once.
  4. Fire up Script Editor and enter the following code:

    do shell script "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -suspend"

    launch application "SleepDisplay"

  5. Save this script to a location cataloged by Quicksilver.
  6. Create a HotKey Trigger in Quicksilver to run the script. I use command-control-shift-S as my keystroke combination.

I’m running Mac OS 10.5.1 on an iMac and this works great!