Hi all, here's new alarm script (2 slightly different), changes:
- Using preferences file ~/.yalrm
(remembering now most previously used settings for next time run)
- Supports now setting the alarm for next day, same hour (previously didn't work) (but gives prompt 'Are you sure' yes/no in case accidentally set)
- Display of tray icon as long as waiting for the alarm to go off (thanks Argolance and vovchik
)
TODO maybe: Multiple alarms can be set, but, as it is now, everytime a new tray icon appears, better only one icon but tooltip and message when left click changed when setting new alarm
First script: depends on the 4 icons Argolance attached here:
http://murga-linux.com/puppy/viewtopic. ... 89#1042989
Extract and make sure they are in /root, e.g. '/root/alarm_1.png', '/root/alarm_2.png' etc..
Code: Select all
#!/bin/bash
alrm () {
PLAYER="$1"
if [ -z "$PLAYER" ]; then
yad --center --title="No player" --text=" No player selected " --width=350 --button="gtk-close:0"
exit
fi
sed -i 's#^PLAYER=.*#PLAYER="'"$PLAYER"'"#' $HOME/.yalrm
sound="$2"
if [ ! -f "$sound" ]; then
yad --center --title="No sound file" --text=" No sound file selected " --width=350 --button="gtk-close:0"
exit
fi
sed -i 's#^sound=.*#sound="'"$sound"'"#' $HOME/.yalrm
export hr="$3"
[ "${hr:0:1}" = '0' ] && hr=${hr#?}
export mn="$5"
target="$hr:$mn"
repeat="$4"
sed -i 's#^repeat=.*#repeat="'"$repeat"'"#' $HOME/.yalrm
interval="$6"
sed -i 's#^interval=.*#interval="'"$interval"'"#' $HOME/.yalrm
dur="$7"
sed -i 's#^dur=.*#dur="'"$dur"'"#' $HOME/.yalrm
command () {
yad --undecorated --image=/root/alarm_4.png --borders 12 --title="Alarm" --text=" \t<b>Alarm set to $hr:$mn</b> " --geometry=280x50-40-50 --no-buttons --timeout 4
} ; export -f command
show_icon () {
while true; do echo 'icon:'/root/alarm_1.png; sleep 1; echo 'icon:/root/alarm_2.png'; sleep 1; echo 'icon:/root/alarm_3.png'; sleep 1; echo 'icon:/root/alarm_4.png'; sleep 1; done | yad --notification --auto-kill --no-middle --listen --text="Alarm is running in the background...
Set to $hr:$mn" --command="bash -c command" &
export XPID=$!
}; export -f show_icon
hrnow=$(echo $(date +"%k"))
# For when trying to set alarm for next day e.g. from current 21:00 to target 6:00
if [ "$hr" -eq "$hrnow" ] && [ "$mn" -le "$(date +"%M")" ]; then
# prompt yes/no in case accidentally chosen current hour but less than current minute
yad --center --title="Are you sure?" --text=" Are you sure to set the alarm to $target (next day) ?" --width=400 --button="Yes:0" --button="No:1"
[ $? -ne 0 ] && exit
command; show_icon
sleep $(( $(date --date="$(((23+$hr) - $hrnow)):$mn" +%s) - $(date --date="00:$(date +"%M"):$(date +"%S")" +%s) + 3600 ))
elif [ "$hr" -lt "$hrnow" ]; then
command; show_icon
sleep $(( $(date --date="$(((24+$hr) - $hrnow)):$mn" +%s) - $(date --date="00:$(date +"%M"):$(date +"%S")" +%s) ))
else
command; show_icon
sleep $(( $(date --date="$target" +%s) - $(date +%s) ));
fi
kill $XPID
echo "Wake Up!"
while [ $repeat -gt 0 ]; do
$PLAYER "$sound" </dev/null >/dev/null 2>&1 &
sleep $dur
pidplayer="$(pgrep -lf "$PLAYER $sound" | awk '{ print $1 }')"
[ -n "$pidplayer" ] && kill -9 $pidplayer
repeat=$(($repeat-1))
if [ $repeat -gt 0 ]; then
if [ $dur -lt $interval ]; then
sleep $(($interval-$dur)) 2> /dev/null
# else
# sleep $interval 2> /dev/null
fi
fi
done
}; export -f alrm
# set hour and minute at current time in yad GUI
hour=$(echo -e "$(seq -w $(date +"%H") 23)\n$(seq -w 00 $(date +"%H"))" | sed '$ d' |xargs | tr ' ' ':')
min=$(echo -e "$(seq -w $(date +"%M") 59)\n$(seq -w 00 $(date +"%M"))" | sed '$ d' |xargs|tr ' ' ':')
# create preference file $HOME/.yalrm, if not exists
if [ ! -f $HOME/.yalrm ]; then
echo "PLAYER=mplayer
sound=""
repeat=01
interval=00
dur=05
" > $HOME/.yalrm
fi
. $HOME/.yalrm
while :
do
date +"%H":"%M":"%S"
sleep 0.5
done | yad --plug="$$" --tabnum=1 --form --cycle-read --columns=2 --field="\t\t<i><b>S e t A l a r m</b></i>\t:lbl" --field="Current Time:: " &
yad --plug="$$" --tabnum=2 --center --title="Alarm" --form --columns=4 --item-separator=":" \
--field="Player::sfl" "$PLAYER" \
--field="Sound::sfl" "$sound" \
--field="Hour::CB" "$hour" \
--field="Repeat::NUM" "$repeat:01..300:01" \
--field="Minutes::CB" "$min" \
--field="Interval:\n(seconds):NUM" "$interval:00..1000:01" \
--field="Duration:\n(seconds):NUM" "$dur:01..10000:01" \
--field="Set Alarm:fbtn" 'bash -c "alrm %1 %2 %3 %4 %5 %6 %7 %8 %9"' \
--no-buttons &
yad --center --title="Alarm" --paned --key="$$" --orient=vert --no-buttons
Second script contains the icon setup for yad --notification that vovchik provided (self containing), for the rest it's the same as above.
Code: Select all
#!/bin/bash
export A_ICON="/tmp/alarm1a.svg"
export B_ICON="/tmp/alarm1b.svg"
export C_ICON="/tmp/alarm1c.svg"
COL="red"
if [ ! -e "$A_ICON" ]; then
SVG="<""svg width='22' height='22' viewBox='0 0 100 100'>
<mask id='mask_circle'>
<circle cx='50' cy='50' r='50' fill='white'/>
<circle cx='50' cy='50' r='12' fill='gray'/>
</mask>
<linearGradient x1='20' y1='60' x2='60' y2='40' id='lg'
gradientUnits='userSpaceOnUse'>
<stop style='stop-color:dark$COL;stop-opacity:1' offset='0'/>
<stop style='stop-color:$COL;stop-opacity:1' offset='1'/>
</linearGradient>
<circle cx='50' cy='50' r='28%' style='fill:dark$COL'/>
<circle cx='50' cy='50' r='23%' style='fill:url(#lg);
stroke:gray;stroke-width:3'/>
</svg>"
echo "$SVG" > $A_ICON
SVG=$(echo "$SVG" | sed 's/28%/38%/g' | sed 's/23%/33%/g')
echo "$SVG" > $B_ICON
SVG=$(echo "$SVG" | sed 's/38%/48%/g' | sed 's/33%/43%/g')
echo "$SVG" > $C_ICON
fi
alrm () {
PLAYER="$1"
if [ -z "$PLAYER" ]; then
yad --center --title="No player" --text=" No player selected " --width=350 --button="gtk-close:0"
exit
fi
sed -i 's#^PLAYER=.*#PLAYER="'"$PLAYER"'"#' $HOME/.yalrm
sound="$2"
if [ ! -f "$sound" ]; then
yad --center --title="No sound file" --text=" No sound file selected " --width=350 --button="gtk-close:0"
exit
fi
sed -i 's#^sound=.*#sound="'"$sound"'"#' $HOME/.yalrm
export hr="$3"
[ "${hr:0:1}" = '0' ] && hr=${hr#?}
export mn="$5"
target="$hr:$mn"
repeat="$4"
sed -i 's#^repeat=.*#repeat="'"$repeat"'"#' $HOME/.yalrm
interval="$6"
sed -i 's#^interval=.*#interval="'"$interval"'"#' $HOME/.yalrm
dur="$7"
sed -i 's#^dur=.*#dur="'"$dur"'"#' $HOME/.yalrm
command () {
yad --undecorated --image=$C_ICON --borders 12 --title="Alarm" --text=" \t<b>Alarm set to $hr:$mn</b> " --geometry=280x50-40-50 --no-buttons --timeout 4
} ; export -f command
show_icon () {
while true; do
echo 'icon:'$A_ICON;
sleep 0.25;
echo 'icon:'$B_ICON;
sleep 0.25;
echo 'icon:'$C_ICON;
sleep 0.25;
echo 'icon:'$B_ICON;
sleep 0.25;
done | yad \
--notification \
--image="$C_ICON" \
--auto-kill \
--no-middle \
--listen \
--text="Alarm is running in the background...
Set to $hr:$mn" \
--command="bash -c command" &
XPID=$!
}; export -f show_icon
hrnow=$(echo $(date +"%k"))
# For when trying to set alarm for next day e.g. from current 21:00 to target 6:00
if [ "$hr" -eq "$hrnow" ] && [ "$mn" -le "$(date +"%M")" ]; then
# prompt yes/no in case accidentally chosen current hour but less than current minute
yad --center --title="Are you sure?" --text=" Are you sure to set the alarm to $target (next day) ?" --width=400 --button="Yes:0" --button="No:1"
[ $? -ne 0 ] && exit
command; show_icon
sleep $(( $(date --date="$(((23+$hr) - $hrnow)):$mn" +%s) - $(date --date="00:$(date +"%M"):$(date +"%S")" +%s) + 3600 ))
elif [ "$hr" -lt "$hrnow" ]; then
command; show_icon
sleep $(( $(date --date="$(((24+$hr) - $hrnow)):$mn" +%s) - $(date --date="00:$(date +"%M"):$(date +"%S")" +%s) ))
else
command; show_icon
sleep $(( $(date --date="$target" +%s) - $(date +%s) ));
fi
kill $XPID
echo "Wake Up!"
while [ $repeat -gt 0 ]; do
$PLAYER "$sound" </dev/null >/dev/null 2>&1 &
sleep $dur
pidplayer="$(pgrep -lf "$PLAYER $sound" | awk '{ print $1 }')"
[ -n "$pidplayer" ] && kill -9 $pidplayer
repeat=$(($repeat-1))
if [ $repeat -gt 0 ]; then
if [ $dur -lt $interval ]; then
sleep $(($interval-$dur)) 2> /dev/null
# else
# sleep $interval 2> /dev/null
fi
fi
done
}; export -f alrm
# set hour and minute at current time in yad GUI
hour=$(echo -e "$(seq -w $(date +"%H") 23)\n$(seq -w 00 $(date +"%H"))" | sed '$ d' |xargs | tr ' ' ':')
min=$(echo -e "$(seq -w $(date +"%M") 59)\n$(seq -w 00 $(date +"%M"))" | sed '$ d' |xargs|tr ' ' ':')
# create preference file $HOME/.yalrm, if not exists
if [ ! -f $HOME/.yalrm ]; then
echo "PLAYER=mplayer
sound=""
repeat=01
interval=00
dur=05
" > $HOME/.yalrm
fi
. $HOME/.yalrm
while :
do
date +"%H":"%M":"%S"
sleep 0.5
done | yad --plug="$$" --tabnum=1 --form --cycle-read --columns=2 --field="\t\t<i><b>S e t A l a r m</b></i>\t:lbl" --field="Current Time:: " &
yad --plug="$$" --tabnum=2 --center --title="Alarm" --form --columns=4 --item-separator=":" \
--field="Player::sfl" "$PLAYER" \
--field="Sound::sfl" "$sound" \
--field="Hour::CB" "$hour" \
--field="Repeat::NUM" "$repeat:01..300:01" \
--field="Minutes::CB" "$min" \
--field="Interval:\n(seconds):NUM" "$interval:00..1000:01" \
--field="Duration:\n(seconds):NUM" "$dur:01..10000:01" \
--field="Set Alarm:fbtn" 'bash -c "alrm %1 %2 %3 %4 %5 %6 %7 %8 %9"' \
--no-buttons &
yad --center --title="Alarm" --paned --key="$$" --orient=vert --no-buttons
Fred