【问题标题】:How to create basic VBScript alarm如何创建基本的 VBScript 警报
【发布时间】:2023-03-29 03:22:01
【问题描述】:

我是 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

所以我想要一种无需反复打开源文件即可更新信息的方法。

【问题讨论】:

    标签: vbscript


    【解决方案1】:

    正如 Ansgar 所建议的,一系列计划任务可能是这里最好的解决方案。否则,我能看到的唯一其他方法是使用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 进程。另外,既然任务计划程序已经为您完成了,为什么还要编写轮询/调度脚本呢?

    【讨论】:

      【解决方案2】:

      将警报作为脚本的参数并将代码剥离到这一行:

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

      像这样创建运行脚本的计划任务:

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

      【讨论】:

        【解决方案3】:

        这是 Windows 7 的关闭代码:

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

        【讨论】:

          【解决方案4】:

          我确实有点理解您的问题,但请检查这是否适用于您的问题:

          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
          

          我知道您要求提供“BASIC”代码,请继续尝试。

          【讨论】: