Alright, Not going to lie. I saw this on stackoverflow and thought it was a challenging question. Soooo I just spent the last 2 hours writing some code. And here it is....
After following the steps below, you can type "TaskClose notepad.exe" after hitting "Start" and it will auto save all undocumented notepad files into desktop. It will auto-close chrome.exe and save the restoration settings.
You can add and remove additional settings for other applications under the if conditions. For instance:
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
The vbs and batch files performs the following procedures:
- Collects the executable.
- Queries the executable application names off
of the tasklist.
- Performs an "Alt+TAB(x)" procedure until it has
verified the window is open.
- Then Executes the rolling commands whether it be "Alt+F4" or even in extreme cases
- Alt+F4
- Activate Save
- AutoIncrememnt Filename
- Exit application.
ReturnAppList.bat : install in "C:\windows\system32\"
for /f "tokens=10 delims=," %%F in ('tasklist /v /fi "imagename eq %1" /fo csv') do @echo %%~F >>result.txt
TaskClose.bat : install in "C:\windows\system32\" AND "C:\Users\YourUserName\"
C:\windows\system32\wscript.exe c:\windows\system32\taskclose.vbs %1
TaskClose.vbs : install in "C:\windows\system32\"
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(application_name_array, 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(application_name_array, 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
This was alot of fun to write and I'm more happy about finishing it than actually showing the answer. Have a great week!