HOW TO: If program1.exe ends, close program2.exe Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
_bonbonboy
Posts: 12
Joined: 12 Mar 2019, 17:38

HOW TO: If program1.exe ends, close program2.exe

Post by _bonbonboy » 21 Mar 2023, 11:37

Hi all,

I would like to create a script which can close a launcher.exe once his program related is closed.

Here are the steps:
- the script runs launcher.exe
- the script is pending, as i authenticate to start program.exe (program.exe runs right way on its own after my log-in)
- then the script should continuously check if program.exe is still running
- once no program.exe is found, the script closes launcher.exe
- closing of the script


I found this idea from Exaskryz in another topic, but i don't succeed to adapt it to my need:

Code: Select all

#Persistent
#SingleInstance
Run, launcher.exe
SetTimer, check, 500
return

check:
Process, Exist, program.exe
If (ErrorLevel=0) ; process not found
Process, Close, launcher.exe
Process, Exist, launcher.exe
If (ErrorLevel=0)
Process, Close, program.exe
return
Thank you beforehand for your help :)

User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: HOW TO: If program1.exe ends, close program2.exe

Post by mikeyww » 21 Mar 2023, 12:11

Hello,

This is easier without a timer.

Code: Select all

#Requires AutoHotkey v2.0
launchApp := 'notepad.exe'
launcher  := 'ahk_exe' launchApp
program   := 'ahk_class OpusApp ahk_exe WINWORD.exe'
Run launchApp
ToolTip 'Waiting for program'
SoundBeep 2000
WinWait program
ToolTip
SoundBeep 1500
WinWaitClose
WinClose launcher
SoundBeep 1000

_bonbonboy
Posts: 12
Joined: 12 Mar 2019, 17:38

Re: HOW TO: If program1.exe ends, close program2.exe

Post by _bonbonboy » 21 Mar 2023, 12:55

Thank you for your answer.
How do you specify the location in your script?

tried with " " ("D:\XXX\YY\launcher.exe") or without, doesn't work

Code: Select all

#Requires AutoHotkey v2.0
launchApp := 'D:\XXX\YY\launcher.exe'
launcher  := 'ahk_exe' launchApp
program   := 'ahk_class OpusApp ahk_exe D:\WWW\ZZ\program.exe'
Run launchApp
ToolTip 'Waiting for program'
SoundBeep 2000
WinWait program
ToolTip
SoundBeep 1500
WinWaitClose
WinClose launcher
SoundBeep 1000

User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: HOW TO: If program1.exe ends, close program2.exe

Post by mikeyww » 21 Mar 2023, 13:59

If OpusApp is not the window class for your program, you can remove that from the WinTitle (program definition).

One thing you might want to do first is test the script as is, with no changes. It will run Notepad. You can then run Word, and then close it. See what happens. See if it works.

_bonbonboy
Posts: 12
Joined: 12 Mar 2019, 17:38

Re: HOW TO: If program1.exe ends, close program2.exe

Post by _bonbonboy » 21 Mar 2023, 18:48

As I do not own word, i substituted by wordpad.exe
However, when i ran the script, it opens well notepad.exe but when i close my wordpad window, the notepad doesn't close :/

User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: HOW TO: If program1.exe ends, close program2.exe

Post by mikeyww » 21 Mar 2023, 19:30

The script won't work unless you adjust it to fit your situation. The following worked here. A different script may yield different results.

Code: Select all

#Requires AutoHotkey v2.0
launchApp := 'notepad.exe'
launcher  := 'ahk_exe' launchApp
program   := 'ahk_exe wordpad.exe'
Run launchApp
ToolTip 'Waiting for program'
SoundBeep 2000
WinWait program
ToolTip
SoundBeep 1500
WinWaitClose
WinClose launcher
SoundBeep 1000

_bonbonboy
Posts: 12
Joined: 12 Mar 2019, 17:38

Re: HOW TO: If program1.exe ends, close program2.exe  Topic is solved

Post by _bonbonboy » 22 Mar 2023, 07:35

The script itself works with Notepad and wordpad.
However, the adaptation on my side worked in a different way because nothing happened after I closed the program.exe, I had to make the script target the process instead:

Code: Select all

#Requires AutoHotkey v2.0
launchApp := 'launcher.exe'
launcher  := 'ahk_exe' launchApp
program   := 'program.exe'
Run launchApp
ToolTip 'Waiting for program'
SoundBeep 2000
WinWait program
ToolTip
SoundBeep 1500
WinWaitClose
ProcessClose "launcher.exe"
SoundBeep 1000
Thank you again for your time and your help :)

User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: HOW TO: If program1.exe ends, close program2.exe

Post by mikeyww » 22 Mar 2023, 08:23

Sure, in some cases, a Process function is the way to go instead of targeting a window-- as you showed. This is generally true if a program has no window, has hidden windows, or has multiple windows.

Post Reply

Return to “Ask for Help (v2)”