将hs.chooser中的文本粘贴到锤匙中

时间:2019-06-25 09:59:02

标签: lua hammerspoon

我正在尝试创建一个快捷方式,在其中使用hs.chooser存储一组文本模板。而且,用户可以通过单击hs.chooser的下拉菜单来粘贴它。

我使用以下代码显示我的模板,但不粘贴文本。

有人可以指出我在做什么吗?

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)

1 个答案:

答案 0 :(得分:0)

我想出了答案

-- 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)