在 Hammerspoon 中从 hs.chooser 粘贴文本

我正在尝试创建一个快捷方式,通过使用 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"] = "测试",
            ["subText"] = "测试我的文本"
        }
    }
    chooser:choices(choices)
end)

chooser:searchSubText(true)

chooser:show()
   end)
点赞
用户8937847
用户8937847

我想出了答案。

-- 焦点放到上一个被使用的窗口。

local function focusLastFocused()
    local wf = hs.window.filter
    local lastFocused = wf.defaultCurrentSpace:getWindows(wf.sortByFocusedLast)
    if #lastFocused > 0 then lastFocused[1]:focus() end
end
-- 在选择后,将文本复制并粘贴到焦点应用程序中。

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"] = "浏览器\n",
         ["subText"] = "我使用这些浏览器",
      },
      {
         ["text"] = "设备\n",
         ["subText"] = "我使用这些设备",
      },

})
hs.hotkey.bind({"E"}, "E", function() chooser:show() end)
2019-06-25 11:14:22