Last active
April 14, 2018 11:06
-
-
Save dentex/fda859c26b8068492a1a7e9f206114d8 to your computer and use it in GitHub Desktop.
Get a popup window as a timer using zenity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# (C) 2014 Roland Latour, gpl v2 | |
# from http://user.cavenet.com/rolandl | |
# uses zenity to create/move popup window as a timer | |
################################################################### | |
# modified by Samuele Rini | |
# | |
# https://github.com/dentex/ | |
# https://dentex.github.io/ | |
# | |
# original source at http://user.cavenet.com/rolandl/timer.txt | |
################################################################### | |
# | |
#Config-no-term | |
# | |
IMAGE_FILE="/home/samuele/.face" | |
AUDIO_FILE="/usr/share/sounds/linuxmint-gdm.wav" | |
function parse_time () { | |
m=0 | |
s=0 | |
if [[ $1 = *":"* ]]; then | |
m=`echo $1 | cut -d ':' -f1` | |
s=`echo $1 | cut -d ':' -f2` | |
LIMIT=$(( $m*60+$s )) | |
else | |
LIMIT=$1 | |
fi | |
} | |
LABEL=`zenity --entry --text="Enter the timer's LABEL string" 2>/dev/null` | |
time=`zenity --entry --text="Enter timer's countdown time;\nFormat 'MM:SS'\nor\n'SS' only (i.e. 90)" 2>/dev/null` | |
parse_time $time | |
# load wmctrl into memory so it's there when requested later | |
wmctrl -l 2> /dev/null > /dev/null | |
function blink_status_bar () { | |
(for ((i=1; i<8; i++));do | |
sleep .1;echo 0;sleep .1;echo 100 | |
done) } | |
# if there is a LIMIT entry, use it, else assume 3 minutes | |
if test -z "$LIMIT"; then | |
LIMIT=180 | |
LABEL="Tea_Timer" | |
else | |
# force mathematical evaluation, for syntax: timer minutes*60 | |
let LIMIT=($LIMIT * 1) | |
# eliminates error message: "integer expression expected" | |
# getting strange results with leading zeros | |
let min=($LIMIT / 60) | |
fi | |
# if there is a LABEL entry, use it as LABEL | |
if test -z "$LABEL"; then | |
true | |
else | |
LABEL=`echo $LABEL|sed 's/ /_/g'` | |
fi | |
# ======= Start timing ========== | |
# parens cause work to be done in a subshell, then piped to zenity | |
( | |
# relocate zenity to 1st workspace | |
sleep .2;wmctrl -r $LABEL -t 0 | |
# If command is run from cmdline, can't 'Move to Another Workspace'-dunno why. | |
# Or control properties like OnTop, Visible, or Below | |
# tried 'wmctrl -r $LABEL -b remove,sticky' with no effect | |
# This problem does not occur when run via F-key. Very strange! | |
# Worse: can use above syntax to move zenity to another desktop, as shown | |
# in 'Workspace Switcher', but zenity does not appear on that actual desktop! | |
# Instead, it re-appears on 2nd desktop as soon as I switch back to it. | |
# the above problem does not apply if I run, say 'xmag', from the cmdline! | |
# relocate zenity to upper left corner | |
if [ $LIMIT -lt 3600 ]; then | |
wmctrl -r $LABEL -e 0,30,20,-1,-1 | |
else | |
wmctrl -r $LABEL -e 0,280,20,-1,-1 | |
fi | |
for ((a=$LIMIT; a > 0; a--)); do | |
# compute Hrs/Mins/Secs | |
let hour=($a / 3600) | |
let y=($hour * 60) | |
let min=($a / 60) | |
let min=($min - $y) | |
let y=($y * 60) | |
let z=($min * 60) | |
let sec=($a - $y - $z) | |
if [ $hour -gt 0 ]; then | |
# pad min and sec, ex: "9" to "09", "0" to "00" | |
echo "# Time Left: $hour:`printf "%02.f" $min`:`printf "%02.f" $sec`" | |
else | |
# pad sec, ex: "9" to "09", "0" to "00" | |
echo "# Time Left: $min:`printf "%02.f" $sec`" | |
fi | |
# compute percentage of limit time, update progress bar | |
let y=($LIMIT - $a) | |
let y=($y * 100) | |
let pct=($y / $LIMIT) | |
echo $pct;sleep 0.99 | |
# slow by 7 seconds in 3 hours | |
done | |
echo 100;echo "# $LABEL completed" | |
# move zenity to current workspace, always-on-top | |
wmctrl -R $LABEL;wmctrl -r $LABEL -b add,above | |
# Notifications | |
aplay $AUDIO_FILE | |
xcowsay -t 5 --at=1950,20 --image="$IMAGE_FILE" "$LABEL completed" & | |
blink_status_bar | |
) | zenity --progress --title=$LABEL --percentage=0 --auto-kill 2>/dev/null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment