Settimer to detect specific windows

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
thegame2010
Posts: 29
Joined: 04 Feb 2016, 18:14
Location: 'Murica

Settimer to detect specific windows

Post by thegame2010 » 26 Aug 2016, 08:58

In my job I have a lot of programs that pop up asking me to sign back in or to click OK to avoid session timeout. It's obnoxious, so I've made a couple looping scripts that work flawlessly. What I'd like to do is combine them into a single script detecting several windows with individual commands concurrently.
Here's an example of a single working script:

Code: Select all

#persistent
SetTitleMatchMode, 2
Loop 
{
	WinWait, Timeout Warning
	WinActivate
	Send {Tab}{Enter}
	sleep, 100
}
So what I'd like is something with two or more loops. This can be done with settimer but I'm struggling with syntax. Here's what I've tried, and it's not working at all:

Code: Select all

#persistent
SetTitleMatchMode, 2
SetTimer, Win1Alert, 500
SetTimer, Win2Alert, 500
return

InputBox, UserVar, Username Please, Give me your username!
InputBox, PassVar, Password Please, Give me your password!

Win1Alert:
	WinWait, Window Title 1
	WinGetTitle, Title, A
	WinActivate, Window Title 1
	Sleep, 100
	Send %UserVar%{Tab}%PassVar%
	Sleep, 50
	Send {Enter}
	sleep, 100
	WinActivate, %Title%
return

Win2Alert:
	WinWait, Window Title 2
	WinGetTitle, Title, A
	WinActivate, Window Title 2
	Send {Enter}
	sleep, 100
	WinActivate, %Title%
return
Suggestions?
GEV
Posts: 1005
Joined: 25 Feb 2014, 00:50

Re: Settimer to detect specific windows

Post by GEV » 26 Aug 2016, 09:57

Put the InputBox-commands in the auto-execute section (top of the script, before the first return), so that the script can store the text of the variables UserVar and PassVar.
https://autohotkey.com/docs/Scripts.htm#auto
User avatar
thegame2010
Posts: 29
Joined: 04 Feb 2016, 18:14
Location: 'Murica

Re: Settimer to detect specific windows

Post by thegame2010 » 26 Aug 2016, 10:46

Thanks, I was wondering about that. Hadn't gotten around to trying it (I will now though). Otherwise, it looks like it should be looking for both windows simultaneously? I'll test it now.

New code looks like this:

Code: Select all

#persistent
SetTitleMatchMode, 2

InputBox, UserVar, Username Please, Give me your username!
InputBox, PassVar, Password Please, Give me your password!

SetTimer, Win1Alert, 500
SetTimer, Win2Alert, 500
return

Win1Alert:
	WinWait, Window Title 1
	WinGetTitle, Title, A
	WinActivate, Window Title 1
	Sleep, 100
	Send %UserVar%{Tab}%PassVar%
	Sleep, 50
	Send {Enter}
	sleep, 100
	WinActivate, %Title%
return

Win2Alert:
	WinWait, Window Title 2
	WinGetTitle, Title, A
	WinActivate, Window Title 2
	Send {Enter}
	sleep, 100
	WinActivate, %Title%
return
User avatar
thegame2010
Posts: 29
Joined: 04 Feb 2016, 18:14
Location: 'Murica

Re: Settimer to detect specific windows

Post by thegame2010 » 29 Aug 2016, 17:03

Didn't get around to testing this until now. I am at work, after all... The code I have above is as correct as I can think of, and it asks me to enter my credentials now. It does not detect the windows though. I suspect it has to do with winwait, but I don't know.
punchin
Posts: 439
Joined: 17 Jan 2014, 17:54

Re: Settimer to detect specific windows

Post by punchin » 29 Aug 2016, 17:29

Use the windows spy that came with ahk and make sure you have the window titles correct. Also, some platforms (java and citrix to name a couple) do not like to play nice.
User avatar
thegame2010
Posts: 29
Joined: 04 Feb 2016, 18:14
Location: 'Murica

Re: Settimer to detect specific windows

Post by thegame2010 » 31 Aug 2016, 09:07

Note: When I have only one running at a time in a loop it works perfectly. I'm merely struggling with combining several scripts into a single one to detect 2 (or eventually like 6) things.
GEV
Posts: 1005
Joined: 25 Feb 2014, 00:50

Re: Settimer to detect specific windows

Post by GEV » 31 Aug 2016, 09:27

Try

Code: Select all

	Win1Alert:
IfWinNotExist, Window Title 1
	return ; do nothing
; otherwise:
SetTimer, Win1Alert, Off
WinActivate, Window Title 1
WinWaitActive, Window Title 1
Send %UserVar%{Tab}%PassVar%
Sleep, 50
Send {Enter}
WinWaitClose, Window Title 1
Sleep, 100
SetTimer, Win1Alert, On
return
User avatar
thegame2010
Posts: 29
Joined: 04 Feb 2016, 18:14
Location: 'Murica

Re: Settimer to detect specific windows

Post by thegame2010 » 05 Sep 2016, 13:04

I have 2 timers running, though one of them has not had the opportunity to run yet. The other (basically copying your text exactly) is working every bit as well as it did stand-alone. I'll reply once I find out if the other works too! I have about 8 things I'd like this to detect in the end and this will work marvelously, I think! Thanks a bunch!

PS: of course it's just an if-statement. WinWait does exactly that: waits. I bet it got to that line in my code and waited. I don't know, I still would have expected something to happen, but clearly it didn't.
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Settimer to detect specific windows

Post by Nextron » 05 Sep 2016, 16:55

Wouldn't it be better to just keep the loop and put every window check in it without waiting for it to exist. The issue with the timers is that they can interrupt each other, resulting in unpredictable interaction. Sure you can easily prevent that but why even bother with setting them and unsetting them.

Example, put your checks under the WindowCheck label:

Code: Select all

#persistent
SetTitleMatchMode, 2
Loop
{
	Sleep, 100 ;Checking about 10 times per second seems enough?
	WinGetTitle, Title, A
	GoSub, WindowCheck
	WinActivate, % Title
}

WinExistThenActivate(WinTitle, WinText:="",  ExcludeTitle:="", ExcludeText:=""){
	If WinExist(WinTitle, WinText,  ExcludeTitle, ExcludeText){
		WinActivate, % WinTitle, % WinText, % ExcludeTitle, % ExcludeText
		WinWaitActive, % WinTitle, % WinText, % ExcludeTitle, % ExcludeText
		Return 1
	}
}

WindowCheck:

	If WinExistThenActivate("ahk_exe calc.exe"){
		Send {+}1{Enter}
	}
	
	If WinExistThenActivate("Untitled"){
		Send NOTEPAD{Tab}Password
		Sleep 100
		Send {Enter}
	}	
	
Return
Post Reply

Return to “Ask for Help (v1)”