Better use ifwinactive or #ifwinactive

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Better use ifwinactive or #ifwinactive

21 Jan 2018, 15:43

I use the hotkey #P to enter login info where it is needed. Of course, the login data and the necessary actions depend on the program / internet site that is active, and there are quite a lot of different situations to handle.

Which method is better (in respect to minimizing system load):

Code: Select all

#ifwinactive windowname1
#p:: 
click 400,820
send username1{tab}password1{enter]
return

#ifwinactive windowname2
#p:: 
click 530,680                                 
send username2{tab}password2
click 900,610
return

etc.
or

Code: Select all

#p::
ifwinactive windowname1
   {
   click 400,820
   send username1{tab}password1{enter]
   return
   }
ifwinactive windowname2
   {
   click 530,680
   send username2{tab}password2
   click 900,610
   return
   }

etc.
or should I better first use wingettitle to read the title of the active window into a variable and compare the contents of the variable to the different window names that I want to handle?
User avatar
Spawnova
Posts: 554
Joined: 08 Jul 2015, 00:12
Contact:

Re: Better use ifwinactive or #ifwinactive

21 Jan 2018, 16:00

I'd probably use wingettitle

here's an example code that you may find usefull

Code: Select all

windows := []
windows["Notepad++"] := {username:"myName",password:"123abc",x:50,y:50}
windows["Calculator"] := {username:"myName",password:"123abc",x:50,y:50}
windows["Untitled - Notepad"] := {username:"myName",password:"123abc",x:50,y:50}


numpad1:: ;method 1, in case the title changes, use inStr
WinGetTitle, Title, A
for wTitle, wData in windows
{
	if inStr(Title,wTitle) {
		x := wData.x
		y := wData.y
		click,%x%,%y%
		sleep 1000
		send % wData.username "{tab}" wData.password "{enter}"
		break
	}
}
return

numpad2:: ;method 2, if the title never changes, inStr not needed
WinGetTitle, Title, A
if(windows.hasKey(Title)) {
	x := windows[Title].x
	y := windows[Title].y
	click,%x%,%y%
	sleep 1000
	send % windows[Title].username "{tab}" windows[Title].password "{enter}"
}
return

f9::reload
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: Better use ifwinactive or #ifwinactive

21 Jan 2018, 16:52

This would be nice, but it is not possible this way, because depending on the program / site, the actions are somewhat different, so it is not possible to do the task in such a loop.
Sometimes the login button has to be clicked, in other login screens the enter key works, in one case the script has to wait before clicking on the login button until I have solved a captcha (in this case, the script checks the colour of a specific screen pixel that changes its colour when the captcha is correctly solved), in some other programs the script has to do different things after the login. I even have one internet site where the position of the input field is not always the same, so the script first has to find out where the input field is before clicking there.

Currently I have a big script that is loaded when the computer starts and automates many different tasks with a lot of different hotkeys.
For all kinds of login screens, I currently use #ifwinactive ... and #p:: , but I am reorganising the sript and adding several other logins, and I am asking myself if many #ifwinactive directives might degrade the performance of the system.
Last edited by braunbaer on 21 Jan 2018, 17:16, edited 1 time in total.
User avatar
Spawnova
Posts: 554
Joined: 08 Jul 2015, 00:12
Contact:

Re: Better use ifwinactive or #ifwinactive

21 Jan 2018, 17:01

Ah, I see, that is indeed a lot of variables to consider.

In that case, personally I would still use WinGetTitle and just have a lot of if/else statements, possibly making a few functions to wrap common actions.

There may be a more efficient way, but I can't think of anything atm.
User avatar
divanebaba
Posts: 805
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Better use ifwinactive or #ifwinactive

21 Jan 2018, 17:21

Don't you have to close the directive? Or is it not necessary? Like below?

Code: Select all

#ifwinactive windowname1
#p:: 
click 400,820
send username1{tab}password1{enter]
return
#ifwinactive ; <------ closing directive

#ifwinactive windowname2
#p:: 
click 530,680                                 
send username2{tab}password2
click 900,610
return
#ifwinactive ; <------ closing directive

; etc
Einfach nur ein toller Typ. :mrgreen:
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: Better use ifwinactive or #ifwinactive

21 Jan 2018, 17:35

No. Any #ifwinactive automatically overrides the previous #ifwinactive directive.

@Spawnova
I am afraid you are right. That is going to be a lot of typing :evil: but it probably is the best solution.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 239 guests