Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

Autohotkey: send username and password

Hello experts,
I use the following autohotkey to send user and password.
uid := "UserName"
pw := "Password"
!m::
Send, %uid%{Tab}%pw%{Enter}

Open in new window

I would like to enhance it by adding an error and winwait loop
The idea is to have with the same shortcut key:
1-Open the site reported in ahk
2-Wait till reference windows is active
3-Send user and password

Example of winwait active.
WinWaitActive, Untitled - Notepad, , 2
if ErrorLevel
{
    MsgBox, WinWait timed out.
    return
}
else
    WinMinimize  ; minimize the window found by WinWaitActive.

Open in new window


Complementary question: How I identify the id or name related of an url to set up winwaitactive loop?
Thank you in advance for your help.
Avatar of Joe Winograd
Joe Winograd
Flag of United States of America image

Open the site reported in ahk
I don't know what you mean by "site reported".
Wait till reference windows is active
The window Title in login dialogs varies, and also depends on browser. For example:

Gmail - Google Chrome [this is for Google Mail in Chrome]
Log In - Microsoft Edge [this is for Experts Exchange in Edge]
Yahoo - login - Mozilla Firefox [this is for Yahoo Mail in Firefox]

You may code for that in a single AutoHotkey script, but I think you'll be better off bookmarking each site in your browser so that you'll get there with one mouse click. Then use your original script (a single hotkey) when the login dialog appears to send the username and password. So, that does it with one mouse click and one hotkey...automating it beyond that is not a big time-saver, imo...unless your idea is to do this unattended.

As a side comment, storing passwords unencrypted in a plain text file is not a good idea, but I'm sure you're already aware of that.
How I identify the id or name related of an url to set up winwaitactive loop?
Here's an AutoHotkey script that displays the Title of the active window (and puts it on the clipboard):

^F10:: ; Ctrl+F10 - change it to whatever hotkey you want
WinGetTitle,ActiveWindowTitle,A ; get title of the active window
Clipboard:=ActiveWindowTitle ; put it on the clipboard
Msgbox,4096,Active Window Title,%ActiveWindowTitle%`n`n(it is on clipboard)
Return

Open in new window

Use that to identify the window Title of any URL that you visit (or, for that matter, of any window that is active). You may then use that in WinWaitActive and any AutoHotkey command that has a window Title as a parameter. Regards, Joe
instead of using autohotkey as a password manager you should really be using something like lastpass, 1password or another real password manager
Personally, I'm not a fan of password managers, but many folks in the AutoHotkey community agree with David. I've seen numerous threads at the AutoHotkey help forum about using AutoHotkey to enter usernames/passwords, and while some folks try to do it, most folks come down on the side of using a "real" password manager (although I do neither). Regards, Joe
Avatar of Luis Diaz

ASKER

Thank you very much for your feedback.
User information are not sensitive as it is related to a generic account to access an specific point of delivery in which I need to monitor logs etc.
@Joe: I tried to get activewindows but I got:
User generated imageIs there a way to identify the id related to the url to loop on it?
Thank you in advance for your help.
> I tried to get activewindows but I got

That is correct! It shows that the Title of the active window is:

Authentication - Google Chrome

> id related to the url

I don't know what you mean by that. I thought you were looking for the window Title so that you could do a WinWaitActive on it, and that's what my AutoHotkey script above shows, but do you mean something else by "id related to the url"?

Besides the window Title, there are other IDs associated with a window (not a URL, unless you mean the browser window that is displaying the URL), such as the unique ID (aka window handle - HWND), the PID (Process ID), and the ProcessName (e.g., notepad.exe).
Noted, thank you Joe.
The aim is to identify the id related to the Windows browser when I try to get an specific url:
Example:  https://www.experts-exchange.com doesn't have the same id as www.yahoo.com but the same Windows title if I try to get through chrome browser. I was thinking to loop through the ID to make sure to send user information as long as the site that I am try to reach is displayed in my web browser. I supposed that I need to display windows ahk id. Could your please help to display windows ahk id and then I will built the autohotkey? Thank you in advance for your help.
> identify the id related to the Windows browser when I try to get an specific url

What "id" do you mean?

https://www.experts-exchange.com doesn't have the same id as www.yahoo.com but the same Windows title if I try to get through chrome browser

I don't know what you mean by "same id", but the comment after that is wrong. The AutoHotkey script that I provided shows that they do NOT have the same window Title. This is true for both Chrome and Firefox, as shown below in the results of my WinGetTitle script:

Welcome to Experts Exchange - Mozilla Firefox
Welcome to Experts Exchange - Google Chrome
Yahoo - Mozilla Firefox
Yahoo - Google Chrome

Regards, Joe
Ok, now is clear. I was confused by the fact that I got an "untitled" in the website that I tried to reach when I displayed windows title. Due to this I was trying to get another way to loop on specific website.
I will try with:
setTitleMatchMode,regex
[#if winActive(" - initial string $ ahk_exe ^Chrome.exe$")

Open in new window

First, I never use MatchMode=RegEx on SetTitleMatchMode. I've always been able to accomplish what I need with MatchMode=1 (starts with), or 2 (contains), or 3 (matches exactly). So, I can't help you with that from personal experience, although the doc makes it sound straightforward, i.e., using MatchMode=RegEx changes WinTitle to accept regular expressions, so if you can get the regex that you want, it should work fine. Second, I don't know if the parameter to the WinActive function will achieve what you want, but I do know that the syntax on that line is wrong...you certainly don't want the first character to be the left bracket. Regards, Joe
Ok, I got the following result for WindowsActiveTitleUser generated imageAs a result I can loop on the WindowsActiveTitle.
So we can do something like this

uid := "UserName"
pw := "Password"

:Run chrome.exe "https://reference-site"

WinWaitActive, Authentication - Google Chrome, , 2
if ErrorLevel
{
    MsgBox, WinWait timed out.
    return
}
else
Send, %uid%{Tab}%pw%{Enter}

Open in new window


Let me know what do you think.
Thank you in advance for your help.
Problem is, websites vary. Obviously, that one website has a Title of Authentication - Google Chrome on its login page, but that's not always the case. For example, at the EE home page, the login dialog has this as its Title:

Experts Exchange | Problem Solved. - Google Chrome

Likewise, the Yahoo login page has this Title:

Yahoo - login - Google Chrome

Similarly, the Chrome login page has this:

Sign in - Google Accounts - Google Chrome

And, of course, the other issue is that you (should!) have different usernames and passwords for different sites.

You would need to write code to handle all that. As with most requirements in AutoHotkey (and, for that matter, in other programming languages), there are numerous ways to attack it. One idea in AutoHotkey is to use an associative array, which contains Key-Value pairs. The Key would be the window Title and the Value would be the username and password separated by a character that never appears in your usernames (I suspect the slash, backslash, and pipe are good choices, but pick whatever works for you). For example:

LoginArray:={"Authentication - Google Chrome":"Username01/Password01"
,"Experts Exchange | Problem Solved. - Google Chrome":"Username02/Password02"
,"Yahoo - login - Google Chrome":"Username03/Password03"
,"Sign in - Google Accounts - Google Chrome":"Username04/Password04"}

Open in new window


Of course, I wouldn't put the array like that in the AutoHotkey source code...I just did that to show you how it works. I would put the Key-Value pairs in a text file and FileRead it into the array.

In essence, you'd be building a home-brew password manager with the usernames and passwords being stored in a plain text file (an additional idea is to encrypt the text file and then decrypt it in your AutoHotkey script). You'll have to decide for yourself if that's the right way to go. Regards, Joe
Thank you very much for your feedback Joe. Just one clarification. The aim is not to manage a set of credentials, just one related to a generic account and one specific site. Windows title of specific website never change as a result we should not have issues with the winwaitactive loop.
My question is concerning the following loop proposal. What do you think about winwaitactive loop and error level?
Thank you in advance for your feedback.
uid := "UserName"
pw := "Password"

:Run chrome.exe "https://reference-site"

WinWaitActive, Authentication - Google Chrome, , 2
if ErrorLevel
{
    MsgBox, WinWait timed out.
    return
}
else
Send, %uid%{Tab}%pw%{Enter}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Joe Winograd
Joe Winograd
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello Joe,
Thank you very much for your recommendations.
I was able to cover the need as following:
^+q:: 
Run chrome.exe "https://reference-site"
WinWaitActive,Authentication - Google Chrome,,4
if ErrorLevel
{
    MsgBox, WinWait timed out.
    return
}
else
Sleep, 500
Send, {Tab}
Sleep, 15
Send, %uid%{Tab}%pw%{Enter}
Return

Open in new window

Thank you again.
You're welcome, Luis. Glad to hear that you have it working to your satisfaction. Note that only the Sleep,500 is executed in the else path, since you don't have the other statements in a block (that's why indenting is important visually). In this case, it doesn't matter, but in other cases, it could lead to a bug. Of course, in this case, you don't even need the else because the true path above it does a return, so I suggest removing the else for better clarity in the code. Regards, Joe
Thank you Joe, I will remove the else. Concerning indentation. Should I intend for better comprehension from line 4 to 8?
Your indentation is fine on lines 4-8. Point is, the code inside the if block (lines 6-7) should be indented, and it is. This can make a big difference in comprehending code when there are many levels of If/Else logic and other conditional statements, such as Loop and While. For example, here's a code snippet from one of my programs:

If (!FileExist(TempFolder))
{
  FileCreateDir,%TempFolder%
  If (ErrorLevel!=0)
  {
    MsgBox,4112,Fatal Error 001003,Error Level %ErrorLevel% trying to create temp folder`n`n%TempFolder%
    ExitApp
  }
}

Open in new window

That shows the value of code indentation as you follow the logic flow. Compare that to this:

If (!FileExist(TempFolder))
{
FileCreateDir,%TempFolder%
If (ErrorLevel!=0)
{
MsgBox,4112,Fatal Error 001003,Error Level %ErrorLevel% trying to create temp folder`n`n%TempFolder%
ExitApp
}
}

Open in new window

That works the same (the AHK interpreter doesn't care), but is a lot less clear to read, imo. Now imagine 3, 4, 5, or more levels deep in conditional logic (happens a lot in big, complex programs). The indentation becomes critical in following the logic flow, imo. Regards, Joe
Noted, thank you very much for your help.
You're very welcome.