.vbs 用 Sapi 告诉时间但不中断某些打开的进程
.vbs to tell time with Sapi but not interrupt given some open processes
我编写了一个 .vbs 文件,对这种编码语言的了解几乎为零(主要代码来自 and here)。我把它放在一起让我的电脑每 15 分钟告诉我一次时间(结合任务调度程序)。最棘手的部分是让脚本检查缩放是否为 运行(以免用语音打断视频通话)。现在,出于同样的原因(不中断视频通话),我想更进一步检查第二个进程 Microsoft Teams。所以,我在下面复制了我的基本脚本。它适用于报时和检查缩放,但我不确定如何添加“Microsoft Teams”以进行检查。
Dim hour_now, minute_now, speaks, speech
hour_now = hour(time)
minute_now = minute(time)
If minute_now = 0 Then
speaks = "Il est " & hour_now & " heures"
Else
speaks = "Il est " & hour_now & " heures " & minute_now & " minutes"
End If
Set speech = CreateObject("sapi.spvoice")
Dim i
Dim strComputer
Dim FindProc
strComputer = "."
FindProc = "zoom"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select Name from Win32_Process WHERE Name LIKE '" & FindProc & "%'")
If colProcessList.count>0 then
'wscript.echo FindProc & " is running"
else
'wscript.echo FindProc & " is not running"
speech.Speak speaks
End if
Set objWMIService = Nothing
Set colProcessList = Nothing
我想我需要将 FindProc
对象设置为两个对象的列表,然后迭代或让它一次求值。任何帮助表示赞赏。顺便说一句,我的电脑有法语语音,所以显示时间是用法语写的。
此外,如果有人对如何使用某种 off/on 按钮或开关设置此任务有任何想法,那也很有用。
TIA
我明白了。我对修复并不完全满意,但它确实有效,这才是真正重要的。
感谢对 post 的评论引用 For Each
循环和一些额外的侦查,这就是我所拥有的。
Dim hour_now
Dim minute_now
Dim speaks
Dim speech
hour_now = hour(time)
minute_now = minute(time)
If minute_now = 0 Then
speaks = "Il est " & hour_now & " heures"
Else
speaks = "Il est " & hour_now & " heures " & minute_now & " minutes"
End If
Set speech = CreateObject("sapi.spvoice")
Dim strComputer
Dim TheseProcs
Dim colProcessList
Dim mycount
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")
TheseProcs = Array("zoom", "teams")
mycount = 0
For Each FindProc In TheseProcs
Set colProcessList = objWMIService.ExecQuery _
("Select Name from Win32_Process WHERE Name LIKE '" & FindProc & "%'")
If colProcessList.count>0 Then
'wscript.echo FindProc & " is running"
mycount = mycount + 1
End If
Next
If mycount=0 then
speech.Speak speaks
End if
Set objWMIService = Nothing
Set colProcessList = Nothing
不过,我 运行 遇到了几个路障。定义 Array()
和设置 For Each
循环非常简单。我很难 存储 循环内的输出。我尝试首先将 colProcessList
定义为 ArrayList
,然后使用 colProcessList.Add
组合每次迭代的结果,但我不断收到错误消息,指出该对象不允许这样做(我假设不兼容来自 .ExecQuery
。)。因此,然后,我将 colProcessList.count
代码带入循环并创建另一个变量来跟踪它。循环结束后,我会评估此对象的语音功能。我对这部分不满意,因为我确信有更好的方法来捕获每次迭代的输出。此外,我不满意代码将在每次迭代中执行,而不是在满足某些条件后停止(例如,mycount>0
)。我欢迎就这些问题提出意见,也欢迎使用 off/on 开关手动安排任务。
我在您的答案代码中看到的唯一问题是,如果任何可执行文件 运行 以“zoom”或“teams”开头,例如 ZoomIt.exe,它将无法说出时间或 TeamSpirit.exe。否则,这一切都可以用更少的代码行来完成。这是我的版本:
Set oSapi = CreateObject("sapi.spvoice")
Set oWMI = GetObject("winmgmts://./root/cimv2")
Function ProcessExist(Exe)
On Error Resume Next
Set oProcesses = oWMI.ExecQuery("Select Name from Win32_Process Where Name = '" & Exe & "'")
If oProcesses.Count>0 Then ProcessExist = True Else ProcessExist = False
On Error Goto 0
End Function
If Not ProcessExist("Zoom.exe") And Not ProcessExist("Teams.exe") Then
Speech = "Il est " & hour(time) & " heures "
If minute(time)>0 Then Speech = Speech & minute(time) & " minutes"
oSapi.Speak Speech
End If
检查 Exes 列表的替代版本:
Const ExeList = "Zoom,Teams"
Set oSapi = CreateObject("sapi.spvoice")
Set oWMI = GetObject("winmgmts://./root/cimv2")
Function ProcessExist(Exe)
On Error Resume Next
Set oProcesses = oWMI.ExecQuery("Select Name from Win32_Process Where Name = '" & Exe & "'")
If oProcesses.Count>0 Then ProcessExist = True Else ProcessExist = False
On Error Goto 0
End Function
Function InProcessList()
ArrExe = Split(ExeList,",")
InProcessList = False
For Each Exe In ArrExe
If ProcessExist(Exe & ".exe") Then InProcessList = True
Next
End Function
If Not InProcessList Then
Speech = "Il est " & hour(time) & " heures "
If minute(time)>0 Then Speech = Speech & minute(time) & " minutes"
oSapi.Speak Speech
End If
我编写了一个 .vbs 文件,对这种编码语言的了解几乎为零(主要代码来自
Dim hour_now, minute_now, speaks, speech
hour_now = hour(time)
minute_now = minute(time)
If minute_now = 0 Then
speaks = "Il est " & hour_now & " heures"
Else
speaks = "Il est " & hour_now & " heures " & minute_now & " minutes"
End If
Set speech = CreateObject("sapi.spvoice")
Dim i
Dim strComputer
Dim FindProc
strComputer = "."
FindProc = "zoom"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select Name from Win32_Process WHERE Name LIKE '" & FindProc & "%'")
If colProcessList.count>0 then
'wscript.echo FindProc & " is running"
else
'wscript.echo FindProc & " is not running"
speech.Speak speaks
End if
Set objWMIService = Nothing
Set colProcessList = Nothing
我想我需要将 FindProc
对象设置为两个对象的列表,然后迭代或让它一次求值。任何帮助表示赞赏。顺便说一句,我的电脑有法语语音,所以显示时间是用法语写的。
此外,如果有人对如何使用某种 off/on 按钮或开关设置此任务有任何想法,那也很有用。
TIA
我明白了。我对修复并不完全满意,但它确实有效,这才是真正重要的。
感谢对 post 的评论引用 For Each
循环和一些额外的侦查,这就是我所拥有的。
Dim hour_now
Dim minute_now
Dim speaks
Dim speech
hour_now = hour(time)
minute_now = minute(time)
If minute_now = 0 Then
speaks = "Il est " & hour_now & " heures"
Else
speaks = "Il est " & hour_now & " heures " & minute_now & " minutes"
End If
Set speech = CreateObject("sapi.spvoice")
Dim strComputer
Dim TheseProcs
Dim colProcessList
Dim mycount
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")
TheseProcs = Array("zoom", "teams")
mycount = 0
For Each FindProc In TheseProcs
Set colProcessList = objWMIService.ExecQuery _
("Select Name from Win32_Process WHERE Name LIKE '" & FindProc & "%'")
If colProcessList.count>0 Then
'wscript.echo FindProc & " is running"
mycount = mycount + 1
End If
Next
If mycount=0 then
speech.Speak speaks
End if
Set objWMIService = Nothing
Set colProcessList = Nothing
不过,我 运行 遇到了几个路障。定义 Array()
和设置 For Each
循环非常简单。我很难 存储 循环内的输出。我尝试首先将 colProcessList
定义为 ArrayList
,然后使用 colProcessList.Add
组合每次迭代的结果,但我不断收到错误消息,指出该对象不允许这样做(我假设不兼容来自 .ExecQuery
。)。因此,然后,我将 colProcessList.count
代码带入循环并创建另一个变量来跟踪它。循环结束后,我会评估此对象的语音功能。我对这部分不满意,因为我确信有更好的方法来捕获每次迭代的输出。此外,我不满意代码将在每次迭代中执行,而不是在满足某些条件后停止(例如,mycount>0
)。我欢迎就这些问题提出意见,也欢迎使用 off/on 开关手动安排任务。
我在您的答案代码中看到的唯一问题是,如果任何可执行文件 运行 以“zoom”或“teams”开头,例如 ZoomIt.exe,它将无法说出时间或 TeamSpirit.exe。否则,这一切都可以用更少的代码行来完成。这是我的版本:
Set oSapi = CreateObject("sapi.spvoice")
Set oWMI = GetObject("winmgmts://./root/cimv2")
Function ProcessExist(Exe)
On Error Resume Next
Set oProcesses = oWMI.ExecQuery("Select Name from Win32_Process Where Name = '" & Exe & "'")
If oProcesses.Count>0 Then ProcessExist = True Else ProcessExist = False
On Error Goto 0
End Function
If Not ProcessExist("Zoom.exe") And Not ProcessExist("Teams.exe") Then
Speech = "Il est " & hour(time) & " heures "
If minute(time)>0 Then Speech = Speech & minute(time) & " minutes"
oSapi.Speak Speech
End If
检查 Exes 列表的替代版本:
Const ExeList = "Zoom,Teams"
Set oSapi = CreateObject("sapi.spvoice")
Set oWMI = GetObject("winmgmts://./root/cimv2")
Function ProcessExist(Exe)
On Error Resume Next
Set oProcesses = oWMI.ExecQuery("Select Name from Win32_Process Where Name = '" & Exe & "'")
If oProcesses.Count>0 Then ProcessExist = True Else ProcessExist = False
On Error Goto 0
End Function
Function InProcessList()
ArrExe = Split(ExeList,",")
InProcessList = False
For Each Exe In ArrExe
If ProcessExist(Exe & ".exe") Then InProcessList = True
Next
End Function
If Not InProcessList Then
Speech = "Il est " & hour(time) & " heures "
If minute(time)>0 Then Speech = Speech & minute(time) & " minutes"
oSapi.Speak Speech
End If