Write a timed task pop-up window using VBScript – reminder to rest or drink water

Time:2019-9-20

Write a timed task pop-up window using VBScript - reminder to rest or drink water

As a programmer, you have to face the computer for a long time in the process of learning or working. Sometimes you forget to rest or drink water. Sometimes you sit for hours.

This is very harmful to health. I didn’t care much about it before. I usually drink water when I’m thirsty. It wasn’t until recently that I felt a little uncomfortable in my throat, possibly inflamed, that I realized it was a sign of protest inside my body.

In fact, when you feel thirsty, your body is very short of water.Therefore, we should drink water regularly, usually at intervals.

Alarm clock or countdown

But for many people, it’s hard to get the brain’s automatic conditioning to drink water. So we need to remind us by external means.

There are many ways to remind people, such as setting a few alarm clocks or counting down. Unfortunately, there is also a drawback in this way of using mobile phones. Sometimes, if you are too focused on your work and don’t have time to watch your mobile phone, the alarm clock and countdown will be invalid.

Write reminders using scripts

Therefore, in order to better solve the above problems, we can proceed from the essence. Generally speaking, programmers have more time to face the computer when they work a day, so we can write a timed task on the computer.

My approach isWindowsWrite on the systemVBScript scriptEvery once in a while, play the window to remind me to drink water.

Concrete realization

This article mainly introduces how to be in theWindowsSystem under useVBScriptWrite a simple timer window.

If it isMacbookYou can use it.AppleScriptLinuxThe system can be usedShell

What is VBScript

VBScriptIt is a scripting language developed by Microsoft.VBScriptYes, you can.WindowsWrite automatic tasks under the system and so on.
VBScriptDocument to.vbsSuffixWindowsUnder the system, you can double-click directly, and the code is run byC:\Windows\System32Underneathwscript.exeResponsible for interpretation and execution.

VBScript tutorial

I didn’t learn it systematically before I wrote this pop-up window widget.VBScriptNow I have learned to use it, but I have only learned it roughly. If you want to understand it systematically, you can take a look at the novice bird tutorial.

VBScript Knowledge Used

This gadget is actually very simple, the core is just a few lines of code, similar to the web page.setIntervalAnd then as soon as the time comesalert

Here’s a detailed description of what’s used in the codeVBScriptKnowledge.

  1. Dim variableNameDefine variables
  2. Do ... LoopCirculation, similardo ... while(true)
  3. If ... Then ... Else ... End IfConditional structure
  4. InputBoxSimilar to the web pagewindow.propmt
  5. MsgBoxSimilar to the web pagewindow.alert

Detailed code

The following is the full code of the tool.

Dim interval
Dim ret

' set task's interval
Do
  interval = InputBox("Please input the interval:", "Interval(seconds)", 30 * 60 * 1000)

  If interval = vbEmpty Then
    ' click cancel
    MsgBox "You input empty, interval is set default half an hour."
    interval = 1800000
    Exit Do
  End If

  If Not IsNumeric(interval) Then
    ' input what is't a numeric
     MsgBox "Please input number"
   Else
     Exit Do
  End If
Loop

' execute interval task
Do
  ret = MsgBox("Drink some water", vbOKCancel, "Time up")
  If ret = 1 Then
    Wscript.sleep interval
  Else
    MsgBox "Close notification", vbOkOnly, "Close notification"
    Exit Do
  End If
Loop

summary

Sometimes it’s interesting to find that programming can be used in everyday life.

Attached at the end of the projectGithubAddress, welcomeStar or ForkThank you for your support.