Skip to content

Commit

Permalink
updates to work with alfred3 out of the box
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnrice committed Jul 28, 2016
1 parent 46d42e4 commit 52a692c
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 32 deletions.
Binary file modified Caffeinate Control.alfredworkflow
Binary file not shown.
6 changes: 3 additions & 3 deletions action.sh
Expand Up @@ -5,13 +5,13 @@

. library.sh

data=`getDataDir`
pref="$data/preferences"
data="${alfred_workflow_data}"
pref="${data}/preferences"

if [ ! -f "$pref" ]; then
echo "i" > "$pref"
fi
pref=`cat "$pref"`
pref=`cat "${pref}"`
# echo $pref

if [[ -z $1 ]]; then
Expand Down
Binary file modified applescripts/conf-and-help.scpt
Binary file not shown.
42 changes: 36 additions & 6 deletions library.sh
Expand Up @@ -12,6 +12,9 @@ secondsToHumanTime() {
# empty
text="indefinitely."
else
# We now have the amount of time left.
# left=`expr $total - $s`

# Let's reset some variables.
h=0; m=0; s=0;
((hours=$total/3600))
Expand Down Expand Up @@ -83,11 +86,11 @@ parseTime() {
echo `parseTimeArg $arg`
exit
elif [[ $count -eq 2 ]]; then
t1=`parseTimeArg ${args[0]}h`
t2=`parseTimeArg ${args[1]}m`
(( time=$t1 + $t2 ))
echo $time
exit
t1=`parseTimeArg ${args[0]}h`
t2=`parseTimeArg ${args[1]}m`
(( time=$t1 + $t2 ))
echo $time
exit
else
# there are more than three arguments, so just make this indefinite
echo "0"
Expand Down Expand Up @@ -146,6 +149,13 @@ if [[ -z $alfred_workflow_data ]]; then
fi
fi

if [[ ! -e "${alfred_workflow_data}" ]]; then
mkdir -p "${alfred_workflow_data}"
fi
if [[ ! -e "${alfred_workflow_cache}" ]]; then
mkdir -p "${alfred_workflow_cache}"
fi

VPREFS="${alfred_workflow_data}/"
NVPREFS="${alfred_workflow_cache}/"

Expand Down Expand Up @@ -199,5 +209,25 @@ getBundleId() {
###############################################################################
getDataDir() {
local BUNDLEID=$(getBundleId)
echo "$NVPREFS$BUNDLEID"
echo "$alfred_workflow_data"
}

### Convert etime (from `ps`) to seconds. I love stackoverflow.
#https://stackoverflow.com/questions/14652445/parse-ps-etime-output-and-convert-it-into-seconds
function etimeToSeconds() {
echo $1 | awk -F $':' -f <(cat - <<-'EOF'
{
if (NF == 2) {
print $1*60 + $2
} else if (NF == 3) {
split($1, a, "-");
if (a[2] > 0) {
print ((a[1]*24+a[2])*60 + $2) * 60 + $3;
} else {
print ($1*60 + $2) * 60 + $3;
}
}
}
EOF
)
}
26 changes: 9 additions & 17 deletions readme.md 100644 → 100755
@@ -1,27 +1,19 @@
## Requirements ##
[Caffeinate](https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man8/caffeinate.8.html) is a native OS X command line utility that solves the problem of your Mac constantly falling asleep on you. This is especially annoying when you're trying to read something, and your screen keeps dimming and then turning off. This workflow allows you to interface with Caffeinate so that you needn't open the terminal and send commands. Caffeinate was introduced in Mountain Lion (10.8), and it basically replicates what the Caffeine utility ([web](http://lightheadsw.com/caffeine/) | [app store](http://itunes.apple.com/us/app/caffeine/id411246225) from [Lighthead Software](http://lightheadsw.com/)) does.

OS X 10.8.x (Mountain Lion) or higher
Option Configuration

## Description ##
You can configure how you want Caffeinate to work for you. Do you want to keep the system awake? Just the display? Everything? Just type "caff configure" (or "caff c"), and you can set the options easily. If you want to change how it works later, then just run the config again. Change however frequent you want!

In Mountain Lion (10.8), Apple introduced a utility called "Caffeinate" that basically replicates what the Caffeine utility ([web](http://lightheadsw.com/caffeine/) | [app store](http://itunes.apple.com/us/app/caffeine/id411246225) from [Lighthead Software](http://lightheadsw.com/) does.
Defaults

## Installation ##
Download the [workflow](http://www.packal.org/workflow/caffeinate-control) file and double click it.
We default to "i" or just to keep the system from idling (which lets you display turn off). Just run the config and choose more options to as you please. Multiple options are available by pressing cmd while clicking.

## Commands ##
Commands

Just type "caff" to get started. It will tell you the status and give you the most relevant option first.

Otherwise:
"caff e" enables caffeinate indefinitely
"caff d" disables caffeinate
"caff 15 2" enables caffeinate for 15 hours and 2 minutes
"caff 23" enables caffeinate for 23 minutes
"caff 2h" enables caffeinate for 2 hours
"caff configure" opens the configuration dialog
"caff help" opens the help dialog.

See screenshots in directory.
Example Arguments

"caff e" enables caffeinate indefinitely "caff d" disables caffeinate "caff 15 2" enables caffeinate for 15 hours and 2 minutes "caff 23" enables caffeinate for 23 minutes "caff 2h" enables caffeinate for 2 hours "caff configure" opens the configuration dialog "caff help" opens the help dialog.

Note: v1 and v2 were written in PHP. v3 is now simply a bash script and is much faster.
20 changes: 14 additions & 6 deletions script.sh
Expand Up @@ -5,29 +5,37 @@
################################################################################

. library.sh
# Data dir is made in library.sh
datadir="${alfred_workflow_data}"
datadir=`getDataDir`

if [ ! -d "$datadir" ]; then
mkdir "$datadir"
fi

#### Start Caffeinate Script Filter
arg=$1

# first, let's do something if there are no commands.
if [[ -z $arg ]]; then
cmd=`ps -eo etime,args|grep caffeinate|grep -v grep`
cmd=$(ps -eo etime,args|grep caffeinate|grep -v grep|sed -e 's|^[[:space:]]*||')

if [[ -z "$cmd" ]]; then
addResult "" "configure" "Configure Caffeinate Control" "Configure how you want your computer to stay awake." "images/configure.png" "yes" "configure"
addResult "" "help" "Caffeinate Control Help" "Read the help files for Caffeinate Control." "images/blue-question.png" "yes" "help"
addResult "" "enable" "Enable Caffeinate forever" "Never let your computer sleep." "images/green-coffee.png" "yes" "enable"
addResult "" "status" "Caffeinate is not running" "Your computer is tired." "images/off.png" "yes" "caf"
else
# Just grab how many seconds caffeinate was activated for
[[ $cmd =~ (caffeinate -t )([0-9]*)([a-z -]*) ]]
total=${BASH_REMATCH[2]}
[[ $cmd =~ ([0-9:]*)( )(caffeinate -t )([0-9]*)([a-z -]*) ]]
total=${BASH_REMATCH[4]}
# Convert how long it has been running for to seconds
running=$(etimeToSeconds ${BASH_REMATCH[1]})
# Take the difference
total=$((total - running))

if [[ -z "$total" ]]; then
text="Currently, your computer will never sleep."
else
string=`secondsToHumanTime $total`
string=$(secondsToHumanTime $total)
text="Caffeinate will be active for another $string"
fi
addResult "" "help" "Caffeinate Control Help" "Read the help files for Caffeinate Control." "images/blue-question.png" "yes" "help"
Expand Down
16 changes: 16 additions & 0 deletions workflow.ini
@@ -0,0 +1,16 @@
;;;;;
; `workflow.ini` generated by Alphred v0.9.3
; at 2015-Sep-14 08:50:16
;;;;;


[packal]
github = "shawnrice/alfred-2-caffeinate-workflow"
forum = "http://www.alfredforum.com/topic/3418-caffeinate-control-v-2-released/"
tags = "sleep,display,os x annoyance fixers,caffeinate"
categories = "Productivity,Utilities"
osx = "10.11,10.10,10.9,10.8"
description_file = "/Users/Sven/Downloads/packal_data/README.md"

[workflow]
version = "3.4.0"

0 comments on commit 52a692c

Please sign in to comment.