Help keeping a script running

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ndiaz
Posts: 73
Joined: 10 Jan 2017, 17:05
Contact:

Help keeping a script running

22 Feb 2019, 10:44

Hello,

I have a script without a hotkey that waits for a window and then performs a series of steps. The problem I'm having is that I can't get it to reset and wait for the window again, so it only runs once. How can I get it to keep running so the steps will be executed whenever the window appears? I read that I could use #Persistent and a loop, tried both, no luck. Here's the script:

Code: Select all

#IfWinActive ahk_exe SDLTradosStudio.exe
#Persistent
Loop
{
WinWait,Information
WinGetText, text
Send {Esc}
RegExMatch(text,"(?<=').*?(?=')",TMname)
Sleep 100
Send !u
Sleep 100
Send {Down 6}{Enter}
Sleep 100
Send %TMname%
Sleep 100
Send {Enter}
Sleep 100
WinWait, Add Supported Language Pairs
WinClose 
}
Thank you in advance!
User avatar
sinkfaze
Posts: 616
Joined: 01 Oct 2013, 08:01

Re: Help keeping a script running

22 Feb 2019, 11:14

Try running it as a timer instead:

Code: Select all

#Persistent

SetTimer, AddLanguagePairs, 1000
return

AddLanguagePairs:
if	WinActive("ahk_exe SDLTradosStudio.exe") && WinExist("Information")
{
	WinGetText, text
	Send {Esc}
	RegExMatch(text,"'\K[^']+",TMname)
	Sleep 100
	Send !u
	Sleep 100
	Send {Down 6}{Enter}
	Sleep 100
	Send %TMname%
	Sleep 100
	Send {Enter}
	Sleep 100
	WinWait, Add Supported Language Pairs
	WinClose
}
return
ndiaz
Posts: 73
Joined: 10 Jan 2017, 17:05
Contact:

Re: Help keeping a script running

22 Feb 2019, 12:31

Thank you, but the revised script still yields the same result: it runs only once, when I follow the steps so that the Information window will appear again, the script doesn't run a second time.
User avatar
sinkfaze
Posts: 616
Joined: 01 Oct 2013, 08:01

Re: Help keeping a script running

22 Feb 2019, 14:09

Are you sure it runs only once? Did you check ListLines? I have a sneaking suspicion that you're getting stuck here:

WinWait, Add Supported Language Pairs
ndiaz
Posts: 73
Joined: 10 Jan 2017, 17:05
Contact:

Re: Help keeping a script running

22 Feb 2019, 14:52

Yes, I think you are correct. I'm not proficient enough in AHK to know what's going on, but I was just looking at the lines that were executed, and the very last one reads like this after attempting to trigger the script a second time:

027: WinWait,Add Supported Language Pairs (10.88)

Is there any way to fix this?
User avatar
sinkfaze
Posts: 616
Joined: 01 Oct 2013, 08:01

Re: Help keeping a script running

22 Feb 2019, 15:17

What it's telling you is that WinWait is not detecting the requested window, and since you did not set anything in the Seconds parameter, it will wait infinitely to find the window.

Probably the easiest thing to do is create a test hotkey with WinWait and some variation of the information needed to locate the window, and set an if/else with MsgBox. Once you've got reliable information to locate it, then put that into your working code.

Code: Select all

!z::
WinWait, << WinTitle information >>, , 3	; set with 3 second time out
if	ErrorLevel	; if the wait times out
	MsgBox, Failed to find the window.
else	MsgBox, Fount it!
return
ndiaz
Posts: 73
Joined: 10 Jan 2017, 17:05
Contact:

Re: Help keeping a script running

22 Feb 2019, 19:09

Aha! I now see what's happening, it turns out that the window named "Add Supported Language Pair" is not always part of the process. There are times when it shows at the end of the process (which I thought was happening all the time), times when it doesn't appear at all, and other times when it appears at the beginning of the process (twice, one after the other), before the "Information" window, so if I remove that line from the script, the script runs as it should, every time, and now I just have to manually close the "Add Supported Language Pair" window when it pops up.

How could I automate this as part of the same script?, i.e., close the "Add Supported Language Pair" window, when and if it appears?
User avatar
sinkfaze
Posts: 616
Joined: 01 Oct 2013, 08:01

Re: Help keeping a script running

25 Feb 2019, 08:57

Probably the easiest way will be to run two separate timers, one for your reliable task and one that waits to close the window:

Code: Select all

#Persistent

SetTimer, MainProcess, 1000
SetTimer, CloseWindow, 1000
return

MainProcess:
if	WinActive("ahk_exe SDLTradosStudio.exe") && WinExist("Information")
{
	WinGetText, text
	Send {Esc}
	RegExMatch(text,"'\K[^']+",TMname)
	Sleep 100
	Send !u
	Sleep 100
	Send {Down 6}{Enter}
	Sleep 100
	Send %TMname%
	Sleep 100
	Send {Enter}
}
return

CloseWindow:
WinWait, Add Supported Language Pairs
WinClose
return
ndiaz
Posts: 73
Joined: 10 Jan 2017, 17:05
Contact:

Re: Help keeping a script running

25 Feb 2019, 10:27

Thank you! That works very well. Thanks to your generosity I have been able not only to get the script running as needed, but I've also learned about SetTimer. I have another script that needs something similar and I believe now I'll be able to adapt it thanks to this.

Have a wonderful day!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Ineedhelplz, Spawnova and 242 guests