Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: How can I make a program start when another program opens?

28 Jul 2015   #1
SammyTheBEAST

Windows 7 x64
 
 
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 System SpecsSystem Spec
.
28 Jul 2015   #2
UsernameIssues

W7 Pro SP1 64bit
 
 

Try AutoIt3.


pseudo code:

Code:
do
sleep
until winexists(1st app)

run(2nd app)
My System SpecsSystem Spec
28 Jul 2015   #3
SammyTheBEAST

Windows 7 x64
 
 

Quote   Quote: Originally Posted by UsernameIssues View Post
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 System SpecsSystem Spec
.

28 Jul 2015   #4
UsernameIssues

W7 Pro SP1 64bit
 
 

> ...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 System SpecsSystem Spec
28 Jul 2015   #5
SammyTheBEAST

Windows 7 x64
 
 

Quote   Quote: Originally Posted by UsernameIssues View Post
> ...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 System SpecsSystem Spec
28 Jul 2015   #6
UsernameIssues

W7 Pro SP1 64bit
 
 

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 System SpecsSystem Spec
28 Jul 2015   #7
SammyTheBEAST

Windows 7 x64
 
 

Quote   Quote: Originally Posted by UsernameIssues View Post
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 System SpecsSystem Spec
29 Jul 2015   #8
UsernameIssues

W7 Pro SP1 64bit
 
 

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 System SpecsSystem Spec
29 Jul 2015   #9
UsernameIssues

W7 Pro SP1 64bit
 
 

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 System SpecsSystem Spec
29 Jul 2015   #10
SammyTheBEAST

Windows 7 x64
 
 

Quote   Quote: Originally Posted by UsernameIssues View Post
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 System SpecsSystem Spec
Reply

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




Thread Tools Search this Thread
Search this Thread:

Advanced Search




Similar help and support threads
Thread Forum
one program opens on 3rd monitor
Hello : I have one program that on start up , it opens on the 3 rd monitor . I have tried the dragging to the correct monitor , maximizing / minimizing the program , but it always returns to the other monitor ... Ideas and thanks for having a community like this ..
General Discussion
Changing theme when program opens
Hey guys I have a quick question, I play videogames on my pc and get much better performance when I go to windows classic, but I like the look of windows aero for general computing. Is there any way I can make the computer change themes when a certain program is open, like battlefield 3, and switch...
Themes and Styles
Windows Runs Installation of Program When I Start Program
I installed Weathrbug Plus and for some odd reason recently when I run it Windows runs the installation for the program. When I run the prograsm I get the typical "Preparing to install..." abnd then tyhe 'Configuring Weatherbug. I've been in contact with Weatherbug Support all day and we can't seem...
Software
How to make a program start at windows startup:
How to make a program start at windows start-up: Run - These are the most common start-up locations for programs to install auto start from. By default these keys are not executed in Safe mode. If you prefix the value of these keys with an asterisk, *, it will run in Safe Mode. ...
General Discussion
Click on shortcut, other program opens
64 bit Win 7 Ultimate Had a few problems recently with my PC, finally have them resolved, I hope. I only use IE 9 and have a short cut icon on my desk top. When I click on it, Ask Jeeves appears instead. If I go to start/programs, click on IE 9, same thing happens.
Browsers & Mail
LiveMail opens the MSN site not the program!
Hi! I have decided to use MSN Messenger and LiveMail on my Windows 7 laptop. I signed up for a LiveMail email address and this works fine on MSN Messenger. If I click the envelope icon on the top right of the MSN Messenger screen, it causes the MSN web site to open and not the LiveMail...
Software


Our Sites

Site Links

About Us

Find 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 08:20.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App