This script will check if a program is currently running and if it is then it will bring that program's window into view. Otherwise it will start the program. It works with windows that are in the taskbar or minimized to the tray. In the download below, I've included the *source-file as-well as a .exe for those of us who like to keep things portable.
This is useful if the .exe opens a new instance of a program instead of bringing up the existing one (Firefox ), if you get an error trying to open more than a single instance of a program and are forced to use the tiny icon in your tray (KeePass ), if you want to move icons to the hidden part of your tray but still have quick access to them using shortcuts, or even just to get more functionality out of a single button.
This script takes 3 arguments in this order...
- Window title: The program will look for a window that INCLUDES this value somewhere in its title (Case-sensitive)
- Process executable: The .exe name of the process (Open task manager --> right-click a process --> Properties)
- Program Executable: The FULL path to the .exe you want to run if no running process is found (ex: "C:\Program Files\Adobe\Photoshop\Photoshop.exe")
Code: Select all
; If a process is running then bring its window to the front. Otherwise start the program.
; Pass 3 arguments, in the appropriate order, using the terminal
; 1) Window title: The program will look for a window that INCLUDES this value somewhere in its title (Case-sensitive)
; 2) Process executable: The .exe name of the process (Open task manager --> right-click a process --> Properties)
; 3) Program Executable: The FULL path to the .exe you want to run if no running process is found (ex: "C:\Program Files\Adobe\Photoshop\Photoshop.exe")
#NoTrayIcon
DetectHiddenWindows, On
SetTitleMatchMode, 2
Process, Exist, %2%
if !ErrorLevel = 0
{
WinGet, State, MinMax, %1%
if State = -1 ; -1 == Window is minimized
{
WinRestore, %1%
}
WinSet, AlwaysOnTop, On, %1%
sleep, 100
WinSet, AlwaysOnTop, Off, %1%
WinActivate, %1%
}
else
{
Run, %3%
}
* Because of my computer setup (Most things running portable) only the .exe was tested and verified to work. I can't test the .ahk without installing AHK directly to my system, which I rather not do.