Since I don't believe this can be done through a simple .bat
file, this seem like a job for AutoHotKey.
AutoHotkey (AHK) is a free, open-source macro-creation and automation software for Windows that allows users to automate repetitive tasks. It is driven by a scripting language that was initially aimed at providing keyboard shortcuts, otherwise known as hotkeys, that over time evolved into a full-fledged scripting language.
According to this forum post from 2014 you should be able to toggle window visibility with a script similar to this:
#NoTrayIcon
#Persistent
global hBatFile
/* Setup Tray icon and add item that will handle
* double click events
*/
Menu Tray, Icon
Menu Tray, Icon, C:\windows\system32\cmd.exe
Menu Tray, Add, Show / Hide Pingu, TrayClick
Menu Tray, Add, Close Pingu, CloseItem
Menu Tray, Default, Show / Hide Pingu
;// Run program or batch file hidden
DetectHiddenWindows On
Run pingu.bat,, Hide, PID
WinWait ahk_pid %PID%
hBatFile := WinExist()
DetectHiddenWindows Off
return
TrayClick:
OnTrayClick()
return
;// Show / hide program or batch file on double click
OnTrayClick() {
if DllCall("IsWindowVisible", "Ptr", hBatFile) {
WinHide ahk_id %hBatFile%
} else {
WinShow ahk_id %hBatFile%
WinActivate ahk_id %hBatFile%
}
}
CloseItem() {
DetectHiddenWindows On
WinWait ahk_class ConsoleWindowClass
Process, Close, cmd.exe
DetectHiddenWindows Off
ExitApp
}
This script was tested with AutoHotKey 1.1.24.00 (May 2016).
EDIT: Here is a link to the OPs modified version with improvements.
In this instance:
- pingu.bat is the (arbitrary) name of the batch file we wish to run
- hBatFile is an arbitrary variable name holding some window information
- Pingu is an arbitrary name which shows in the additional AHK tray menu items
Simply save the script as something like tray.ahk
(note the .ahk
script extension) and double click to run it (assuming you have installed AutoHotKey).
The script starts the batch file minimized and replaces the default AHK script icon with a miniature console window icon in the tray.
Console window visibility can be toggled with a double click of the tray icon or the added tray menu item (appearing in bold at the bottom of the selection menu).
Closing both the batch file and the command window can be done with the added Close
menu item below Show / Hide
(Exit
will only close the script, not the console window).
Also note that
Run pingu.bat,, Hide, PID
will obviously need pingu.bat
to be replaced with the name of your own batch file. This line assumes that the AHK script appears in the same directory as pingu.bat
or that pingu.bat
is globally accessible (i.e. it has been added to a folder in your system Path or user PATH variables). Otherwise, you will want to replace pingu.bat
with the full path to the executable (watch for spaces!).
Caveats
This script mostly acts as an interface to window visibility. What this means is that if you Exit
via the tray icon, you are only exiting the AHK script, not your batch file.
The simple solution is to use the added Close
menu item to exit both the script and stop cmd.exe
. Alternatively, you can:
They are separate processes, as mentioned.
Note: Orphaned console programs with hidden windows cannot be directly accessed again if the AHK script is terminated with Exit
first — but you can use Task Manager to close the associated cmd.exe
process.
The Close
menu item as written works fine if there is only one cmd.exe
process. However, if more than one cmd.exe
process is running, this may not properly close the batch file you started with the script (it may close something else). You may want to look into closing by process ID (PID) instead. That said, you can use the same Show/Close/Exit process above as well.
Script Notes
Menu Tray, Icon, C:\windows\system32\cmd.exe
refers to the location of a compiled executable with an associated embedded icon file. You should be able to replace this with a reference to any executable with an embedded icon or with a direct reference to an .ico
icon file as well e.g.
Menu Tray, Icon, C:\Path\To\Icons\icon.ico
Best Answer
Save your above code at
%CMDER_ROOT%\config\user-startup.cmd
and it will auto run with you start Cmder. The%CMDER_ROOT%
folder will be where the Cmder.exe is.