Using loop to run other AHK scripts

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
gFisher1234
Posts: 2
Joined: 07 Dec 2017, 16:57

Using loop to run other AHK scripts

08 Dec 2017, 23:19

Hi there, I am very new to AHK. I am trying to write a script that constantly checks if mstsc.exe (remote desktop) is running.
On closing remote desktop the script should show a message box with yes and no buttons. If no is clicked then the script exits. If yes is selected then it should run another AHK script which opens remote desktop and enters an IP address (this is complete and runs).
It all works so far... apart from when the yes button is clicked, the other AHK does not run. Am i missing something here, thanks in advance



#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.


loop {
Process, Exist, mstsc.exe
If (ErrorLevel = 0) ; If it is not running
{
MsgBox, 4, Cherry Mirror Helper, Would you like another lane?
If MsgBox Yes
Run, "C:\Users\gareth.fisher\Documents\AutoHotkey\FTP.exe"
exit
If MsgBox No
exit
}

Else
{
sleep 1000
}
}
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Using loop to run other AHK scripts

08 Dec 2017, 23:55

You have written If MsgBox. You need the specific command IfMsgBox -- no space. This is the only change you should need to make to get your code working; some extra learning is ahead:

Also note that your Yes IfMsgBox would only apply to the Run command, not the Exit command. Either wrap those two lines in a Block { }, or understand that your script is never reading the If MsgBox No (corrected to IfMsgBox No) and the second exit line because no matter what you select in the MsgBox, it would always hit the first exit command. So you can remove the If MsgBox No and it's else.

Additionally, do note that there is the subcommand to Process called WaitClose. This would let you avoid doing the Loop if you prefer.

Hope this helps you!
User avatar
boiler
Posts: 16975
Joined: 21 Dec 2014, 02:44

Re: Using loop to run other AHK scripts

08 Dec 2017, 23:56

(Edit: Was posting at the same time as the above post, so ignore if you wish)

You meant to use IfMsgBox but you didn't because of the extra space. Also, the IfMsgBox No never executes in your script (and isn't necessary anyway) because it exits when it hits the exit command. You may think it's grouped with the run command, but it's not. Look at the way I have indented your code:

Code: Select all

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.


loop {
	Process, Exist, mstsc.exe
	If (ErrorLevel = 0) ; If it is not running
	{
		MsgBox, 4, Cherry Mirror Helper, Would you like another lane?
		If MsgBox Yes
			Run, "C:\Users\gareth.fisher\Documents\AutoHotkey\FTP.exe"
		exit ; <<< can never get past this because it's not part of the conditional block of code (you would have to use braces to define it).
		If MsgBox No ; this never executes, but it's not needed anyway since you get the same effect from the above
			exit 
	}

	Else 
	{
		sleep 1000
	}
}
Your code can be reduced to this since you apparently want it to exit either way:

Code: Select all

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.

loop {
	Process, Exist, mstsc.exe
	If (ErrorLevel = 0) ; If it is not running
	{
		MsgBox, 4, Cherry Mirror Helper, Would you like another lane?
		IfMsgBox Yes
			Run, "C:\Users\gareth.fisher\Documents\AutoHotkey\FTP.exe"
		ExitApp ; better to use this than exit since in some cases this won't exit the entire script
	}
	Else 
		sleep 1000
}
gFisher1234
Posts: 2
Joined: 07 Dec 2017, 16:57

Re: Using loop to run other AHK scripts

09 Dec 2017, 00:53

thanks so much to both of you for the help appreciate the extra lessons as well

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 249 guests