So today I delved into the World of Autohotkey, specifically because I was too lazy to program a new application which would manage logging in and navigating some pages (using form posts) to save a file. The site I was trying to aces doesn’t have an API connector for downloading these reports, only for executing actions, so it was one way. Later on we are going to code this formally with some .NET to perform the login and download all by a program, but for now this Tool has found some purpose in my bag of tricks.
Limitations of AutoHotKey as a automated function
-
it can’t start if logged out
-
it can’t start up the computer
-
you can’t rely on window size as that changes on each opening , so really don’t use mouse functions.
-
Even though a window is active it doesn’t necessarily mean its ready for input , look at the code below where I really had to add sleep functions so that the system wouldn’t start typing in the middle of window dialogs opening
-
Windows keyboard shortcuts are your friend
On the whole, I really enjoyed the simplicity of design. It looks to me that if I had no other way of doing automated tasks this would be a very useful tool. I actually spent a few minutes thinking about our customer service staff and thinking if there are locations where this sort of automation could be handy.
I know there is 7 ways to do this better, I saw the functionality to build in IE right into AutoHotKey
; semi colons start Comments , go figure.
SaveFile(NumberofDays)
{
TimeString =
TimeString += -%NumberofDays%,days ; By setting the TimeString as Empty when I add days to it, it will default to today’s date
FormatTime,TimeString,%TimeString%,yyyyMMdd ; Formatting the time because I am going to use it in the file name
FormatTime,Days,%TimeString%,dd ; the next three are going to be used to file the right url
FormatTime,Months,%TimeString%,MM
FormatTime,Years,%TimeString%,yyyy
;Sendinput\Send basically send keystroke and mose commands, strings encapsulated by % are variables
SendInput,{F4}{DELETE} https://somesite.com/somelocation/%Years%/%Months%/%Days%/v1981406/OrderDetailReport.csv{ENTER}
Sleep, 1500
IfWinExist, Login Page ; If a window with Login Page in the title pops up, login
{
SendInput, SUPERLOGIn{TAB}PASSWORD{ENTER}
}
Sleep, 1500
;now here comes the where we look for the file download window to open and do a series of IE hotkeys to quickly get through it without mouse usage
WinWait, File Download, Do you want to open
IfWinNotActive, File Download, Do you want to open , WinActivate, File Download, Do you want to open
WinWaitActive, File Download, Do you want to open
Sleep, 1000
Send, S
WinWait, Save As,
IfWinNotActive, Save As,, WinActivate, Save As,
WinWaitActive, Save As,
sleep, 1000
Send, {END}{SHIFTDOWN}{HOME}{SHIFTUP}{DELETE} ; I do this to clear out the text space before I send any command data
SendInput, c:\downloads\%TimeString%_FILE.csv{ENTER}
sleep, 1500
WinActivate,Confirm save as,
Send, {LEFT}{ENTER}
sleep, 3000
}
MsgBox “Runnning Daily save of Cybersource file’
SetTitleMatchMode,2 ; when it does windows searches
;Hit the windows button wait for the menu to open up and then type in my command to open IE
WinWait, Program Manager,
IfWinNotActive, Program Manager, , WinActivate, Program Manager,
WinWaitActive, Program Manager,
Send, {LWINDOWN}{LWINUP}
WinWait, Start Menu,
IfWinNotActive, Start Menu, , WinActivate, Start Menu,
WinWaitActive, Start Menu,
Send, iexplore{ENTER}
WinWait,Explorer,
Sleep, 1500
;first time around I looped around it a lot so that I could download all this month’s file.
Loop,1
{
SaveFile(A_Index)
}
;Close the Windows window at the end ofi
IfWinExist, Windows Internet Explorer
{
WinClose ; use the window found above
}