It is currently April 19th, 2024, 9:21 pm

[AHK] ExistenceCheck: Restore open programs

Share and get help with Plugins and Addons
WirlyWirly
Posts: 17
Joined: July 15th, 2020, 6:37 am

[AHK] ExistenceCheck: Restore open programs

Post by WirlyWirly »

A few users, both directly and indirectly, helped me out a lot when modifying an existing rainmeter launcher. I've only been using it for a couple days but it's already made my computing experience much more enjoyable/productive. Therefore, I figured I'd share this little AutoHotkey script I use in my launcher, that maybe others might get some use out of.

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 :Whistle), 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 :Whistle), 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")
Examples

Code: Select all


--- snip ---
[MeterKeePass]
...
LeftMouseUpAction = ["#@#\AutoHotKey\ExistenceCheck.exe" "KeePass" "KeePass.exe" "#PROGRAMPATH#..\KeePassProPortable\KeePassProPortable.exe"]

[MeterClipStudio]
...
LeftMouseUpAction = ["#@#\AutoHotKey\ExistenceCheck.exe" "CLIP STUDIO PAINT" "CLIPStudioPaint.exe" "C:\Program Files\CELSYS\CLIP STUDIO 1.5\CLIP STUDIO PAINT\CLIPStudioPaint.exe"]

--- snip ---
ExistenceCheck.ahk

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%
}

ExistenceCheck.zip
For the most part, this script functions as you would expect, however I can't take into account how every program interacts with windows... my bad for any bugs.

* 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.
You do not have the required permissions to view the files attached to this post.
Last edited by WirlyWirly on July 17th, 2020, 7:40 pm, edited 1 time in total.
User avatar
Active Colors
Moderator
Posts: 1251
Joined: February 16th, 2012, 3:32 am
Location: Berlin, Germany

Re: [AHK] ExistenceCheck: Restore open programs

Post by Active Colors »

Woah, I will try this out. Familiar with the Firefox case. Thank you!
User avatar
Cariboudjan
Posts: 268
Joined: May 12th, 2019, 8:55 am

Re: [AHK] ExistenceCheck: Restore open programs

Post by Cariboudjan »

Very nice. Thank you. Would love to see an included RMSKIN example of this AHK script in use.