使用 lua Hammerspoon 将文本插入表格格式

我正在尝试使用 lua 在 hammerspoon 中创建一个脚本。按下“x”将为用户提供选项,以将他们想要粘贴到网页文本字段中的模板粘贴上去。

不幸的是,我无法弄清如何将该文本格式化为表格(带有行和列)?

我尝试将表格从 Google 表格(Excel)粘贴到 lua 代码中,但仍无法在文本字段中呈现为表格。

-- 聚焦最后使用的窗口。

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"] = "选项 1",
         ["subText"] = [[ 文本 1 文本 2

                          文本 3 文本 4]]

      },
      {
         ["text"] = "选项 2",
         ["subText"] = [[ 文本 1 文本 2

                          文本 3 文本 4]]

      },

})
hs.hotkey.bind({"X"}, "X", function() chooser:show() end)
点赞
用户10126088
用户10126088

将下面翻译成中文并且保留原本的 markdown 格式,插入新行使用\n,插入水平制表符使用\t。详见链接

> print('1\t2\n3\t4')
1       2
3       4
2019-07-04 09:30:47