AutoHotkey and URLGet 0.9.0.0
URLGet can report the progress status as 'Window Messages' when called in Four Parameter Mode.
The parameters:
- URL : A valid url wrapped in double quotes
- NoFileCopy : 0 or 1. 1 will omit file copy. By default, downloaded file is copied from cache into the current folder ( A_WorkingDir )
- CallerWnd : A handle to Window. ( hWnd of GUI )
- CallerMsg : OnMessage Num, like 0x7FFF ( refer Application defined Message Identifier )
URLGet will Send a combination of UGM ( URLGet Message ) and redirects of BINDSTATUS callback. - When wParam of OnMessage() is greater than 0x400 ( WM_USER ), it is UGM - otherwise, a redirect of BINDSTATUS
- Use lParam to recieve Numbers. For Text, you will have to retrieve it from the Console Window Titlebar.
- For each BINDSTATUS Message, OnMessage() should return 0 for URLGet to proceed, or may return 0x80004004 ( E_ABORT ) to cancel the download
Code:
; URLGet Messages:
UGM_INIT := 0x401 ; lParam = hwnd of Console
UGM_CACHEFILE := 0x404 ; use WinGetTitle on Console Window
UGM_TARGETFILE := 0x405 ; use WinGetTitle on Console Window
UGM_DEINIT := 0x406 ; lParam = result from URLDownloadToCacheFile
UGM_CANCEL := 0x410 ; lParam = 1 for acknowledgement
; BINDSTATUS
BINDSTATUS_FINDINGRESOURCE := 1
BINDSTATUS_CONNECTING := 2
BINDSTATUS_REDIRECTING := 3
BINDSTATUS_BEGINDOWNLOADDATA := 4
BINDSTATUS_DOWNLOADINGDATA := 5
BINDSTATUS_ENDDOWNLOADDATA := 6
BINDSTATUS_BEGINDOWNLOADCOMPONENTS := 7
BINDSTATUS_INSTALLINGCOMPONENTS := 8
BINDSTATUS_ENDDOWNLOADCOMPONENTS := 9
BINDSTATUS_USINGCACHEDCOPY := 10
BINDSTATUS_SENDINGREQUEST := 11
BINDSTATUS_CLASSIDAVAILABLE := 12
BINDSTATUS_MIMETYPEAVAILABLE := 13
BINDSTATUS_CACHEFILENAMEAVAILABLE := 14
BINDSTATUS_BEGINSYNCOPERATION := 15
BINDSTATUS_ENDSYNCOPERATION := 16
BINDSTATUS_BEGINUPLOADDATA := 17
BINDSTATUS_UPLOADINGDATA := 18
BINDSTATUS_ENDUPLOADINGDATA := 19
BINDSTATUS_PROTOCOLCLASSID := 20
BINDSTATUS_ENCODING := 21
BINDSTATUS_VERFIEDMIMETYPEAVAILABLE := 22
BINDSTATUS_CLASSINSTALLLOCATION := 23
BINDSTATUS_DECODING := 24
BINDSTATUS_LOADINGMIMEHANDLER := 25
BINDSTATUS_CONTENTDISPOSITIONATTACH := 26
BINDSTATUS_FILTERREPORTMIMETYPE := 27
BINDSTATUS_CLSIDCANINSTANTIATE := 28
BINDSTATUS_IUNKNOWNAVAILABLE := 29
BINDSTATUS_DIRECTBIND := 30
BINDSTATUS_RAWMIMETYPE := 31
BINDSTATUS_PROXYDETECTING := 32
BINDSTATUS_ACCEPTRANGES := 33
BINDSTATUS_COOKIE_SENT := 34
BINDSTATUS_COMPACT_POLICY_RECEIVED := 35
BINDSTATUS_COOKIE_SUPPRESSED := 36
BINDSTATUS_COOKIE_STATE_UNKNOWN := 37
BINDSTATUS_COOKIE_STATE_ACCEPT := 38
BINDSTATUS_COOKIE_STATE_REJECT := 39
BINDSTATUS_COOKIE_STATE_PROMPT := 40
BINDSTATUS_COOKIE_STATE_LEASH := 41
BINDSTATUS_COOKIE_STATE_DOWNGRADE := 42
BINDSTATUS_POLICY_HREF := 43
BINDSTATUS_P3P_HEADER := 44
BINDSTATUS_SESSION_COOKIE_RECEIVED := 45
BINDSTATUS_PERSISTENT_COOKIE_RECEIVED := 46
BINDSTATUS_SESSION_COOKIES_ALLOWED := 47
BINDSTATUS_CACHECONTROL := 48
BINDSTATUS_CONTENTDISPOSITIONFILENAME := 49
BINDSTATUS_MIMETEXTPLAINMISMATCH := 50
BINDSTATUS_PUBLISHERAVAILABLE := 51
BINDSTATUS_DISPLAYNAMEAVAILABLE := 52
BINDSTATUS_SSLUX_NAVBLOCKED := 53
BINDSTATUS_SERVER_MIMETYPEAVAILABLE := 54
BINDSTATUS_SNIFFED_CLASSIDAVAILABLE := 55
BINDSTATUS_64BIT_PROGRESS := 56
BINDSTATUS_LAST = BINDSTATUS_64BIT_PROGRESS := 57
BINDSTATUS_RESERVED_0 := 58
BINDSTATUS_RESERVED_1 := 59
BINDSTATUS_RESERVED_2 := 60
BINDSTATUS_RESERVED_3 := 61
BINDSTATUS_RESERVED_4 := 62
BINDSTATUS_RESERVED_5 := 63
BINDSTATUS_RESERVED_6 := 64
BINDSTATUS_RESERVED_7 := 65
BINDSTATUS_RESERVED_8 := 66
BINDSTATUS_RESERVED_9 := 67
Terminating download:
URLGet process can be terminated by
ControlSending ^c to the Console Window, however:
When simultaneously running multiple instances of URLGet with FileCopy in effect ( NoFileCopy=0 ), every instance will compete for filename in current folder. Providing for such cases, every copy of URLGet increments
* filename to find an available one and immediately creates a zero-byte-file to reserve it for FileCopy, before the download can even commence. Cancelling a download will delete the empty file, whereas, terminating URLGet will leave orphaned ones.
*Filename will be incremented in following pattern: filename.ext, filename
.1.ext, filename
.2.ext ... filename
.100.ext
DEMONSTRATION:Code:
SetWorkingDir %A_ScriptDir%
URL := "http://www.autohotkey.net/~Lexikos/AutoHotkey_L/AutoHotkey_L_Install.exe"
Gui +LastFound +AlwaysOnTop
hGUI := WinExist()
Gui, Font, S10, Tahoma
Gui, Add, Text, w270 h20 0x200, Initiating.. ; Static1
Gui, Add, Text, x+3 w42 hp 0x201, ; Static2
Gui, Add, Progress,xm y+3 w315 h10 cF73D00 h8, 1 ; msctls_progress321
Gui, Add, Text, xm y+3 w42 h25 y+5 c808080 0x200, Bytes: ; Static3
Gui, Add, Text, x+2 w80 h25 Right 0x200, ; Static4
Gui, Add, Text, x+2 w42 h25 Center c808080 0x200 ; Static5
Gui, Add, Text, x+2 w80 h25 Left 0x200 ; Static6
Gui, Add, Button, x+5 w60 hp gCancelDownload, &Cancel ; Button1
Gui, Show,, URLGet Demo
OnMessage( 0x7FFF, "URLGet_Callback" )
Run URLGet "%URL%" 0 %hGUI% 0x7FFF,, Hide
Return ; // end of auto-execute section //
GuiClose:
GuiEscape:
SendMessage, 0x7FFF, 0x410, False,, ahk_id %hGUI% ; UGM_CANCEL before ExitApp
hwndConsole := ErrorLevel
While DllCall( "IsWindow", UInt,hwndConsole ) ; Instead of WinWaitClose to avoid
Sleep 100 ; 'DetectHiddenWindows, On'
ExitApp
Return
CancelDownload:
SendMessage, 0x7FFF, 0x410, False,, ahk_id %hGUI%
IfEqual, A_GuiControl, &Close, ExitApp
Return
WinGetTitle( hWnd ) { ; Instead of WinGetTitle command, to avoid 'DetectHiddenWindows, On'
VarSetcapacity( Title, 4096 ), DllCall( "GetWindowText", UInt,hwnd, Str,Title, Int,4096 )
Return Title
}
UrlGet_Callback( wParam, lParam, Msg, hGUI ) {
static hwndConsole, dataSz, RetVal=0
If ( wParam = 0x401 ) { ; UGM_INIT
hwndConsole := lparam
}
If ( wParam = 0x405 ) { ; UGM_TARGETFILE
TargetFileName := WinGetTitle( hwndConsole )
SplitPath, TargetFileName, TargetFileName
GuiControl,,Static1, %TargetFilename%
}
If ( wParam = 0x406 ) { ; UGM_DEINIT
hwndConsole := dataSz := Retval := 0
GuiControl, , Button1, &Close
}
If ( wParam = 0x410 && DllCall( "IsWindow", UInt,hwndConsole ) ) { ; UGM_CANCEL
If ( lParam = False ) {
RetVal := 0x80004004 ; E_ABORT
GuiControl,,Static1, Cancelling download..
GuiControl, Disable, Button1
Return hwndConsole
} Else {
RetVal := 0
GuiControl,,Static1, Download cancelled!
GuiControl, Enable, Button1
GuiControl, , Button1, &Close
}
}
If ( wParam = 5 || wParam = 6 ) ; BINDSTATUS_DOWNLOADINGDATA
{ ; BINDSTATUS_ENDDOWNLOADDATA
GuiControl,, Static4, %lParam%
If ( dataSz > 0 ) {
prc := Round( (lParam/dataSz)*100 )
GuiControl,,Static2, %prc%`%
GuiControl,,msctls_progress321, %prc%
}
}
If ( wParam = 1 ) { ; BINDSTATUS_FINDINGRESOURCE
Domain := WinGetTitle( hwndConsole )
GuiControl,,Static1, Finding %Domain%
}
If ( wParam = 4 ) { ; BINDSTATUS_BEGINDOWNLOADDATA
If ( dataSz := lParam ) { ; Download data size
GuiControl,,Static5, out of
GuiControl,,Static6, %dataSz%
}
}
Return RetVal
}