3

I am trying to create a shortcut where I store a set of text templates by using hs.chooser. And, the user can paste this by clicking on the drop down from hs.chooser.

I use the below code which displays my template but doesn't paste the text.

Can someone point me what I am doing wrong?

hs.hotkey.bind({"Q"}, "W", function()
local current = hs.application.frontmostApplication()

local chooser = hs.chooser.new(function(choice)
    if not choice then focusLastFocused(); return end
    hs.pasteboard.setContents(choice["chars"])
    focusLastFocused()
    hs.eventtap.keyStrokes(hs.pasteboard.getContents())
end)

chooser:queryChangedCallback(function(string)
    local choices = {
        {
            ["text"] = "Testing",
            ["subText"] = "Testing my text"
        }
    }
    chooser:choices(choices)
end)

chooser:searchSubText(true)

chooser:show()
   end)
moh17
  • 223
  • 1
  • 6
  • 25

1 Answers1

4

I figured out the answer

-- Focus the last used window.

local function focusLastFocused()
    local wf = hs.window.filter
    local lastFocused = wf.defaultCurrentSpace:getWindows(wf.sortByFocusedLast)
    if #lastFocused > 0 then lastFocused[1]:focus() end
end
-- On selection, copy the text and type it into the focused application.

local chooser = hs.chooser.new(function(choice)
    if not choice then focusLastFocused(); return end
    hs.pasteboard.setContents(choice["subText"])
        focusLastFocused()
    hs.eventtap.keyStrokes(hs.pasteboard.getContents())
end)

chooser:choices({
      {
         ["text"] = "Browser\n",
         ["subText"] = "I used these browsers",
      },
      {
         ["text"] = "Device\n",
         ["subText"] = "I used these devices",
      },

})
hs.hotkey.bind({"E"}, "E", function() chooser:show() end)
moh17
  • 223
  • 1
  • 6
  • 25