February 8, 2021

Quick Create Gist (AutoHotkey script)

Recently I have learned from Joe Glines aka the-Automator this nice way to quickly create a Gist with AutoHotkey. I share here my improved solution/ workflow.

Prerequisites

GitHub token

To create a Git by API/ script you need to get a GitHub Token.

Be aware: after creation, you can not view the token at a later time. Therefore I recommend you to store it for example in your Password Manager as Secured note. (I personally use LastPass)
(If you forget it, you will need to create a new token.)

Jxon AHK Library

The provided AHK script makes use of the Jxon Library available here: https://github.com/cocobelgica/AutoHotkey-JSON
This library is also available in my AHK GitHub.

AHK Script

Improvements

From Joe's script, I have made the following changes:
  • GUI improvements
    • Add Cancel
    • Multiple lines for the Description
    • Wider GUI width
  • Clean-up Dependencies: only requires Jxon Library (replace ParseJSON by Jxon_Load)
  • No need for GitName to be filled
  • Open Url after Gist creation (especially useful for commenting the Gist e.g. linking to related blog post)
  • Works not only for AHK but any type of files / Clean-up file extension handling
  • Git Token is stored as a setting in the Registry

Get the script

The script is available on my GitHub here.
Dependencies
You could compile it as a standalone version if you have AHK installed. (If you want a compiled standalone version, you can reach out to me in the comments or via Twitter)

You can also download a more standalone version from the Gist below. 

Code

You can view the code in this Gist. 
; See related blog post https://tdalon.blogspot.com/2021/02/ahk-create-gist.html
#Include <Jxon>
#SingleInstance force
Token := PowerTools_GetSetting("GitHubToken")
If (Token="")
return
WinGetTitle, Title , A ;Get active window title
RegExMatch(Title,"(.*\\)?([^\\]*)\.([^\s]*)",filename) ;Try to isolate the file name from Window title with RegEx
filename = %filename2%.%filename3%
Code := GetSelection()
If (Code="") { ;Added errorLevel checking
MsgBox, No text selected.
Return
}
gui, add,text,,Name of file
gui, add,edit,w400 vFileName,%filename%
gui, add,text,,Description
gui, add,edit,w400 vDescr r5 ;Type in description
gui, Add, Button, default, Gist ;Create button
gui, show, autosize
return
GuiClose:
ExitApp
ButtonGist:
Gui, Submit ; Save the input from the user to each control's associated variable.
Body:=Jxon_Dump({content:Code}) ;need to encode it
Data={"description": "%Descr%","public": true,"files": {"%FileName%": %Body%}} ;build data for post having double quotes
ResponseText := Send(Token,"https://api.github.com/gists","POST",Data)
If (ResponseText="")
return
Obj := Jxon_Load(ResponseText)
;Obj:=ParseJSON(ResponseText) ;make post and return information about it
sUrl := obj.html_url
Run, "%sUrl%"
;Clipboard:="Use the following for a webpage post:`n<script src='https://gist.github.com/" GitName "/"(SubStr(obj.url,Instr(obj.url,"/",,0)+1))".js'></script>`n`nYou can get the code here: " obj.html_url
return
Send(Token,URL:="",Verb:="",Data:=""){
static WebRequest:=ComObjCreate("WinHttp.WinHttpRequest.5.1") ;Create COM object
WebRequest.Open(Verb,URL) ;Open connection
WebRequest.SetRequestHeader("Authorization","token " Token,0)
WebRequest.SetRequestHeader("Content-Type","application/json")
WebRequest.Send(Data) ;Send Payload
return WebRequest.ResponseText
}
; ----------------------------------------------------------------------
PowerTools_GetSetting(SettingName){
RegRead, Setting, HKEY_CURRENT_USER\Software\PowerTools, %SettingName%
If (Setting=""){
Setting := PowerTools_SetSetting(SettingName)
}
return Setting
} ; eofun
; ----------------------------------------------------------------------
PowerTools_SetSetting(SettingName){
; for call from Menu with Name Set <Setting>
SettingName := RegExReplace(SettingName,"^Set ","")
SettingProp := RegExReplace(SettingName," ","") ; Remove spaces
RegRead, Setting, HKEY_CURRENT_USER\Software\PowerTools, %SettingProp%
InputBox, Setting, PowerTools Setting, Enter %SettingName%,, 250, 125
If ErrorLevel
return
PowerTools_RegWrite(SettingProp,Setting)
return Setting
} ; eofun
view raw Gist New.ahk hosted with ❤ by GitHub



This is a slightly different version than from my GitHub repo version - mainly because of authentification issue with VPN and proxy. 
I have also moved the PowerTools Lib dependencies for setting the registry setting inside the Gist so that it only requires the Jxon Lib.

I really like the way the script send the Http Request and parse the response using the Jxon functions. (Great and key learning in  Joe's sharing)

Run the script

I prefer to run the script by running it ;-) instead of having it waiting for a hotkey trigger.
You can run it by double-clicking on it (if AHK.exe is set as default app to open.)
I like to run it from my preferred Application Launcher Executor.
I have simply added a shortcut to this file and moved it to my indexed Apps Favorites location.

Screencast

See also

Create A Gist With Your Github Account (The Automator)

Creating a personal access token (GitHub Docs)