Jump to content


Photo

Toggle Caffeine

toggle

  • Please log in to reply
23 replies to this topic

#1 Shawn Rice

Shawn Rice

    Advanced Member

  • Member
  • PipPipPip
  • 651 posts
  • LocationBrooklyn, NY

Posted 25 February 2013 - 04:47 PM

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."
    else
        turn on
        set status to "activated."
    end if
    
    set output to "Caffeine has been " & status
    
    return output
    
end tell
 

Edited by Shawn Rice, 11 May 2014 - 06:24 AM.


#2 ChrisB

ChrisB

    Newbie

  • Member
  • Pip
  • 7 posts

Posted 26 February 2013 - 01:50 AM

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.



#3 Boarder24

Boarder24

    Member

  • Member
  • PipPip
  • 41 posts

Posted 27 February 2013 - 01:15 PM

Hey Shawn,

 

I actually made a workflow like this just the other day!

You can download it here: http://d.pr/f/Ev5p

 

--

Jacob


Mountain Lion, OS X 10.8.3

 

 


#4 GamerChase

GamerChase

    Member

  • Member
  • PipPip
  • 102 posts

Posted 27 February 2013 - 02:34 PM

Using a few tweaks of my own to streamline to my own liking, I'm using this workflow.

 

Credit goes to the work done by Boarder24 and Jeff Johns' file in Github. Thanks, guys! :)


twitter-follow.png


#5 Boarder24

Boarder24

    Member

  • Member
  • PipPip
  • 41 posts

Posted 28 February 2013 - 06:13 AM

Credit goes to the work done by Boarder24 and Jeff Johns' file in Github. Thanks, guys! :)

 

Thanks GamerChase! This was one of my first workflows, really proud to see it helped form a awesome finished product.


Mountain Lion, OS X 10.8.3

 

 


#6 Peter

Peter

    Newbie

  • Member
  • Pip
  • 3 posts

Posted 03 March 2013 - 03:02 PM

If being activated through Alfred, would it not be cleaner and more direct to make use of 10.8's caffeinate? One less application to install is all I'm thinking! :)



#7 GamerChase

GamerChase

    Member

  • Member
  • PipPip
  • 102 posts

Posted 03 March 2013 - 03:24 PM

If being activated through Alfred, would it not be cleaner and more direct to make use of 10.8's caffeinate? One less application to install is all I'm thinking! :)

 

Ummm... huh? Please elaborate.


twitter-follow.png


#8 Peter

Peter

    Newbie

  • Member
  • Pip
  • 3 posts

Posted 03 March 2013 - 03:40 PM

Ummm... huh? Please elaborate.

 

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

caffeinate


#9 GamerChase

GamerChase

    Member

  • Member
  • PipPip
  • 102 posts

Posted 03 March 2013 - 04:07 PM

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

caffeinate

 

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.


twitter-follow.png


#10 ChrisB

ChrisB

    Newbie

  • Member
  • Pip
  • 7 posts

Posted 03 March 2013 - 08:11 PM

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! 



#11 Sebastian

Sebastian

    Member

  • Member
  • PipPip
  • 51 posts

Posted 03 March 2013 - 09:02 PM

I made a similar workflow based on caffeinate. I polished the worklow and uploaded it since there seems to be interest: 

 

Caffeinate.alfredworkflow

 

There is only one keyword "caff" which starts and stops caffeinate depending wether it is running in the background.

Feel free to improve, criticize or take the code for something else...



#12 Shawn Rice

Shawn Rice

    Advanced Member

  • Member
  • PipPipPip
  • 651 posts
  • LocationBrooklyn, NY

Posted 03 March 2013 - 10:04 PM

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 &
 

Simple.

 

There are many more options for caffeinate that can be found here: https://developer.ap...ffeinate.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.


 


Find and share your workflows and themes on www.packal.org.

Some Shared Workflows: Caffeinate Control | Alfred Bundler | Alfred CronWorkflows Help Workflow | F.luxBarrel Roll | Sudoers
Web: Github


#13 David

David

    Member

  • Member
  • PipPip
  • 22 posts
  • LocationNetherlands

Posted 09 March 2013 - 03:13 PM

Hi,

 

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/D...AppleScript.txt

Workflow: https://github.com/D...rkflow?raw=true



#14 Shawn Rice

Shawn Rice

    Advanced Member

  • Member
  • PipPipPip
  • 651 posts
  • LocationBrooklyn, NY

Posted 10 March 2013 - 12:42 AM

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/s....alfredworkflow

 

The individual files are available at https://github.com/s...inate-workflow/, just wander through the folders.

 

Better explanations below.

 

---

 

Caffeinate v Caffeine

 

Caffeinate

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.

 

Caffeine

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?
 
---
 
Screenshots:
 
 
Launch
caffeinate-launch.PNG
 
 
Enable
 
caffeinate-activated.PNG
 
 
Status
 
caffeinate-activated.PNG
 
 
Help
 
caffeinate-help-large-text.PNG
 
 
 
 
-- Shawn

Find and share your workflows and themes on www.packal.org.

Some Shared Workflows: Caffeinate Control | Alfred Bundler | Alfred CronWorkflows Help Workflow | F.luxBarrel Roll | Sudoers
Web: Github


#15 Shawn Rice

Shawn Rice

    Advanced Member

  • Member
  • PipPipPip
  • 651 posts
  • LocationBrooklyn, NY

Posted 10 March 2013 - 07:10 PM

Updated the previous caffeinate workflow. I forgot to add in some flags (caffeinate is now activated with -dsi). The files at the previous links have been updated.


Find and share your workflows and themes on www.packal.org.

Some Shared Workflows: Caffeinate Control | Alfred Bundler | Alfred CronWorkflows Help Workflow | F.luxBarrel Roll | Sudoers
Web: Github


#16 GamerChase

GamerChase

    Member

  • Member
  • PipPip
  • 102 posts

Posted 10 March 2013 - 07:38 PM

Caffeine

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:

Menu%20bar%201.png

 

My menu bar with Bartender bar open:

Menu%20bar%202.png


  • 300DDR likes this

twitter-follow.png


#17 Shawn Rice

Shawn Rice

    Advanced Member

  • Member
  • PipPipPip
  • 651 posts
  • LocationBrooklyn, NY

Posted 10 March 2013 - 08:01 PM

Yes! I found Bartender not too long ago. A few years ago, I had searched for a utility that would do the same thing, but none existed. I might have jumped up and down when I finally found Bartender a few months ago...

 

Thanks for the tip.


Find and share your workflows and themes on www.packal.org.

Some Shared Workflows: Caffeinate Control | Alfred Bundler | Alfred CronWorkflows Help Workflow | F.luxBarrel Roll | Sudoers
Web: Github


#18 GamerChase

GamerChase

    Member

  • Member
  • PipPip
  • 102 posts

Posted 10 March 2013 - 09:06 PM

Hahaha! Yeah, I'm pretty sure I did something similar when I found it a while back.  :P


twitter-follow.png


#19 rdg

rdg

    Newbie

  • Member
  • Pip
  • 7 posts

Posted 15 March 2013 - 01:47 AM

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.

 

http://db.tt/HX7mny6g

 

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, 15 March 2013 - 09:17 PM.

  • 300DDR likes this

#20 Sebastian

Sebastian

    Member

  • Member
  • PipPip
  • 51 posts

Posted 01 April 2013 - 10:39 AM

My Caffeinate workflow (post 11) has been updated to include Alleyoop updates.

 

Shawn, did you give my worklfow a try? I don't think that two keywords for starting/stopping are necessary. 





Also tagged with one or more of these keywords: toggle

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users