Jump to content

Toggle Caffeine

Recommended Posts

Update: In case anyone is reading this old thread, go ahead and look at the Caffeinate Control Workflow instead. Here's the forum link.




Here's an applescript to toggle Caffeine.


I'll setup a github repository for it later and post a link, but for now:

tell application "Caffeine"
    if active then
        turn off
        set status to "deactivated."
        turn on
        set status to "activated."
    end if
    set output to "Caffeine has been " & status
    return output
end tell
Edited by Shawn Rice
Link to comment

Hi rice.shawn, great idea!


There's a preliminary packaged workflow for a similar workflow, with a few more features in it, hosted on github at: alfred-caffeine-controls


It seems to work okay, but I found I disliked a few things about it:

a. if I've got caffeine enabled already and give it a new time, it toggles it off

b. it only supports "on", "off" and "X" seconds, not hours


I've written fixes for both of these but haven't submitted a pull request for it yet. I'll aim to do that by the end of the week.

Link to comment

There is a built-in terminal command to OSX which does the same job as Caffeine which is as follows.



Ah, ok. Found some documentation on it, and I don't like that it can be switched off so easily (Control + C). If this hotkey could be disabled, I'd be happy to build a new workflow around this and abandon Caffeine and Should I Sleep all together.

Link to comment

I could be wrong, but I thought that the Caffeine utility is essentially a more user-friendly interface to trigger caffeinate without needing to open (or keep open) a terminal window.


Caffeine has to be the most useful little utility for its size in all of Mac utility offerings! 

Link to comment

Ah, ok. Found some documentation on it, and I don't like that it can be switched off so easily (Control + C). If this hotkey could be disabled, I'd be happy to build a new workflow around this and abandon Caffeine and Should I Sleep all together.


I like the little icon to let me know, but I might be able to part with it.


Remember, that the Ctrl+C hotkey can be avoided by making the process go into the background by just adding the "&" character afterward. So, if you want to run caffeinate in the background just type:



caffeinate &



There are many more options for caffeinate that can be found here: https://developer.apple.com/library/mac/#documentation/Darwin/Reference/Manpages/man8/caffeinate.8.html


However, it seems to be lacking something like a status check. I'd like to be able to query caffienate to see if it's running, and, if it is, how much longer it will be on for. Although, I might be missing something obvious. Am I missing something obvious, anyone?


When I get a bit of time, I'll try to write a bash script that will be able to convert times to seconds so that a user could write 1h and that would convert to 3600. I'll try to include other options like 1H or 1 H or 1 Hour or 2 Hours or even Two Hours, but the latter one might be harder. I can also include minutes in the script as well as seconds (although the seconds would be mostly useless, but it would help for usability in that people would expect it to be there). I'll even try to get it to recognize 2.5H and strings like 2 hours 15 minutes 34 seconds. This could be a useful script that could be used for many different workflows.


But, before I spend some time working it out, does anyone already have a script that does what's outlined above? I'd rather not go around reinventing wheels.


Link to comment



i haven't downloaded/seen all the workflows posted above, but FYI i had also created this workflow, mine also shows a Growl notification showing whether it's toggled ON or OFF. Might be usefull for someone...:


Applescript: https://github.com/DavidStaron/alfred-caffeine-toggle/blob/master/source/CaffeineToggleAppleScript.txt

Workflow: https://github.com/DavidStaron/alfred-caffeine-toggle/blob/master/Caffeine%20Toggle.alfredworkflow?raw=true

Link to comment

Here is a more complex one that I did with "Caffeinate" (the 10.8 command utility, not the "Caffeine" app from Lighthead).


Link: https://github.com/shawnrice/alfred-2-caffeinate-workflow/blob/master/Caffeinate.alfredworkflow


The individual files are available at https://github.com/shawnrice/alfred-2-caffeinate-workflow/, just wander through the folders.


Better explanations below.




Caffeinate v Caffeine



Pros: Caffeinate is already on Mountain Lion. With this workflow, you can operate it through just the keyboard.

Cons: No visual status check to see if it's running. Only on 10.8.



Pros: Visual status (see coffee cup on menubar). Normally you have to use the mouse to activate (although toggles exist here). Available on more than just 10.8

Cons: Yet another app to install (although it is free). Yet more clutter in the menubar. 




What I did


The scripts are written in php, and I set it up so that caffeinate takes these commands:


* status         — Shows whether caffeinate is active and returns the number of minutes if it is.
* en              — Enables caffeinate.
* en "minutes" — Enables caffeinate for X minutes.
* dis             — Disables caffeinate.
I divided it into three different php scripts.
The first one takes the commands "help" or "Help" and also takes any command that isn't recognized. Then, the help display (which are the commands above) are displayed via Large Text.
The second one is a status check that executes a few shell commands to see if caffeinate is active (using ps -ef and a regex), and, if it is, then it also pulls the command that launched caffeinate so that I can find whether or not there was an expiration time or if it was enabled indefinitely.
The third one enables and disables the utility via the command line (again). Apparently, you can launch the process several times, and so I used a killall command to take them out on disabling them. If enabling, it checks to see if the utility is already running, if so, then it doesn't launch it again but reports that it is already running.
Also, the status script and the en/dis script use the notification center to report back instead of Large Text.
So, these scripts were thrown together quickly, but they might also be good for others to look at if they need some guidance working with php scripts or other arguments as I also tried to comment them a bit. They also show how to use system commands (especially backgrounding the caffeinate command) via php, which, of course, isn't always the best idea. And these scripts could also be written better, so keep that in mind.
I'll say that I didn't like the Large Text to display the help function because I can't control the formatting well, but it seemed like the best option. The notification center cut off the text too quickly. Is there another way to display a quick help via Alfred when a command goes wrong?
-- Shawn
Link to comment


Pros: Visual status (see coffee cup on menubar). Normally you have to use the mouse to activate (although toggles exist here). Available on more than just 10.8

Cons: Yet another app to install (although it is free). Yet more clutter in the menubar.


Regarding the clutter in the menu bar, have you seen Bartender? Allows you display only what you want seen on the menu bar, hide some stuff in a toggled bar, and completely hide other stuff (ie. Caffeine). Best of all, it allows for stuff to be displayed temporarily when the icon changes (ie. when Caffeine activates or deactivates).


My menu bar with Bartender bar closed:



My menu bar with Bartender bar open:


Link to comment

I was bringing over my old Caffeine control applescript to add to my workflow, and decided to polish it up a bit and add notifications. It will toggle Caffeine on and off, either with the bare keyword, explicitly turn it on or off with those words as arguments, or with a number for the hours that the use want's it to run. You can use decimal numbers like .5 for half an hour, or 3.25 for three and a quarter hours to fine-tune how long it will be on. 


A notification will show how long it was turned on for, or if it is being turned on or off. Hope you like it.




Edit: I updated the notification to show the time when caffeine will turn itself off, if the user adds a number of hours, which might actually be useful.

Edited by rdg
Link to comment
  • 3 weeks later...

I haven't played with it too much. The added keywords could be nice, but that's only if you're not sure often like me whether or not you turned it on. I have a tendency to forget, so I like the ability to enable it when it's already enabled. I'm spending quite a bit of time reworking the "help" workflow that I just put up the other day, and I'm thinking that some of the cooler parts of that workflow might be implemented into the caffeinate workflow.

I'll check out what you did a bit more and think on it.

Link to comment
  • 4 weeks later...

Both of these caffeinate workflows are really useful... but, what I really want is simply the ability, with one command and no parameters to simply start caffeinate for a number of hours.


I'd like to be able to to start it for 5 hours so that I don't need to bother remember to stop it.


Ideally there'd be :


- caff start

- caff stop


(and perhaps in an ideal world caff start [n] - e.g. caff start 3 - would start it for a number of hours.)


I've looked at the code for both versions - and at the caffeinate documentations: but can't figure it out at all.


Can anyone help?

Link to comment

I've been tinkering around with interpreters to process time-based arguments with more granularity, and I've been playing with ways to run scripts to configure workflows themselves. When I get both of those tools abstracted well enough, I'll update both the caffeine and caffeinate workflows to employ them. After that, the workflows should allow you to configure the default behavior as you'd like it to.


For now, what you can do is to go into the caffeinate workflow code, actually go into the caffeinate.en-dis.php file and find the line "caffeinate -dsi >/dev/null 2>&1 &" Then change that to "caffeinate -dsit 33600 >/dev/null 2>&1 &" After that, when you type "caff en" it'll enable caffeinate for 5 hours (the argument is in units of seconds). If you just make that tweak to the script, however, you won't be able to activate caffeinate indefinitely, but you could always just run "caff en 9999" to make it run for about 14 months, which should be good enough.


I'll post an update when the new features are included.



Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...