Nitron
Nitron

Reputation: 23

How to create basic VBScript alarm

I am new to VBScript and i tried to create alarms so that my computer tell me to do a certain task at a particular time. But it is not efficient and uses some computer memory as it opens the vbs file every second to get the time info updated. The code is-

Dim h,m,hn,hm,s

hn=Hour(Time)
hm=Minute(Time)
s=Second(Time)

h=CStr(hn)
m=CStr(hm)

alarm1="alarm 1"
alarm2="alarm 2"
alarm3="alarm 3"

Set speech=CreateObject("sapi.spvoice")
Set fi=CreateObject("WScript.Shell")

If hn=12 And hm=49 And s=1 Then
  speech.Speak alarm1
  fi.Run "alarm.vbs"
ElseIf hn=12 And hm=50 And s=1 Then
  speech.Speak alarm2
  fi.Run "alarm.vbs"
ElseIf hn=12 And hm=51 And s=1 Then
  speech.Speak alarm3
Else
  fi.Run "alarm.vbs"
End If

So I want a method that can update info without opening the source file again and again.

Upvotes: 0

Views: 2965

Answers (4)

Sreenikethan I
Sreenikethan I

Reputation: 331

I do understand your question a bit, but check whether this works for your problem or not:

On Error Resume next
MsgBox "Welcome to the ALARM tool",0+0,"ALARM tool"
MsgBox "Note: 1) If the computer is given a shutdown or a restart command then the alarm will be disabled. 2) This ALARM tool dosen't support decimals & words. If you will enter a decimal, the elapsed message box will appear without processing the alarm due to the error. If you enter words then an error message will appear.",0+0,"ALARM tool"
strComputer = "."
Set objWMIService = GetObject("Winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_DesktopMonitor")
For Each objItem in colItems
    intHorizontal = objItem.ScreenWidth
    intVertical = objItem.ScreenHeight
Next

Set objExplorer = CreateObject _
    ("InternetExplorer.Application")

objExplorer.Navigate "about:blank"
objExplorer.ToolBar = 0

objExplorer.StatusBar = 1
objExplorer.Left = (intHorizontal - 1) / 2
objExplorer.Top = (intVertical - 1) / 2
objExplorer.Width = 381
objExplorer.Height = 371
objExplorer.Visible = 1
objExplorer.Document.Body.InnerHTML = "<b><font style='color: #FF0000;'>NOTE:</font></b> In the <b>TIME</b> section, choose <b>MINUTE</b> to <b>SECOND</b>. <br><b><font style='color: #FF0000;'>NOTE:</font></b> You need Internet Connection for converting & <b><u>DO NOT</u></b> close this window.<br><br><iframe width='250' height='225' src='http://www-open-opensocial.googleusercontent.com/gadgets/ifr?url=http%3A%2F%2Fhosting.gmodules.com%2Fig%2Fgadgets%2Ffile%2F110220971631329159313%2Funitconverter.xml&container=open&view=home&lang=all&country=ALL&debug=0&nocache=0&sanitize=0&v=79794ca11372d4fe&source=http%3A%2F%2Fwww.calculator.net%2Fprojects%2Funit-converter-gadget.php&parent=http%3A%2F%2Fwww.calculator.net%2Fprojects%2Funit-converter-gadget.php&libs=core%3Acore.io#st=%25st%25' frameBorder='0' scrolling='no' style='display: block;'/>"
objExplorer.Document.Title = "ALARM tool"

t=InputBox ("Convert it in the window and enter the time in seconds:","ALARM tool")
If t="" Then
objExplorer.quit
MsgBox "The value is empty, so no alarm is set.",16,"ALARM tool"
WScript.Quit
End If

objExplorer.Document.Body.InnerHTML = "<b><font style='color: #FF0000;'>NOTE:</font></b> In the <b>TIME</b> section, choose <b>MINUTE</b> to <b>SECOND</b>. <br><b><font style='color: #FF0000;'>NOTE:</font></b> You need Internet Connection for converting & now you <b><u>CAN</u></b> close this window.<br><br><iframe width='250' height='225' src='http://www-open-opensocial.googleusercontent.com/gadgets/ifr?url=http%3A%2F%2Fhosting.gmodules.com%2Fig%2Fgadgets%2Ffile%2F110220971631329159313%2Funitconverter.xml&container=open&view=home&lang=all&country=ALL&debug=0&nocache=0&sanitize=0&v=79794ca11372d4fe&source=http%3A%2F%2Fwww.calculator.net%2Fprojects%2Funit-converter-gadget.php&parent=http%3A%2F%2Fwww.calculator.net%2Fprojects%2Funit-converter-gadget.php&libs=core%3Acore.io#st=%25st%25' frameBorder='0' scrolling='no' style='display: block;'/>"
objExplorer.quit

c=InputBox ("Enter your comment; leave it blank if you don't want one:","ALARM tool")

If c="" Then
 If t="1" Then
  MsgBox "Time: "+t+" second        Comment: None",0,"ALARM tool: Preview"
 Else
  MsgBox "Time: "+t+" seconds       Comment: None",0,"ALARM tool: Preview"
 End If
Else
 If t="1" Then
  MsgBox "Time: "+t+" second        Comment: "+c,0,"ALARM tool: Preview"
 Else
  MsgBox "Time: "+t+" seconds       Comment: "+c,0,"ALARM tool: Preview"
 End If
End If

co=MsgBox ("Are you sure?",36,"ALARM tool")
If co="7" Then
MsgBox "Alarm cancelled!!!",0,"ALARM tool"
WScript.Quit
End If

On Error Resume Next

WScript.Sleep (t+"000")

If Err.Number <> 0 Then
MsgBox "There's an error while setting the alarm. Maybe you typed words or it's an internal error. Please try again. Sorry for the inconvenience.",0,"ALARM tool"
WScript.Quit
End If

If t="1" Then
  If c="" Then
   MsgBox "ALARM for 1 second has elapsed!!!",0+0,"ALARM set for 1 second"
  Else
   MsgBox ""+c+"",0+0,"ALARM set for 1 second"
  End If
Else
  If c="" Then
   MsgBox "ALARM for "+t+" seconds has elapsed!!!",0+0,"ALARM set for "+t+" seconds"
  Else
   MsgBox ""+c+"",0+0,"ALARM set for "+t+" seconds"
  End If
End If
WScript.Quit

I know you asked for a "BASIC" code, still try this.

Upvotes: 0

David McKee
David McKee

Reputation: 1

Here's the shutdown code for Windows 7:

set wshshell = wscript.CreateObject("wscript.shell")
wshshell.run "shutdown.exe -s -t 6"
wscript.sleep 2000

Upvotes: 0

Bond
Bond

Reputation: 16311

As Ansgar suggested, a series of scheduled tasks is probably the best solution here. Otherwise, the only other way I can see of doing this is using WScript.Sleep() to put your script to sleep until it's time for your alarm. For example, if your script is launched automatically at 8:00 AM everyday and you have a task you'd like to run at 10 AM and another at 11 AM, just sleep until it's time:

' Sleep for two hours (until 10 AM)...
WScript.Sleep 1000 * 60 * 60 * 2

' Do alarm1

' Sleep for another hour (until 11 AM)...
WScript.Sleep 1000 * 60 * 60 * 1

' Do alarm2

Alternatively, you can poll for the correct time:

Do While True

    hn = Hour(Now)
    hm = Minute(Now)

    If hn = 12 And hm = 49 Then
        ' Alarm1
    ElseIf hn = 12 And hm = 50 Then
        ' Alarm2
    ElseIf ...
    End If

    ' Wait one minute and try again...
    WScript.Sleep 1000 * 60

Loop

But you have no control over your script using these methods. If you need to change the alarm times or cancel them altogether, you'd need to terminate the WSCRIPT.EXE process in Task Manager. Plus, why write a polling/scheduling script when Task Scheduler already does it for you?

Upvotes: 2

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

Make the alarm a parameter to your script and strip your code down to this single line:

CreateObject("sapi.spvoice").Speak WScript.Arguments(0)

Create scheduled tasks running the script like this:

wscript.exe "C:\path\to\your.vbs" "alarm 1"

Upvotes: 0

Related Questions