DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: DONE: Close/run a program when another program is run/closed  (Read 16,872 times)

Phoenix232

  • Supporting Member
  • Joined in 2017
  • **
  • Posts: 9
The idea is to close/kill Program 1 as Program 2 starts and then when Program 2 is closed, Program 1 is started again. Thinking this might be an easy task I've been trying to modify some scripts I found here but without at any success at all because I'm really new at AHK.

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 11,205
    • Participated in NANY Challenge Fourteen Times
    • Coding Snacks
    • Writer
    • Hardware Hacker
Re: IDEA: Close/run a program when another program is run/closed
« Reply #1 on: August 02, 2017, 06:16 PM »
Are either one of the programs in any danger of idle waits for user interaction?  i.e. trying to close notepad with text in it?  Just figuring it would be a good question for whomever picks it up.

skwire

  • I eat Coding Snacks for breakfast...
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,297
    • Participated in NANY Challenge Seventeen Times
    • Coding Snacks
    • Superboy Coder
    • Cody Award
    • Volunteers/Staff
    • Gathering Badge
Re: IDEA: Close/run a program when another program is run/closed
« Reply #2 on: August 02, 2017, 07:09 PM »
How did you want to match the program(s)?  Process name?  Window title?

skwire

  • I eat Coding Snacks for breakfast...
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,297
    • Participated in NANY Challenge Seventeen Times
    • Coding Snacks
    • Superboy Coder
    • Cody Award
    • Volunteers/Staff
    • Gathering Badge
Re: IDEA: Close/run a program when another program is run/closed
« Reply #3 on: August 02, 2017, 07:36 PM »
At any rate, here's an AHK snippet demonstrating how to do it with process names.  This can be easily adapted to work with window titles instead.

Code: ( autohotkey)
#Persistent
DetectHiddenWindows, On

sProgram1 := "notepad.exe"
sProgram2 := "calc.exe"
nSeconds  := 1
SetTimer, Timer, % nSeconds * 1000
Return

Timer:
{
    Process, Exist, % sProgram1
    myProgram1PID := ErrorLevel
    Process, Exist, % sProgram2
    myProgram2PID := ErrorLevel
    
    If ( myProgram2PID )
    {
        Process, Close, % myProgram1PID
    }
    Else
    {
        If ( ! myProgram1PID )
        {
            Run, % sProgram1
        }
    }
}
Return

Phoenix232

  • Supporting Member
  • Joined in 2017
  • **
  • Posts: 9
Re: IDEA: Close/run a program when another program is run/closed
« Reply #4 on: August 02, 2017, 10:44 PM »
At any rate, here's an AHK snippet demonstrating how to do it with process names.  This can be easily adapted to work with window titles instead.

Code: ( autohotkey)
#Persistent
DetectHiddenWindows, On

sProgram1 := "notepad.exe"
sProgram2 := "calc.exe"
nSeconds  := 1
SetTimer, Timer, % nSeconds * 1000
Return

Timer:
{
    Process, Exist, % sProgram1
    myProgram1PID := ErrorLevel
    Process, Exist, % sProgram2
    myProgram2PID := ErrorLevel
    
    If ( myProgram2PID )
    {
        Process, Close, % myProgram1PID
    }
    Else
    {
        If ( ! myProgram1PID )
        {
            Run, % sProgram1
        }
    }
}
Return

This is pretty close of what I was thinking. It's amazing how many lines of code you used to do this. No matter how much I tried I couldn't make it run and even if I could I don't think it would be close to have few lines like yours do. Thank you very much for this.

skwire

  • I eat Coding Snacks for breakfast...
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,297
    • Participated in NANY Challenge Seventeen Times
    • Coding Snacks
    • Superboy Coder
    • Cody Award
    • Volunteers/Staff
    • Gathering Badge
Re: IDEA: Close/run a program when another program is run/closed
« Reply #5 on: August 02, 2017, 10:49 PM »
You're very welcome.  Please let us know if you need further help.  :Thmbsup:

skwire

  • I eat Coding Snacks for breakfast...
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,297
    • Participated in NANY Challenge Seventeen Times
    • Coding Snacks
    • Superboy Coder
    • Cody Award
    • Volunteers/Staff
    • Gathering Badge
Re: IDEA: Close/run a program when another program is run/closed
« Reply #6 on: August 07, 2017, 07:21 PM »
@Phoenix232: If you don't require further help on this, do you mind if I move this to the Finished section?

Phoenix232

  • Supporting Member
  • Joined in 2017
  • **
  • Posts: 9
Re: IDEA: Close/run a program when another program is run/closed
« Reply #7 on: October 19, 2017, 04:33 PM »
@Phoenix232: If you don't require further help on this, do you mind if I move this to the Finished section?

Not at all. Sorry for the late response.

Original address: https://www.donationcoder.com/forum/index.php?topic=44162.0