How can I make a program start when another program opens?

Page 1 of 2 12 LastLast

  1. Posts : 6
    Windows 7 x64
       #1

    How can I make a program start when another program opens?


    Hello everyone,

    I am currently trying to make a program start when another one opens. I don't want to have to use batch scripts since I have a few programs I would need to do this for and a batch script would make them not open correctly. I started to use task scheduler but I cant find what event ID I need to use. It would also be nice if the program ended when the first program shuts down. Any help would be great!

    -Sammy
      My Computer


  2. Posts : 10,485
    W7 Pro SP1 64bit
       #2

    Try AutoIt3.


    pseudo code:

    Code:
    do
    sleep
    until winexists(1st app)
    
    run(2nd app)
      My Computer


  3. Posts : 6
    Windows 7 x64
    Thread Starter
       #3

    UsernameIssues said:
    Try AutoIt3.


    pseudo code:

    Code:
    do
    sleep
    until winexists(1st app)
    
    run(2nd app)
    Sorry I am new to Autoit, what do I put in the ()? Also this will run when a program starts until it exits?
      My Computer


  4. Posts : 10,485
    W7 Pro SP1 64bit
       #4

    > ...new to Autoit, what do I put in the ()
    You might have already done some of the steps below:

    Once AutoIt is installed...
    ...right click on an empty place on your desktop
    ...and select New > AutoIt v3 Script from the context menu.

    Once the new file has been created...
    ...right click on it and select Edit Script from the context menu.

    You should now be in an application named SciTE. Once you enter the function named WinExists into SciTE, you can place the edit cursor next to or inside that WinExists function and press F1. That should open the help file to the WinExists function. There should be examples on how to use that WinExists function in the help file.


    > Also this will run when a program starts until it exits?
    If by this, you mean the script, then:
    Script will run when a program starts...
    No; the script will not automatically start when some other programs starts.
    ...until it exits?
    Does "it" refer to the script or "a program"?
    My guess is: "it" refers to "a program".
    Either way, the wording does not matter much. More on this later.


    The script does this...
    > I am currently trying to make a program start when another one opens.
    ...but probably not in the way that you want. More on this later.

    In the pseudo code that I posted:
    the "Run" line of code is there to make a program start...
    ...when another one opens - is handled by the Do/Sleep/Until code.


    This is later
    Frankly, I saw no need for a script like this, because I batch file could do this too. Since you said that a batch file would not work, I figured that I did not understand what you really wanted. I knew that we would need to exchange more posts until I had a better understanding of what you are after.

    > It would also be nice if the program ended when the first program shuts down.
    I ignored this. I figured that we would get back to it once we understood the task at hand.


    You mentioned using a scheduled task. By default, an app called taskeng.exe is running all of the time. That app watches for trigger events that match scheduled tasks. To use a script, it too would need to be running all of the time - to watch for events that match your desired conditions. To have one script handle multiple events is possible, but it won't be as simple as the few lines of pseudo code that I posted.

    Here is an outline of what I think you are after:

    loop starts here

    if a window that matches app1 shows up, then automatically run app2
    when the window for app1 goes away, then automatically close app2

    if a window that matches app3 shows up, then automatically run app4
    when the window for app3 goes away, then automatically close app4

    those lines get repeated for as many pairs of apps as you want


    return to where the loop starts



    Does it sound like I'm getting closer to what you are after?
      My Computer


  5. Posts : 6
    Windows 7 x64
    Thread Starter
       #5

    UsernameIssues said:
    > ...new to Autoit, what do I put in the ()
    You might have already done some of the steps below:

    Once AutoIt is installed...
    ...right click on an empty place on your desktop
    ...and select New > AutoIt v3 Script from the context menu.

    Once the new file has been created...
    ...right click on it and select Edit Script from the context menu.

    You should now be in an application named SciTE. Once you enter the function named WinExists into SciTE, you can place the edit cursor next to or inside that WinExists function and press F1. That should open the help file to the WinExists function. There should be examples on how to use that WinExists function in the help file.


    > Also this will run when a program starts until it exits?
    If by this, you mean the script, then:
    Script will run when a program starts...
    No; the script will not automatically start when some other programs starts.
    ...until it exits?
    Does "it" refer to the script or "a program"?
    My guess is: "it" refers to "a program".
    Either way, the wording does not matter much. More on this later.


    The script does this...
    > I am currently trying to make a program start when another one opens.
    ...but probably not in the way that you want. More on this later.

    In the pseudo code that I posted:
    the "Run" line of code is there to make a program start...
    ...when another one opens - is handled by the Do/Sleep/Until code.


    This is later
    Frankly, I saw no need for a script like this, because I batch file could do this too. Since you said that a batch file would not work, I figured that I did not understand what you really wanted. I knew that we would need to exchange more posts until I had a better understanding of what you are after.

    > It would also be nice if the program ended when the first program shuts down.
    I ignored this. I figured that we would get back to it once we understood the task at hand.


    You mentioned using a scheduled task. By default, an app called taskeng.exe is running all of the time. That app watches for trigger events that match scheduled tasks. To use a script, it too would need to be running all of the time - to watch for events that match your desired conditions. To have one script handle multiple events is possible, but it won't be as simple as the few lines of pseudo code that I posted.

    Here is an outline of what I think you are after:

    loop starts here

    if a window that matches app1 shows up, then automatically run app2
    when the window for app1 goes away, then automatically close app2

    if a window that matches app3 shows up, then automatically run app4
    when the window for app3 goes away, then automatically close app4

    those lines get repeated for as many pairs of apps as you want


    return to where the loop starts


    Does it sound like I'm getting closer to what you are after?
    Yes that's exactly what I need, would it be better to use a script or task scheduler?
      My Computer


  6. Posts : 10,485
    W7 Pro SP1 64bit
       #6

    Like you mentioned - there probably is not an event for Task Scheduler to trigger on. A script is better. You will probably keep adding to it as you think of other things that you want done automatically. (e.g. not only open the second app, but move the window to the desired location/size on the screen.)

    We will start with a script that only works for one pair of programs. Then once you understand that code, we can try to make one script work for multiple pairs of programs.

    When you make a new script file (as detailed in an earlier post), you should see some text already in the file. You can remove all of that. The script that you are writing is not complicated enough to care about which version of AutoIt3 is needed to run it. That said, it won't hurt anything to leave any or all of that text if you want it there.

    Since I don't know what your apps are using, I'll have to use code that works with simple native apps like Notepad (app1) and Calculator (app2).

    Paste the code below into a script file:

    Code:
    Opt("TrayIconDebug", 1)
    Opt("WinTitleMatchMode", 2)
    
    While 1
        Sleep(50)
        If WinExists("Notepad") Then
            Run("c:\windows\system32\calc.exe")
            WinWait("Calculator")
            WinWaitClose("Notepad")
            WinClose("Calculator")
        EndIf
    WEnd
    I suggest that you use AutoIt's help file to see what each line of code does. Feel free to ask questions if you don't understand something.

    Before you start that script...
    ...I should tell you how to end that script:
    When the script runs, it should place an AutoIt icon in Task bar's notification area.
    (You might want to set that icon to always show.)

    Click on the AutoIt icon in Task bar's notification area...
    ...doing so should pause the script.
    ...select Script Paused to continue running the script.
    ...select Exit to end the script.


    If the script is open in the SciTE editor, press F5 to run the script.
    Otherwise, save the script file and double click on it to run it.

    While the script is running:
    Open Notepad - Calculator should open.
    Close Notepad - Calculator should close.
    Lather/Rinse/Repeat

    You can change the code to work with your app1 and app2.
      My Computer


  7. Posts : 6
    Windows 7 x64
    Thread Starter
       #7

    UsernameIssues said:
    Like you mentioned - there probably is not an event for Task Scheduler to trigger on. A script is better. You will probably keep adding to it as you think of other things that you want done automatically. (e.g. not only open the second app, but move the window to the desired location/size on the screen.)

    We will start with a script that only works for one pair of programs. Then once you understand that code, we can try to make one script work for multiple pairs of programs.

    When you make a new script file (as detailed in an earlier post), you should see some text already in the file. You can remove all of that. The script that you are writing is not complicated enough to care about which version of AutoIt3 is needed to run it. That said, it won't hurt anything to leave any or all of that text if you want it there.

    Since I don't know what your apps are using, I'll have to use code that works with simple native apps like Notepad (app1) and Calculator (app2).

    Paste the code below into a script file:

    Code:
    Opt("TrayIconDebug", 1)
    Opt("WinTitleMatchMode", 2)
    
    While 1
        Sleep(50)
        If WinExists("Notepad") Then
            Run("c:\windows\system32\calc.exe")
            WinWait("Calculator")
            WinWaitClose("Notepad")
            WinClose("Calculator")
        EndIf
    WEnd
    I suggest that you use AutoIt's help file to see what each line of code does. Feel free to ask questions if you don't understand something.

    Before you start that script...
    ...I should tell you how to end that script:
    When the script runs, it should place an AutoIt icon in Task bar's notification area.
    (You might want to set that icon to always show.)

    Click on the AutoIt icon in Task bar's notification area...
    ...doing so should pause the script.
    ...select Script Paused to continue running the script.
    ...select Exit to end the script.


    If the script is open in the SciTE editor, press F5 to run the script.
    Otherwise, save the script file and double click on it to run it.

    While the script is running:
    Open Notepad - Calculator should open.
    Close Notepad - Calculator should close.
    Lather/Rinse/Repeat

    You can change the code to work with your app1 and app2.
    Thank you so much, I got it to work and I only needed to add a sleep command before the second program opens to give the first one time to open. I have one more question, I want this to work with 2 programs, how would I make this into one script?

    EDIT: Could I use an elseif and if it is do I just copy the code from the first one and edit it slightly?
      My Computer


  8. Posts : 10,485
    W7 Pro SP1 64bit
       #8

    Try to avoid using the Sleep function to make the script wait for stuff to happen. The script can become unreliable if something else loads your computer down right when that Sleep function runs. In other words, a Sleep of two seconds might work 80% of the time, but you really should have a script that works 100% of the time.

    The Sleep function that I placed right after the While 1 line is there to keep the loop from using too much of the CPU. Sleep can be used to generically slow everything down, but it should not be used to make the script wait for an event. Is there some field that is ready to edit in app1 (Notepad)? Maybe we can add code that waits for something to be ready in app1 before starting app2. If not, then a Sleep function will have to do.


    I don't think that ElseIf will help us here, but kudos for the thought.


    Let's work thru each line of code and I think that you will see the problem.
    (But it is a problem that can be solved - in most cases.)

    We have a loop. This means that the script is going to do the same code over and over as fast as it can. If the code was just this:

    While 1 (the start of the loop)
    If a window exists with notepad in the title then start calc.exe
    WEnd (go to the start of the loop)

    As soon as a window appeared with Notepad in the title, calc.exe would start. The script goes to the start of the loop, sees Notepad again, and starts another calc.exe. There would be so many calculator windows on the screen, it would look like you won at Solitaire.

    We can tell the script to stop looping until Notepad closes. That is what the WinWaitClose("Notepad") line does in the original script. It prevents the Solitaire effect by halting the script until Notepad closes. Sadly, with the script halted, it cannot check for more windows... so no more pairs of apps can be handled by the one script. You could run one script for each pair of apps, but there might be a better solution:

    Code:
    Opt("TrayIconDebug", 1)
    Opt("WinTitleMatchMode", 2)
    
    $Notepad = "no"
    $WordPad = "no"
    
    While 1
        Sleep(50)
    
        If WinExists("Notepad") And $Notepad = "no" Then
            Run("c:\windows\system32\calc.exe")
            $Notepad = "yes"
        EndIf
        If Not WinExists("Notepad") Then
            WinClose("Calculator")
            WinWaitClose("Calculator")
            $Notepad = "no"
        EndIf
    
        If WinExists("WordPad") And $WordPad = "no" Then
            Run("c:\windows\system32\mspaint.exe")
            $WordPad = "yes"
        EndIf
        If Not WinExists("WordPad") Then
            WinClose("Paint")
            WinWaitClose("Paint")
            $WordPad = "no"
        EndIf
    WEnd
    The code above might work for your needs, but it has a problem. you could never run the Calculator app unless Notepad is running. You could never run Paint, unless WordPad is running. If that is a problem for your pairs of apps, then we will need more code.
      My Computer


  9. Posts : 10,485
    W7 Pro SP1 64bit
       #9

    Before I forget:
    On the Windows Task bar
    Click on Start and type in AutoIt Window Info
    Open either the 32bit or 64bit version.
    Locate the Finder Tool (right side, above middle).
    Drag/drop that tool on to any other window (or your desktop).
    The window title should appear in the AutoIt Window Info app.
    This can help you avoid typos when used as mentioned below.

    In the image below, after using the Finder Tool,
    double clicking where the red arrow is should put
    Document - WordPad
    into the Windows clipboard so that you can paste the title into your script.

    How can I make a program start when another program opens?-wp.png


    Using
    Opt("WinTitleMatchMode", 2)
    WinExists("WordPad")
    will match a window with the title of
    Document - WordPad
    or
    Sample - WordPad
    or
    test - WordPad


    Using
    Opt("WinTitleMatchMode", 3)
    WinExists("Document - WordPad")
    will only match a window with the title of
    Document - WordPad


    You might need to use the exact title matching mode.
      My Computer


  10. Posts : 6
    Windows 7 x64
    Thread Starter
       #10

    UsernameIssues said:
    Try to avoid using the Sleep function to make the script wait for stuff to happen. The script can become unreliable if something else loads your computer down right when that Sleep function runs. In other words, a Sleep of two seconds might work 80% of the time, but you really should have a script that works 100% of the time.

    The Sleep function that I placed right after the While 1 line is there to keep the loop from using too much of the CPU. Sleep can be used to generically slow everything down, but it should not be used to make the script wait for an event. Is there some field that is ready to edit in app1 (Notepad)? Maybe we can add code that waits for something to be ready in app1 before starting app2. If not, then a Sleep function will have to do.


    I don't think that ElseIf will help us here, but kudos for the thought.


    Let's work thru each line of code and I think that you will see the problem.
    (But it is a problem that can be solved - in most cases.)

    We have a loop. This means that the script is going to do the same code over and over as fast as it can. If the code was just this:

    While 1 (the start of the loop)
    If a window exists with notepad in the title then start calc.exe
    WEnd (go to the start of the loop)

    As soon as a window appeared with Notepad in the title, calc.exe would start. The script goes to the start of the loop, sees Notepad again, and starts another calc.exe. There would be so many calculator windows on the screen, it would look like you won at Solitaire.

    We can tell the script to stop looping until Notepad closes. That is what the WinWaitClose("Notepad") line does in the original script. It prevents the Solitaire effect by halting the script until Notepad closes. Sadly, with the script halted, it cannot check for more windows... so no more pairs of apps can be handled by the one script. You could run one script for each pair of apps, but there might be a better solution:

    Code:
    Opt("TrayIconDebug", 1)
    Opt("WinTitleMatchMode", 2)
    
    $Notepad = "no"
    $WordPad = "no"
    
    While 1
        Sleep(50)
    
        If WinExists("Notepad") And $Notepad = "no" Then
            Run("c:\windows\system32\calc.exe")
            $Notepad = "yes"
        EndIf
        If Not WinExists("Notepad") Then
            WinClose("Calculator")
            WinWaitClose("Calculator")
            $Notepad = "no"
        EndIf
    
        If WinExists("WordPad") And $WordPad = "no" Then
            Run("c:\windows\system32\mspaint.exe")
            $WordPad = "yes"
        EndIf
        If Not WinExists("WordPad") Then
            WinClose("Paint")
            WinWaitClose("Paint")
            $WordPad = "no"
        EndIf
    WEnd
    The code above might work for your needs, but it has a problem. you could never run the Calculator app unless Notepad is running. You could never run Paint, unless WordPad is running. If that is a problem for your pairs of apps, then we will need more code.
    I do need to be able to run the second program without the first and also this is for a game called counterstrike so whenever I watch a video with the same name as the game in chrome the 2nd program opens, is there a way to fix that?
      My Computer


 
Page 1 of 2 12 LastLast

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 01:03.
Find Us