You are not logged in.

#51 2019-12-05 09:40:32

misko_2083
Member
Registered: 2016-05-24
Posts: 264

Re: Alarm clock scripts

^Well that depends from the chemical composition of the water, temerature, volume of the pot, barometric pressure, density of the spaghetti...
No, I'm pretty sure they boil them in the microwave oven. For science... :-)
Time is relative, for someone hungry waiting for spaghetti to boil - it takes too long, for someone who had a snack when no one was watching those 12 minutes are a blink of an eye.

Offline

#52 2020-01-21 14:10:05

misko_2083
Member
Registered: 2016-05-24
Posts: 264

Re: Alarm clock scripts

New script with at command.

You can choose between running the file in mpv or a custom command.
If you type in custom command, don't use "@" character. This is the limitation.
Custom command can be firefox "www.youtube.com" for example.
Leave the custom blank for mpv.
Ticking "Loop forever" overides "repeat".
Job can be listed and removed via "List at Job" button.


#!/bin/bash
# Fancy alarm Misko_2083 @ 2020
# Requires: at, yad 0.42 (with html dialog), mpv

#########################
# Begin User Interface  #
#########################

# JavaScript, HTML and CSS

YURI="$(cat << EOF
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">

<!--CSS for the page-->
<style>
.body, .html {
  overflow-y: hidden;
  cursor: default;
  background-color: #CECECE;
}

#jsalarmclock{
font-weight: bold;
font-size: 14px;
}

#jsalarmclock div{
margin-bottom: 0.8em;
}

#jsalarmclock div.leftcolumn{
float: left;
width: 150px;
font-size: 14px;
clear: left;
}

#jsalarmclock span{
 margin-right: 10px;
}

#jsalarmclock input[type=number]{
 width: 50px;
}

p {
 font-weight: bold;
 font-size: 11px;
}

.default:hover {
  background: #000000;
  color: white;
  text-decoration: none;
}

.title {
 font-size: 10px
 cursor: default;
}
</style>

<!--Script to prevent right click menu-->
<script type="text/javascript">
    if (document.addEventListener) { // IE >= 9; other browsers
        document.addEventListener('contextmenu', function(e) {
            //here you do nothing
            e.preventDefault();
        }, false);
    } else { // IE < 9
        document.attachEvent('oncontextmenu', function() {
            alert("You've tried to open context menu");
            window.event.returnValue = false;
        });
    }
</script>


<script type="text/javascript">

/***********************************************

* JavaScript Alarm Clock- by JavaScript Kit (www.javascriptkit.com)
* This notice must stay intact for usage
* Visit JavaScript Kit at http://www.javascriptkit.com/ for this script and 100s more

***********************************************/

/* Modified for my needs Misko_2083 */

var jsalarm={
	padfield:function(f){
		return (f<10)? "0"+f : f
	},
	showcurrenttime:function(){
		var dateobj=new Date()
		var ct=this.padfield(dateobj.getHours())+":"+this.padfield(dateobj.getMinutes())+":"+this.padfield(dateobj.getSeconds())
		this.ctref.innerHTML=ct
		this.ctref.setAttribute("title", ct)
	},
	init:function(){
		var dateobj=new Date()
		this.ctref=document.getElementById("jsalarm_ct")
		this.submitref=document.getElementById("submitbutton")
		this.submitref.onclick=function(){
			jsalarm.setalarm()
			//this.value="Alarm Set"
			this.disabled=false
			return false
		}
		var selections=document.getElementsByTagName("select")
		this.hourselect=selections[0]
		this.minuteselect=selections[1]
		for (var i=0; i<60; i++){
			if (i<24) //If still within range of hours field: 0-23
			this.hourselect[i]=new Option(this.padfield(i), this.padfield(i), false, dateobj.getHours()==i)
			this.minuteselect[i]=new Option(this.padfield(i), this.padfield(i), false, dateobj.getMinutes()+1==i)

		}
		jsalarm.showcurrenttime()
		jsalarm.timer=setInterval(function(){jsalarm.showcurrenttime()}, 1000)
	},
        info:function(a) {
                var h = document.createElement("p");
                var filename = String(a).replace(/^.*[\\\/]/, '');
                var t = document.createTextNode("Alarm "+filename);
                h.appendChild(t);
                document.body.appendChild(h);
                h.setAttribute("id", "info");
        },
        dom:function(a){
                // DOM 2 Events
                var dispatchMouseEvent = function(target, var_args) {
                  var e = document.createEvent("MouseEvents");
                  // If you need clientX, clientY, etc., you can call
                  // initMouseEvent instead of initEvent
                  e.initEvent.apply(e, Array.prototype.slice.call(arguments, 1));
                  target.dispatchEvent(e);
                };
                dispatchMouseEvent(a, 'mouseover', true, true);
                dispatchMouseEvent(a, 'mousedown', true, true);
                dispatchMouseEvent(a, 'click', true, true);
                dispatchMouseEvent(a, 'mouseup', true, true);
        },
	setalarm:function(){
		this.hourwake=this.hourselect.options[this.hourselect.selectedIndex].value
		this.minutewake=this.minuteselect.options[this.minuteselect.selectedIndex].value
                var a = document.createElement('a');
                var linkText = document.createTextNode("Go");
                a.appendChild(linkText);
                a.title = "fake link";

                a.href = this.hourselect.options[this.hourselect.selectedIndex].value+":"+this.minuteselect.options[this.minuteselect.selectedIndex].value;
                jsalarm.dom(a)
                jsalarm.info(a)
                a.href = "repeat@"+document.getElementById("repeat").value;
                jsalarm.dom(a)
                if(document.getElementById("forever").checked == true) {
                  a.href = "repeat@"+document.getElementById("forever").value;
                  jsalarm.dom(a)
                }
                a.href = "command@"+document.getElementById("command").value;
                jsalarm.dom(a)
                //a.parentNode.removeChild(linkText);
		//this.hourselect.disabled=true
		//this.minuteselect.disabled=true
	}

}

</script>
</head>
<body class="body">
<br />

 <form action="" method="">
  <div id="jsalarmclock">
  <div><div class="leftcolumn">Current Time:</div> <span id="jsalarm_ct" style="letter-spacing: 2px"></span></div>
  <div><div class="leftcolumn">Set Alarm:</div> <span><select></select> Hour</span> <span><select></select> Minutes</span> </div>

  <div><div class="leftcolumn">Repeat:</div><input size="5" type="number" id="repeat" min="1" value="1"/>  <br>
        <div class="leftcolumn">Loop forever</div> <input type="checkbox" id="forever" value="forever"/>
       <span style="font: normal 11px Tahoma">Loop forever overides repeat</span> <br>
  <div><div class="leftcolumn">Set file:</div><a href="file:///musicfile@"><button type="button">Select a file to play</button></a>
             <span style="font: normal 11px Tahoma">Select a file to run with mpv or type in a custom action</span></div>
  <div><div class="leftcolumn">Set Custom Action:</div> <input type="text" id="command" size="55" value="" /> <br>
       <span style="font: normal 11px Tahoma">*Command to launch, if empty mpv will be used (do not use "@" character here)</span></div>
  <input type="submit" value="Set Alarm!" id="submitbutton" />

  </div>
</form>

<a href="file:///atq@"><button type="button">At Job List</button></a>

<script type="text/javascript">

jsalarm.init()

</script>
</body>
</html>
EOF
)"
#########################
# End of User interface #
#########################

#########################
# Bash functions        #
#########################

# Main window
function _yad_ui(){
    # Set the line buffering with stdbuf
    echo "${YURI}" | stdbuf -oL -eL yad --title="Set Alarm" --width=650 --height=500 --no-escape \
    --html --no-buttons --print-uri --borders=0 --window-icon="alarm1c.png" 2>&1 & echo $!
}

# Main Function
function _do_stuff(){
    # fetch yad process id
    read YAD_PID

    # read yad stdout line by line
    while read -r line
    do
        case ${line##*/} in
         [0-9][0-9]:[0-9][0-9]*)
            #kill -SIGUSR1 $YAD_PID
            echo "Alarm set to: ${line##*/}"
            alarm_time="${line##*/}"           
;;
                   repeat@*) REPEAT="$(echo  "${line##*@}" | sed 's/%20/ /g')"
;;
                   command@*) COMMAND="$(echo  "${line##*@}" | sed 's/%20/ /g')"

if [ ! -e "/tmp/yalarm.$$" ] ; then
      MUSIC="$(yad --width=600 --height=500 \
                    --title="Alarm" --text="Select a file to play in mpv" \
                    --file-filter="Audio | *.mp3 *.MP3 *.wav *.WAV *.ogg *.OGG" \
                    --file 2>/dev/null)"
      [ $? != 0 ] && continue
       echo "${MUSIC}" > /tmp/yalarm.$$
fi

# Fredx181
hrnow=$(echo $(date +"%k"))

# For when trying to set alarm for next day e.g. from current 21:00 to target 6:00
if [ "${alarm_time%%:*}" -eq "$hrnow" ] &&  [ "${alarm_time##*:}" -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 ${alarm_time} - tomorrow ?" --width=400 --button="Yes:0"  --button="No:1"
[ $? -ne 0 ] &&  continue 
fi

if [ ! "${COMMAND}" = "" ]; then
at "${alarm_time}" << EOF
exec env DISPLAY="$DISPLAY" $(echo  "${line##*@}" | sed 's/%20/ /g') &
EOF
else
    if [  "${REPEAT}" = "forever" ]; then
at "${alarm_time}" << EOF
exec env DISPLAY="$DISPLAY" mpv --force-window --loop inf "$(cat /tmp/yalarm.$$)"
EOF
    else
at "${alarm_time}" << EOF
exec env DISPLAY="$DISPLAY" mpv --force-window --loop "${REPEAT}" "$(cat /tmp/yalarm.$$)"
EOF
   fi
fi

;;
                   atq@*) (REMOVE_JOB="$(atq | yad --list --width=600 --height=500 \
                                                  --text="Select 'at' job to remove or close this window" \
                                                  --column "Job List" \
                                                  --button="Remove at Job" 2>/dev/null \
                                            | awk '{print $1}')"
                          if [[ "${REMOVE_JOB}" == [0-9]* ]]; then
                             echo "${REMOVE_JOB}"
                             atrm "${REMOVE_JOB}"
                             killall mpv
                          fi) &                      
;;
                   musicfile@*) (MUSIC="$(yad --width=600 --height=500 \
                                              --title="Alarm" --text="Select a file to play in mpv" \
                                              --file-filter="Audio | *.mp3 *.wav" \
                                              --file 2>/dev/null)" \
                                               [ $? = 0 ] && echo "${MUSIC}" > /tmp/yalarm.$$) &
;;
       esac 
    done
rm /tmp/yalarm.$$
exit
}

_yad_ui | _do_stuff & wait $! 2>/dev/null

This can be converted from HTML to regular GTK+ dialogs.

Last edited by misko_2083 (2020-01-21 14:17:13)

Offline

#53 Yesterday 10:30:06

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 1,966
Website

Re: Alarm clock scripts

@misko, nice, some thoughts (I build latest yad)
- position 'Repeat' and 'Loop forever' under 'Select a file to play' so it is clear this is related to sound and not repeating alarm every day (or something)
- Select a file to play' doesn't do anything for me, I get file dialog after clicking 'Set Alarm!'
- 'At Job List' is showing two alarms set at same time, but when running atq from cli there is only single event
- Missing delete event functionality?

edit: I wonder if analog picker like
https://weareoutman.github.io/clockpicker/ or
https://github.com/luncheon/clocklet
could be implemented?

edit2: I have repacked some css/html/js/svg clock for testing purposes and it works (but don't feel brave enough for js->yad interface as it would be needed for picker).
https://github.com/brontosaurusrex/svgclockcool
jbTMpz4b.png

Last edited by brontosaurusrex (Yesterday 23:31:53)

Offline

Board footer

Powered by FluxBB