Quickly share code / Text with AHK Pastbin

I’m working my way through my main AutoHotkey.ahk file and thought I’d share this gem which helps sharing code / text with people.

I borrowed the “guts” of it from an AHK Studio plugin.  I’m also using Maestrith’s Notify Class / Function.

Quickly share code / Text with AHK Pastbin

https://youtu.be/Dgi44702l2E

Here’s the code (but make sure you get the above mentioned Notify Class/Function.

Quickly share code / Text with AHK Pastbin


Browser_Back:: ;Select, copy, paste selected text to AutoHotkey Pastebin
clipboard:="" ;Blank out clipboard
Send, ^c ;Send Copy
ClipWait, 1 ;wait for clipboard to be populated
Clipboard:=AHKPastebin(Clipboard,"Joe Glines",1,0) ;1 will run it in your default browser, 0 doesn't
return

AHKPastebin(Content,Name:="",Notify:=1,Run:=0){
HTTP:=ComObjCreate("WinHttp.WinHttpRequest.5.1")
HTTP.Open("POST","https://p.ahkscript.org/", False)
HTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
HTTP.Send("code=" UriEncode(Content) "&name=" UriEncode(Name) "&channel=#ahkscript")
if HTTP.Status()!=200{ ;If not okay
MsgBox Something went wrong
return
}
If (Notify)
Notify().AddWindow(Content,{Time:3000,Icon:300,Background:"0x1100AA",Icon:14,Title:"Added to pastebin at: " HTTP.Option(1),TitleSize:18,size:14,TitleColor:"0xFF0000"})
If (Run)
Run % HTTP.Option(1) ;URL
return HTTP.Option(1) ;Return URL
}

;~ ;********************URI Encode string***********************************
UriEncode(Uri, RE="[0-9A-Za-z]"){
VarSetCapacity(Var,StrPut(Uri,"UTF-8"),0),StrPut(Uri,&Var,"UTF-8")
While Code:=NumGet(Var,A_Index-1,"UChar")
Res.=(Chr:=Chr(Code))~=RE?Chr:Format("%{:02X}",Code)
Return,Res
}

Comments are closed.