Josh Dick portrait Josh Dick

Sleep Displays with Alfred v2

Because my keyboard doesn't have an eject key.

Introduction

I can’t use a Mac for more than a few minutes without somehow interacting with Alfred. I use an excellent Alfred extension by Tony Wang called Sleep Display. When triggered via a hotkey, the extension powers off all connected displays without actually suspending/sleeping the computer. While it’s true that simultaneously pressing Control+Shift+Eject also accomplishes the same thing, the extension is useful for those whose keyboards don’t have an eject key.

The first public beta of the next version of Alfred, Alfred v2, became available a few weeks ago. Since v2 has done away with extensions and has replaced them with a feature called “Workflows”, Tony’s extension isn’t compatible with v2. Further, v2 doesn’t natively support shutting off displays as a built-in “System Command” when building Workflows (yet!).

To fill the gap, I decided to create a v2 Workflow called “Sleep Displays”. (Yes, I added an “s” to the the name of Tony’s original extension. Yes, I’m creative.)

How It Works

The Workflow contains a binary program, directly lifted from Tony’s extension, that was built using the following code found at Stack Overflow:

#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>

/* Returns 0 on success and 1 on failure. */
int display_sleep(void)
{
    io_registry_entry_t reg = IORegistryEntryFromPath(kIOMasterPortDefault, "IOService:/IOResources/IODisplayWrangler");
    if (reg) {
                IORegistryEntrySetCFProperty(reg, CFSTR("IORequestIdle"), kCFBooleanTrue);
                IOObjectRelease(reg);
        } else {
                return 1;
        }
        return 0;
}

According to the Stack Overflow answer, the code can be compiled on Mac with gcc using the flags -framework CoreFoundation -framework IOKIT.

Can I Download It Already?

You sure can. Grab it right here.

That’s All, Folks

Lacking the ability to sleep displays via a hotkey was the last thing preventing me from using Alfred v2 full time. Now I can do that with v2, and so can you.

[ ↩ all writing posts ]