Keep your Mac awake with Caffeine

This is an AppleScript I wrote that works in conjunction with a free little utility called Caffeine (you can download it from the developer at, and it is also available on the Mac App Store). Caffeine is a Menubar app that effectively keeps your screensaver and power-saving functions from activating, thereby keeping your screen, and your Mac, awake. I have Caffeine set to load on boot up for ease of use, but you don't have to if that's not your preference, though you will need to have Caffeine running in order for this particular script to work as I've written it.

-- Author: Tim Bolhouse
-- Created: 26 August 2014 3:09:14 pm
-- Modified: 07 April 2015 2:57:23 am
-- Application: Caffeine
-- Task: Activate/Deactivate 1.1.3
-- Tags: @AppleScript, @Script, @Screensaver

-- Script Name: Activate/Deactivate Caffeine
-- This script turns on or turns off the Caffeine app depending on its current state.
-- It overrides any default time setting you have set in Caffeine Preferences.

(* If you want to activate the app for a specific length of time and then 
have it deactivate automatically, replace "*turn on*" with "*turn on for 0000*", 
where 0000 being the number of seconds you wish to keep Caffeine active.  
Can be as short as 1 second or as long as you want.  Leading zeros are not required. *)

tell application "Caffeine"
	if active = false then
		turn on
		turn off
	end if
end tell

This is what it will look like in Keyboard Maestro:

Hey Tim,

I didn’t realize Caffeine was scriptable.

The UI on El Capitan is a little flakey, so this will let me set it without having to mess with the UI.



Hi Chris,

Wow, I taught @ccstone something new?! Inconceivable!! :smile:

Glad you found it useful,



Useful : -)

FWIW, a JavaScript for Automation translation:

    (function () {
        'use strict';

        var blnNotify = false, // edit to true for notifications
            caf = Application("Caffeine");

        if ( caf.turnOff();
        else caf.turnOn();

        if (blnNotify) {
            var a = Application.currentApplication();

            a.includeStandardAdditions = true;
                "Caffeine " + ( ? "ON" : "OFF")

or, more sparsely:

    (function (a) {
 ? a.turnOff() : a.turnOn();


I put this one in ... works great. Many thanks.

However, I did not want to have to go first look at caffeine's state. So I did a quick modification to simply have it turn Caffeine on or off.

Per most of these scripts, I type a string into Alfred. So I added the string trigger.

The result is the macro is attached.

The trigger is simply to type "cafon" and "cafoff"

Caffeine KM with Alfred.kmmacros (4.6 KB)

There is an old macro that doesn’t use Caffeine here:

Slightly different functionality.


Is there a way to find out if Airplay is currently in use? Because this would be handy in combination with the caffeine script, to prevent the screensaver from starting when I’m in a conference.