OS X: Set the output device for sound effects with AppleScript

When I switch audio output devices from LaunchBar, sometimes Mavericks decides to change my system sound effects to my laptop's internal speakers rather than the selected audio device, which is what I always want.

The command line utility I use to switch audio output sources doesn't support changing the sound effects output device, so I threw together an AppleScript. The AppleScript has to pull up the Sound panel in System Preferences on the screen, so it takes a second to run, but it's still way faster than doing it manually.

FastMail: Hide unread count for folder with a Userscript

I just wrote a JavaScript snippet to hide the unread count on folders in the FastMail web interface's sidebar. I use this with Fluid.app's Userscript support.

Here's the snippet:

window.mmHide = function() {
    jQuery('.FolderTree span:contains("Folder 1 name here") + .badge').hide();
    jQuery('.FolderTree span:contains("Folder 2 name here") + .badge').hide();
    // Duplicate previous line as needed

jQuery(function() {
    setTimeout(window.mmHide, 1000);
    setInterval(window.mmHide, 10000);

You'll need to also include jQuery as a Userscript. (You can just copy/paste the minified jQuery above this snippet in your Userscript.) I'm sure there's a non-jQuery way to do this but I already have jQuery available so this was easiest.

OS X: Quickly switching audio output devices

The first step is getting AudioSwitcher. This involves downloading the Xcode project at the previous link and building to get an AudioSwitcher binary. I discovered Dr. Drang keeps a bin/ directory in Dropbox, so that's what I'm doing as well. This means that I now have the AudioSwitcher binary in ~/Dropbox/bin/, and this folder is in my $PATH.

If you've done this correctly, you should be able to run AudioSwitcher -a and see a list of devices:

$ AudioSwitcher -a
Built-in Microphone (input)
Monitor Webcam (input)
Built-in Output (output)
FiiO USB DAC-E10 (output)

Now, set up some AppleScripts to run AudioSwitcher commands. For example, to switch to my DAC, all you need is:

do shell script "/Users/max/Dropbox/bin/AudioSwitcher -s 'FiiO USB DAC-E10'"

I used the AppleScript Editor and saved the script as an application in /Applications so I can trigger it quickly with LaunchBar.

(If you trust me, you can download a compiled version of AudioSwitcher here. The standard disclaimer applies, and you really shouldn't be running software from random people on the internet.)

Zotero: quickly get links to library items using zotero:// url scheme links

I use Zotero for managing my digital library of scientific papers. Sometimes I want to link back to papers in my Zotero library from other applications, but there's no built-in way to do this.

Fortunately, Zotero is incredibly extensible and has a solid community of users who have developed various add-ins. Based on some plug-ins linked to in this forum thread, I created a Translator plug-in to get the output I wanted.

Here's how it works:

1) Select one or more papers in Zotero:

2) Press command-shift-c (on the Mac; probably ctrl-shift-c on Windows) to a "Quick Copy", or drag-and-drop the selected items onto another application.

3) You should see output like this:

zotero://select/items/0_HGFPVI65 - Sickle cell trait is not associated with endemic Burkitt lymphoma: An ethnicity and malaria endemicity-matched case-control study suggests factors controlling EBV may serve as a predictive biomarker for this pediatric cancer: Sickle cell trait and EBV loads in endemic Burkitt lymphoma - International Journal of Cancer (Mulama et al. 2014)

zotero://select/items/0_Q8GZQSJT - Rates and determinants of seasonal influenza vaccination in pregnancy and association with neonatal outcomes - Canadian Medical Association Journal (Legge et al. 2014)

zotero://select/items/0_HKRG6JXP - Capture–Recapture Method for Estimating Annual Incidence of Imported Dengue, France, 2007–2010 - Emerging infectious diseases (La Ruche et al. 2013)

zotero://select/items/0_9QST5VPW - Lifetime pesticide use and telomere shortening among male pesticide applicators in the agricultural health study - Environmental health perspectives (Hou et al. 2013)

The "zotero://" links will take you directly to the corresponding item in Zotero. 

Installation instructions

To install, grab the code from this gist and create a new file in the "translators" folder in your Zotero library folder called "modified-zotselect-link.js". Then go to your Zotero preferences, open the "Export" tab, and select "Modified ZotSelect Link" from the "Default Output Format" dropdown under "Quick Copy".

How to fix disappearing calendar invitations sent from an iCloud calendar

I recently ran into an annoying situation where invitations sent using my iCloud calendar (from iCloud.com and from Calendars.app on OS X).

Let's say I'm inviting john@example.com to an event:

But, john@example.com never receives my invitation!

What is happening is that iCloud sends calendar invitations on the server side. So when you invite someone to an event in any iCloud client (Calendar.app, BusyCal, iCloud.com's calendar, etc.), the client creates the event with the invitation on the server, and then the iCloud server sends an email to whatever address you specify with the invitation (john@example.com in this case).

This works great except when John has added john@example.com to his Apple ID. (He might do this so he can send iMessages from this email address, for example.) If John has done this, the iCloud server that usually emails invitations decides that he must be using an iCloud calendar and the invitation goes straight into his iCloud calendar notifications. It is never delivered to his inbox as an email, like would happen for non-iCloud users.


There are two ways to fix this problem, but unfortunately they are both on the recipient's side (there's nothing you can do as the event creator).

First, the recipient needs to go to https://applied.apple.com and look under "alternate email addresses" to see if his email address is listed here. If it is, there are two options:

  1. Remove the email address from the Apple ID (which may break other Apple services like iMessage using this email account); or
  2. Go to https://icloud.com, log in, click on the "Calendar" icon, click on the gear in the bottom left corner, click "Preferences", click "Advanced", and then change "Receive event notifications" from in-app to "Email to john@example.com".

Bottom line

If you use iCloud calendars to send events to people who do not use iCloud calendars, but might have associated their email address with an Apple ID, you have to manually check with them to make sure they received your event invitation. 😒

SAS: best practices for working with datasets and libraries

The way SAS handles datasets and libraries of datasets is fiddly, and can cause big problems with analysis if not handled properly. Here, I explain briefly what SAS is doing behind the scenes and how I avoid common pitfalls.

Datasets in SAS

A SAS dataset is essentially a glorified Excel sheet (each variable is a column; each record is a row). SAS saves datasets in folders somewhere on your computer (more on that below). These files have an .sas7bdat extension. The name of the dataset in SAS matches the beginning part of the filename (the part before the .sas7bdat).

Libraries in SAS

A library is a collection of multiple datasets. It maps to a folder on your computer with .sas7bdat files in it. You can give the library a short alias that's 1 to 8 letters long. You do this by placing libname baz "c:\path\to\folder"; at the top of your SAS Editor file.

In the screenshot above, the first line of the Editor window is telling SAS to look in c:\path\to\folder whenever the library "baz" is referenced. On the third line, data=baz.foo is telling SAS to look in the folder with the alias "baz" (that's c:\path\to\folder) for a file called foo.sas7bdat.

The default library (work)

Perhaps the most confusing thing in SAS is what happens when you don't specify a library name when referencing a dataset. SAS has a temporary library it calls "work", and this is used whenever you don't specify a library. Datasets in "work" are deleted every time you restart SAS.

This makes the following two editor windows exactly equivalent.

This is convenient but confusing, because proc contents data=foo; and proc contents data=baz.foo; are referencing two entirely different datasets.

Note that you can see the library and name of the dataset when you run proc contents;:

Best practices

Here's how I avoid confusion about datasets and libraries in my own code:

  • I always use the full "libname.datasetname" syntax so there's no ambiguity about whether I'm using "work" or my own manually defined library.
  • I only use "work" for something that's truly temporary. Anything I want to reference in a later proc, I save to a manually defined library so it won't disappear if I close SAS.
  • If I create a dataset based on an existing dataset, I prefix the name with "drv" like this. This stands for "derived", which tells me that it's based on another dataset and could theoretically be recreated by running my code again. In contrast, my raw datasets with no "drv" prefix can't be reproduced by running my code.
  • I use descriptive names for my derived datasets. "drv_foo1" and "drv_foo2" are not particularly helpful when trying to remember what has changed between them. "drv_foo_drop_missing" is much better.
  • SAS will allow you to run proc whatever; without explicitly specifying a dataset. I never do this because it makes it ambiguous which dataset I'm using.

An example

If I was doing a Broad Street Pump analysis, I would make a folder called c:\projects\broadstreet where I would save all my SAS Editor (.sas) files.

I would also make a subfolder called c:\projects\broadstreet\data where I would point my SAS library at with libname broadst "c:\projects\broadstreet\data"; at the top of my Editor file.

If my primary dataset was called "primary", then I would expect there to be a file on my computer called c:\projects\broadstreet\data\primary.sas7bdat.

I would then use "broadst.primary" to reference this dataset in my procs. For example: proc contents data=broadst.primary;.