有什么方法可以使用vbscript显示Windows 10 Toast通知吗?

时间:2019-10-25 11:09:40

标签: vbscript

我正在尝试使用vbscript在Windows 10中创建和显示通知。我找到了一个适用于mac的简单解决方案,在这里我可以使用此applescript轻松显示通知 display notification "All graphics have been converted." with title "My Graphic Processing Script" subtitle "Processing is complete." sound name "Frog"

vbscript在Windows上是否有类似的东西?

1 个答案:

答案 0 :(得分:1)

下面是一个示例,向您展示如何编写 .PS1文件(Powershell脚本) ,并从vbscript中执行该文件。

Option Explicit
Dim Ws,Ret,ByPassPSFile,PSFile
Set Ws = CreateObject("wscript.Shell")
ByPassPSFile = "cmd /c PowerShell.exe -ExecutionPolicy bypass -noprofile -file "
Call WritePSFile("Warning","10","'Please wait...'","' Scan is in progress....'","'Warning'","10")
Ret = Ws.run(ByPassPSFile & PSFile,0,True)
'------------------------------------------------------------------------------------------------------------
Sub WritePSFile(notifyicon,time,title,text,icon,Timeout) 
Const ForWriting = 2
Dim fso,ts,strText
PSFile = Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) & "ps1"
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(PSFile,ForWriting,True)
strText = strText & "[reflection.assembly]::loadwithpartialname('System.Windows.Forms') | Out-Null;" & VbCrlF
strText = strText & "[reflection.assembly]::loadwithpartialname('System.Drawing') | Out-Null;" & VbCrlF 
strText = strText & "$notify = new-object system.windows.forms.notifyicon;" & VbCrlF
strText = strText & "$notify.icon = [System.Drawing.SystemIcons]::"& notifyicon &";" & VbCrlF 
strText = strText & "$notify.visible = $true;" 
strText = strText & "$notify.showballoontip("& time &","& title &","& text &","& icon &");" & VbCrlF 
strText = strText & "Start-Sleep -s " & Timeout &";" & VbCrlF
strText = strText & "$notify.Dispose()"
ts.WriteLine strText
End Sub
'------------------------------------------------------------------------------------------------------------

编辑:

另一个可以获取您的公共IP和ISP并将其显示在BallonTip上的示例。

Option Explicit
Dim Ws,Ret,ByPassPSFile,PSFile
Set Ws = CreateObject("wscript.Shell")
ByPassPSFile = "cmd /C PowerShell.exe -ExecutionPolicy bypass -noprofile -file "
Call WritePSFile(DblQuote("Warning"),"20",DblQuote("Public IP Information"),DblQuote(showIP),DblQuote("Warning"),"10")
Ret = Ws.run(ByPassPSFile & PSFile,0,True)
'------------------------------------------------------------------------------------------------------------
Sub WritePSFile(notifyicon,time,title,text,icon,Timeout) 
Const ForWriting = 2
Dim fso,ts,strText
PSFile = Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) & "ps1"
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(PSFile,ForWriting,True)
strText = strText & "[reflection.assembly]::loadwithpartialname('System.Windows.Forms') | Out-Null;" & VbCrlF
strText = strText & "[reflection.assembly]::loadwithpartialname('System.Drawing') | Out-Null;" & VbCrlF 
strText = strText & "$notify = new-object system.windows.forms.notifyicon;" & VbCrlF
strText = strText & "$notify.icon = [System.Drawing.SystemIcons]::"& notifyicon &";" & VbCrlF 
strText = strText & "$notify.visible = $true;" 
strText = strText & "$notify.showballoontip("& time &","& title &","& text &","& icon &");" & VbCrlF 
strText = strText & "Start-Sleep -s " & Timeout &";" & VbCrlF
strText = strText & "$notify.Dispose()"
ts.WriteLine strText
End Sub
'------------------------------------------------------------------------------------------------------------
Function ShowIP()
Dim http,strJson,j,Info
Set http = CreateObject("Msxml2.XMLHTTP")
http.open "GET","http://ip-api.com/json/",False
http.send
strJson = http.responseText
Set j = Parse(strJson)
Info = Info & "IP="&j.query & vbCrLf &_
"ISP="&j.isp & vbCrLf &_
"Country="&j.country & vbCrLf &_
"City="&j.city
ShowIP = Info
End Function
'------------------------------------------------------------------------------------------------------------
Function Parse(strJson)
Dim html,window
    Set html = CreateObject("htmlfile")
    Set window = html.parentWindow
    window.execScript "var json = " & strJson, "JScript"
    Set Parse = window.json
End Function
'------------------------------------------------------------------------------------------------------------
Function DblQuote(Str)
    DblQuote = chr(34) & Str & chr(34)
End function
'------------------------------------------------------------------------------------------------------------

使用不带Powershell但使用对象CreateObject('Internet.HHCtrl').TextPopup的vbscript的另一种方法

Option Explicit
Dim http,strJson,j,Info,HH
Set http = CreateObject("Msxml2.XMLHTTP")
http.open "GET","http://ip-api.com/json/",False
http.send
strJson = http.responseText
Set j = Parse(strJson)
Info = Info & "IP="&j.query & vbCrLf &_
"ISP="&j.isp & vbCrLf &_
"Country="&j.country & vbCrLf &_
"City="&j.city & vbCrLf &_
"TimeZone="&j.timezone & vbCrLf &_
"CountryCode="&j.countryCode & vbCrLf &_
"org="&j.org & vbCrLf &_
"AS="&j.as & vbCrLf &_
"Latitude="&j.lat & vbCrLf &_
"Longitude="&j.lon
Set HH = CreateObject("Internet.HHCtrl")
HH.TextPopup Info,"Verdana,12",12,12,12,12
WScript.Sleep 10000
Wscript.Quit()
'****************************************************************************
Function Parse(strJson)
Dim html,window
    Set html = CreateObject("htmlfile")
    Set window = html.parentWindow
    window.execScript "var json = " & strJson, "JScript"
    Set Parse = window.json
End Function
'****************************************************************************