EN / RU
← Back to essays
· Essay · 1 min

Automation with HammerSpoon and ChatGPT

The real discovery from the MIT course "The Missing Semester of Your CS Education" that I wrote about is the HammerSpoon tool.

The main idea is that it is an extension over the operating system that allows you to write small scripts and automate various actions. There are many ready-made plugins (for example, resizing and moving application windows across screens with one click).

My favorite script that I created is 22 lines long and, with the combination Cmd+Shift+Option+G, takes the copied text, sends it to ChatGPT with the request "fix the grammar," and replaces it in the clipboard, leaving it ready to paste back. This saves an unreal amount of time every day and eliminates the need to copy and paste text back and forth from ChatGPT.

```if OPENAI == "" then return end

hs.hotkey.bind({"cmd","shift","alt"},"G",function()
local txt = hs.pasteboard.getContents(); if not txt or txt=="" then return end
local hold = hs.alert.show("⏳ Fixing…",9999)
local body = hs.json.encode{
model="gpt-4o-mini",
messages={
{role="system",content="Fix grammar; return only text."},
{role="user",content=txt}
}
}
hs.http.asyncPost("https://api.openai.com/v1/chat/completions",body,
{[

Automation with HammerSpoon and ChatGPT — illustration