如何创建基本的VBScript报警

时间:2014-03-11 07:42:46

标签: vbscript

我是VBScript的新手,我试图创建闹钟,以便我的计算机告诉我在特定时间执行某项任务。但它效率不高并且使用一些计算机内存,因为它每秒打开vbs文件以获取更新的时间信息。代码是 -

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

所以我想要一个可以在不打开源文件的情况下更新信息的方法。

4 个答案:

答案 0 :(得分:2)

正如安斯加所说,一系列计划任务可能是最好的解决方案。否则,我能看到的另一种方法就是使用WScript.Sleep()让你的脚本进入睡眠状态,直到你的闹钟为止。例如,如果您的脚本每天上午8:00自动启动,并且您有一项任务,您希望在上午10点和另一个上午11点运行,那么只需睡觉直到它的时间:

' 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

或者,您可以轮询正确的时间:

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

但是您无法使用这些方法控制脚本。如果您需要更改闹钟时间或完全取消它们,则需要在任务管理器中终止WSCRIPT.EXE进程。另外,为什么在任务计划程序为您执行此操作时编写轮询/计划脚本?

答案 1 :(得分:0)

使闹钟成为脚本的参数,并将代码删除到这一行:

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

创建运行脚本的计划任务,如下所示:

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

答案 2 :(得分:0)

我确实理解了你的问题,但检查这是否适用于你的问题:

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

我知道你要求一个&#34; BASIC&#34;代码,仍然试试这个。

答案 3 :(得分:0)

以下是Windows 7的关闭代码:

.BY