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 http://lightheadsw.com/caffeine/, 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
	else
		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.

Thanks.

-Chris

Hi Chris,

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

Glad you found it useful,

Tim

2 Likes

Useful : -)

FWIW, a JavaScript for Automation translation:

    (function () {
        'use strict';

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


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

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

            a.includeStandardAdditions = true;
            a.displayNotification(
                "Caffeine " + (caf.active() ? "ON" : "OFF")
            );
        }
    })();

or, more sparsely:

    (function (a) {


        a.active() ? a.turnOff() : a.turnOn();


         })(Application("Caffeine"));

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.

1 Like

Ditto!

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.

This macro seems to work well for my needs - thank you! Might also make a copy without the prompt and just set the variable to a desirable interval.

To ensure it's mirrored somewhere, I've uploaded an exact copy of the author's 2013 macro below.

Courtesy Patrick Welker (GitHub: patrickwelker/KeyboardMaestro):

Caffeinate.kmmacros (3.1 KB)

(Will email Patrick a link to this comment now... thank you!)