Using a slightly modified version of
UrlDownloadToVar, I've made a wee script with a simple GUI which lets you shorten URLs.
Handy for using with Twitter or other sites.
It lets you choose between is.gd, tr.im, and TinyURL but it can be modified to work with any of the other ones easily enough.
Code:
;Win + U shortens URLs
#u::
Gui, Add, Edit, x12 y30 w280 h20 Default vLongURL, http://
Gui, Add, Text, x12 y10 w280 h20 , Enter long URL:
Gui, Add, DropDownList, x12 y60 w100 h70 vService AltSubmit, is.gd||tr.im|TinyURL
Gui, Add, Button, x142 y60 w50 h30 gOK Default, OK
Gui, Add, Button, x212 y60 w80 h30 gGuiClose, Cancel
Gui, Show, x327 y226 h105 w307, Shorten URL
SendInput {Right}
Return
OK:
Gui, Submit
If (Service==1) {
URL = http://is.gd/api.php?longurl=%LongURL%
shortURL := UrlDownloadToVar(URL,0)
}
Else If (Service==3) {
URL = http://tinyurl.com/api-create.php?url=%LongURL%
shortURL := UrlDownloadToVar(URL,0)
}
Else If (Service==2) {
URL = http://api.tr.im/api/trim_simple?url=%LongURL%
shortURL := UrlDownloadToVar(URL,1)
}
SendInput %shortURL%
Gui, Destroy
Return
GuiClose:
Gui, Destroy
Return
UrlDownloadToVar(URL, DeleteAmount="", Proxy="", ProxyBypass="") {
AutoTrim, Off
hModule := DllCall("LoadLibrary", "str", "wininet.dll")
;msgbox %URL%
If (Proxy != "")
AccessType=3
Else
AccessType=1
;INTERNET_OPEN_TYPE_PRECONFIG 0 // use registry configuration
;INTERNET_OPEN_TYPE_DIRECT 1 // direct to net
;INTERNET_OPEN_TYPE_PROXY 3 // via named proxy
;INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY 4 // prevent using java/script/INS
io_hInternet := DllCall("wininet\InternetOpenA"
, "str", "" ;lpszAgent
, "uint", AccessType
, "str", Proxy
, "str", ProxyBypass
, "uint", 0) ;dwFlags
iou := DllCall("wininet\InternetOpenUrlA"
, "uint", io_hInternet
, "str", url
, "str", "" ;lpszHeaders
, "uint", 0 ;dwHeadersLength
, "uint", 0x80000000 ;dwFlags: INTERNET_FLAG_RELOAD = 0x80000000 // retrieve the original item
, "uint", 0) ;dwContext
If (ErrorLevel != 0 or iou = 0) {
DllCall("FreeLibrary", "uint", hModule)
return 0
}
VarSetCapacity(buffer, 512, 0)
VarSetCapacity(NumberOfBytesRead, 4, 0)
Loop
{
irf := DllCall("wininet\InternetReadFile", "uint", iou, "uint", &buffer, "uint", 512, "uint", &NumberOfBytesRead)
NOBR = 0
Loop 4 ; Build the integer by adding up its bytes. - ExtractInteger
NOBR += *(&NumberOfBytesRead + A_Index-1) << 8*(A_Index-1)
IfEqual, NOBR, 0, break
;BytesReadTotal += NOBR
DllCall("lstrcpy", "str", buffer, "uint", &buffer)
res = %res%%buffer%
}
; The bit I added:
StringTrimRight, res, res, DeleteAmount ;Removes new lines if necessary
DllCall("wininet\InternetCloseHandle", "uint", iou)
DllCall("wininet\InternetCloseHandle", "uint", io_hInternet)
DllCall("FreeLibrary", "uint", hModule)
AutoTrim, on
return, res
}