Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Does anyone know if this can be done in other macOS automation apps too? (like Keyboard Maestro, Hammerspoon, Raycast, Alfred, ...)


Of course. I use this in Hammerspoon. The API call is simply in Lua.


How did you get the selected text in the focused app and replace it with the GPT response? I worked on it for hours and couldn't do it. I'd appreciate it if you could share that config.


If you want to go full circle you can send the ⌘-C and ⌘-V key events before/after they query. This will just send selected text and replace this with answer. I prefer not to do that, since queries are quite slow and I rarely block waiting for round trip. I also use vim and vim mode in lot's of apps, so replace works differently there.

I prefer to use clipboard as the exchange place. I select text, copy it, then query the service. Once the query completes I can see the answer in popup and can paste it to my current editing place.

This is the code:

  local hyper = {"cmd", "alt", "ctrl"}

  hs.hotkey.bind(hyper, "Y", function()
    local url = "https://api.openai.com/v1/completions"
    local api_key = "..."

    local headers = {
      authorization="Bearer " .. api_key,
      ["content-type"]="application/json",
      accept="application/json"
    }

    -- hs.eventtap.keyStroke({"cmd"}, "c")

    local message = hs.pasteboard.readString()

    local data = {
      prompt=message,
      model="text-davinci-003",
      max_tokens=32,
      temperature=0
    }

    hs.notify.new({title="OpenAI query", informativeText=message}):send()

    hs.http.asyncPost(url, hs.json.encode(data), headers, function(status, body, headers)
      local response = hs.json.decode(body)
      local answer = response["choices"][1]["text"]

      print(body)

      hs.notify.new({title="OpenAI response", informativeText=answer}):send()
      hs.pasteboard.writeObjects(answer)
      -- hs.eventtap.keyStroke({"cmd"}, "v")
    end)
  end)


Thanks a ton! I'm going to test that. You have a good point about Vim. I tried the OP's method in Neovim and it didn't work exactly for the reason you mentioned. Clipboard as the exchange place sounds more reasonable.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: