好吧,不骗你。我在 stackoverflow 上看到这个问题,觉得这是个很有挑战性的问题。于是我花了两个小时写了一些代码。就是这样………..
按照下面的步骤,你可以在点击 “开始 "后输入 "TaskClose notepad.exe",它就会自动将所有未被记录的记事本文件保存到桌面上。
你可以在if条件下添加和删除其他应用程序的附加设置。例如:
If InStr(SINGLECLOSEAPPS, WScript.arguments(0)) Then
SmartSendKeys strOutput,"bypass","%{F4}"
ElseIf InStr(AUTOCLOSEAPPS, WScript.arguments(0)) Then
SmartSendKeys strOutput,"","%{F4}|%s|{autoname}.txt|%s"
'Example 1: =============================================
ElseIf InStr(WScript.arguments(0), "outlook") Then
SmartSendKeys strOutput,"","%{F4}" 'Activate Alt+4
'========================================================
'Example 2: =============================================
ElseIf InStr(WScript.arguments(0), "notepad") Then 'I know, already in my autoapps
SmartSendKeys strOutput,"","%{F4}|%s|{autosave}.txt" 'Activate Alt+4 + Save File + {AutoSave} = autoincrement filename
'========================================================
Else
SmartSendKeys strOutput,"bypass","%{F4}"
End If
for /f "tokens=10 delims=," %%F in ('tasklist /v /fi "imagename eq %1" /fo csv') do @echo %%~F >>result.txt
vbs和批处理文件执行以下程序:
- 收集可执行文件。
- 从任务列表中查询可执行程序名称。
- 然后执行滚动命令,不管是 "Alt+F4",甚至在极端情况下
- Alt+F4
- 激活保存
- 自动生成文件名
- 退出应用程序。ReturnAppList.bat:安装在 "C:\windowssystem32 "中。AND "C:\Users\YourUserName”
C:\windows\system32\wscript.exe c:\windows\system32\taskclose.vbs %1
TaskClose.vbs : install in “C:\windowssystem32\”
“`
Set WshShell = CreateObject("WScript.Shell”)
Const SINGLECLOSEAPPS = “chrome.exe|iexplore.exe|firefox.exe”
Dim DEFAULTSAVELOCATION : DEFAULTSAVELOCATION = wshshell.SpecialFolders(“Desktop”)
Const AUTOCLOSEAPPS = “notepad.exe”
Const WshFinished = 1
Const WshFailed = 2
strCommand = “returnapplist.bat ”
Set WshShellExec = WshShell.Exec(strCommand & WScript.Arguments(0))
WScript.sleep 2000
Select Case WshShellExec.Status
Case WshFinished
strOutput = LoadStringFromFile(“result.txt”)
Case WshFailed
strOutput = LoadStringFromFile(“result.txt”)
End Select
‘SmartSendKeys(applicationnamearray, bypassclause, additionalcommands)
’========================
If InStr(SINGLECLOSEAPPS, WScript.arguments(0)) Then
SmartSendKeys strOutput,“bypass”,“%{F4}”
ElseIf InStr(AUTOCLOSEAPPS, WScript.arguments(0)) Then
SmartSendKeys strOutput,“”,“%{F4}|%s|{autoname}.txt|%s”
Else
SmartSendKeys strOutput,“bypass”,“%{F4}”
End If
‘SmartSendKeys(applicationnamearray, bypassclause, additionalcommands)
’========================
Function SmartSendkeys(fArr, LoopCount, RollCommands)
Dim x
Dim splt : splt = Split(farr, vbCrLf)
If loopcount = “bypass” Then
x = 0
Else
x = UBound(splt)
End If
a = 0
For s=0 To x
If Len(splt(s)) > 1 Then
Set objShell = WScript.CreateObject(“WScript.Shell”)
c = 1 : tabs = “”
Success = False
Do Until Success = True
Success = objShell.AppActivate(Trim(splt(s)))
If success <> True Then
If c = 1 Then
tabs = “{TAB}”
Else
tabs = “{TAB ” & c & “}”
End If
‘wscript.echo “Activating: ” & “%” & tabs
WshShell.SendKeys “%” & tabs
WScript.Sleep 5000
c = c + 1
If c = 100 Then
WScript.echo “App not found”
Exit Function
End If
End If
Loop
Dim cmds : cmds = Split(rollcommands, “|”)
For Each cm In cmds
If InStr(cm, "{autoname}") Then
Dim file_ext : file_ext = Split(cm, ".")
cm = DEFAULTSAVELOCATION & "autosave" & a & "." & file_ext(1)
a = a + 1
End If
WshShell.SendKeys cm
WScript.sleep 500
Next
End If
Next
End Function
Function LoadStringFromFile(filename)
Const fsoForReading = 1
Const fsoForWriting = 2
Dim fso, f
Set fso = CreateObject(“Scripting.FileSystemObject”)
Set f = fso.OpenTextFile(filename, fsoForReading)
Dim fulltext : fulltext = f.ReadAll
LoadStringFromFile = fulltext
f.Close
fso.DeleteFile(filename)
End Function
“`
这是一个很有趣的写法,我很高兴完成它比实际显示答案更多。祝你有一个愉快的一周!