How can I make a .bat file run only in system tray?
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:
Show the console window and use Ctrl + C (or simply close the console window with the red 'X') to terminate the batch process.
Select
Exit
from the tray icon to stop the AHK script
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
How do I make a *.bat file run in the tray?
You can use RBTray:
RBTray is a small Windows program that runs in the background and allows almost any window to be minimized to the system tray by right clicking its minimize button.
It is free, open source, and is distributed under the terms of the GNU General Public Licence.
It is only 45k and has both 32 and 64 bit versions.
Disclaimer
I am not affiliated with RBTray in any way, I am just an end user of their software.