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!
@MosheKatz: Can you show me some example of exit code please? I would like to know what happens when I hit the X button.. – neoDev – 2014-03-11T21:01:50.957
6Taskkill sends WM_CLOSE message to the target program unless /F (forced termination) is specified, to allow the program to have chances to clean up itself. Test this with Notepad - open a notepad and type something there, and enter "taskkill /im notepad.exe". You will see a prompt to save the data. – Scott Rhee – 2014-03-11T21:01:57.990
@ScottRhee: I already tried to run it without /F, but seems to be an unproper close... Am I wrong? – neoDev – 2014-03-11T21:07:17.517
@ScottRhee: Im trying with Chrome too! Sometimes, when I relaunch it says "restore tabs" in a yellow line under the URL similar to this: http://backroom.bostonproductions.com/wp-content/uploads/2012/07/image1.png
– neoDev – 2014-03-11T21:17:45.100@Roger That's strange as I don't get that message with Taskkill /IM chrome.exe. Anyway, I believe you did the right thing. I wrote an answer below so please try that instead. – Scott Rhee – 2014-03-11T21:32:22.080
If
taskkill
works the same as TaskManager (which I haven't verified, but strongly suspect), then it works by first sendingWM_CLOSE
, and setting a timer (something like 100-200ms) after whichTerminateProcess
is called if the process still exists. That allows a responsive application (usually) to exit gracefully, and will blast a non-responsive application out of existence anyway. If Chrome shows an improper shutdown dialog on next start, it probably takes too long to shut down. – Damon – 2014-03-12T10:44:46.220