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

Mac Screenshot Tips

I use the Mac screenshot utility a lot, but have never thought of using it as a ruler. I’ve always used a browser extension to measure pixels on the screen. If you’re not using this utility, you initiate it by pressing command + shift + 4 on your keyboard.

I learned a few other tricks from the thread. Once you start dragging your mouse to create the rectangle:

  • Press shift to lock in an axis as you continue sizing the area.
  • Press space to move the rectangle to a new location.

Do you have any other tips?

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.

Reload Your Mac Camera

Last month I bought some simple stickers to cover up the cameras on my laptops. My friend Ingrid bought some fancy covers at about the same time. Ever since, we’ve both been having issues with MacOS recognizing the camera when uncovered. Rebooting the Mac resolved the issue so I figured something was jacked up internally with sensors. Found a solution, but wanted to make it easier than typing in the terminal commands.

I’m an Alfred user, so I made a workflow (available on Github). Save the file and open it, which should import into Alfred. Change the keyword (default is camera) in Alfred if you want.

If you don’t use Alfred, I also made a command script you can launch instead of manually typing in the commands. It’s also on Github. Use File->Save Page in your browser. Open up a terminal window and cd to wherever you saved the reload-camera.command file. Change the execute permissions on the file by running chmod 744 reload-camera.command. Then you should be able to double-click on the file to run the script.

By the way, yes, you probably should cover up your camera, especially if you never use it.

Micro SD Card Imaging

I’ve been cloning micro SD cards to create backups and writing images to them during setup of Raspberry Pis a lot lately. My tool of choice has been the command line tool dd on the Mac (man page). Every tutorial referencing imaging for Raspberry Pis I’ve seen tells you to clone a drive with:

sudo dd if=/dev/disk2 of=~/Downloads/filename.img

Write an image to a drive with:

sudo dd if=~/Downloads/filename.img of=/dev/disk2

Those commands work, but are painfully slow. After firing off an image process, I came back over 3.5 hours later and it wasn’t finished! The write speed was about 0.25 MB/second.

I found a thread on the Raspberry Pi Forums discussing this. While none of the suggestions worked exactly for me, they did lead me to the solution. Here is what worked to clone a drive:

sudo dd bs=1m if=/dev/rdisk2 of=~/Downloads/filename.img

And then the opposite to write an image back to a drive:

sudo dd bs=1m if=~/Downloads/filename.img of=/dev/rdisk2

Now I got about 10 MB/second writing that image to disk and it completed in less than 7 minutes! The read speeds I’m getting are over 15 MB/second when cloning a disk. For specifics read why /dev/rdisk is so much faster than /dev/disk.

** Note: all of my examples use disk2 because that’s what I usually get on my machine. Your setup may vary. Run diskutil list to see a list of your drives and determine the correct number. You do still need to diskutil unmountDisk /dev/disk like the other tutorials mention.

I’ve actually stopped directly using dd since I started writing this post. Now I’m using ApplePi Baker, an app built on top of dd with a simple to use GUI.

I have experienced an issue using different brands of micro SD cards. Even though a couple were both 16 GB, they were slightly different sizes. Write an image file too large to a disk and no boot for you. I found rpi-clone to handle this. The next time I’m not using a headless Raspbian-lite I’ll try the built-in SD card copier, which ships with the OS.

Another app, PiBakery, looks like a neat way to automate and customize image creation, but I haven’t tried it yet.

3 MacBook Pros

My new MacBook Pro came yesterday. While not much felt different in my last laptop upgrade, I could tell this one was completely different right away. The size and weight remind me of the MacBook Air I got in 2011. The new color is fresh. Trackpad “clicks” feels familiar, maybe because of the new iPhone 7 home “button.” The […]

My First Mac Was…

The Mac turns 30 today. My first Mac was the original black MacBook in 2006 and I haven’t looked back since. What was your first Mac?

Ten years ago on the 20th anniversary, Steve Jobs said this in an interview

Like, when you make a movie, you burn a DVD and you take it to your DVD player. Someday that could happen over AirPort, so you don’t have to burn a DVD — you can just watch it right off your computer on your television set.

Now we watch movies on our TVs through Netflix or the iTunes Store with the Apple TV and don’t think twice about it. It’s exciting to think about what Apple is working on that will come out 3 or 5 years from now.