Exit Script On Program Close

Get help with using AutoHotkey and its commands and hotkeys
ikrananka
Posts: 2
Joined: 18 Oct 2017, 16:29

Exit Script On Program Close

18 Oct 2017, 16:59

I'm trying to figure out a working script that will automatically exit when a specific open program is closed. I found a few previous threads that seemed to discuss what I needed and had some example scripts in them, but none of them seem to work for me, i.e. the script either exits upon being run or does not exit when the program is closed. The threads I'm referring to are:

https://autohotkey.com/board/topic/9254 ... gram-load/
https://autohotkey.com/board/topic/9246 ... tomations/

I will be setting the script up as a shortcut that when run will automatically start a program called "Sonos" and will then stay active and running until I manually close the Sonos program at which point I want the script to automatically exit. There will be much more in the script when it is running but for now I just want to get the basic actions of running the program and auto exiting the script on program closure working.

If it's any use, when I start the Sonos program and use Window Spy I get the following:
Window Title : Sonos
ahk_class : HwndWrapper[WpfDomain;;41dc915b-6457-4f12-89fe-16c5f0f8a80f]
ahk_exe : Sonos.exe
I tried the following:

Code: Select all

Run, C:\Program Files (x86)\Sonos\Sonos.exe
WinWait, ahk_exe Sonos.exe
SetTimer,Sonos,100
 
Sonos:
IfWinNotExist, Sonos
     ExitApp
Return
This successfully starts the Sonos program but the script then immediately exits.
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
GitHub: AAhkUser
Location: France

Re: Exit Script On Program Close

18 Oct 2017, 18:45

Hi Ikrnanka and welcolme to the AutoHotkey community.

I can't figure it out why your script immediatly exits. Maybe it could be due to the fact that Soros run with administrator privileges, while your script not. Whatever it be, try using WinWaitClose instead:

Code: Select all

Run, notepad.exe,,, PID ; run notepad and stores its ID as process in PID
WinWait, ahk_pid %PID%
SetTimer, Sonos, -1000 ; if the period parameter is negative, the timer will run only once
; other lines of code... (WinWaitClose will not block the execution of these line - if any - while it waits since a label runs its subroutine as a new separated thread)
return ; end of the auto-execute part of the script
 
Sonos: ; sonos subroutine
WinWaitClose, ahk_pid %PID%
MsgBox, The program will exit.
ExitApp
Or RunWait !?
RunWait will wait until the program finishes before continuing
source: Run/RunWait

Code: Select all

SetTimer, Sonos, -1 ; if the period parameter is negative, the timer will run only once
; other lines of code...
return ; end of the auto-execute part of the script
 
Sonos:
RunWait, notepad.exe ; RunWait will wait until the program finishes before continuing
MsgBox, The program will exit.
ExitApp
Good luck!
my scripts
ikrananka
Posts: 2
Joined: 18 Oct 2017, 16:29

Re: Exit Script On Program Close

19 Oct 2017, 10:07

Thanks, after a lot of trial and error I figured out what the problem was. Firstly, I needed to use #Persistent to ensure that the script would ONLY end on an ExitApp command. Secondly, I had to add SetTitleMatchMode and set it to 3. It would seem that my code was not correctly identifying the Sonos window and was therefore not acting upon it.

The following code works for me.

Code: Select all

#Persistent
SetTitleMatchMode, 3
Run, C:\Program Files (x86)\Sonos\Sonos.exe
WinWait, Sonos, , 5
SetTimer, Sonos, 100
 
Sonos:
IfWinNotExist, Sonos
{
    ExitApp
    Return
}
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Exit Script On Program Close

19 Oct 2017, 15:17

Although the suggestions above seem fine, I might mention this. This detects new/closed processes.

New Process Notifier - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/5698 ... ntry358038

There are also windows event hooks for detecting window-related events.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: alexolsson00, Fumetsu, Google [Bot], Peaceful, pythonker, songdg, Xtra and 40 guests