moh17
moh17

Reputation: 243

Inserting text in table format using lua Hammerspoon

I'm trying to create a script using lua in hammpersppon. Where pressing "x" will give the user option to paste the kind of template that they want to paste into a text field in web.

Unfortunately, I am not able to figure out how to format that text as tables ( with rows and columns)?

I tried pasting the table from google sheet (excel) into lua code but still, it doesn't render it as a table in the text field.

-- 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"] = "Option 1",
         ["subText"] = [[ Text 1 Text 2

                          Text 3 Text 4]]

      },
      {
         ["text"] = "Option 2",
         ["subText"] = [[ Text 1 Text 2

                          Text 3 Text 4]]

      },

})
hs.hotkey.bind({"X"}, "X", function() chooser:show() end)

Upvotes: 2

Views: 701

Answers (1)

csaar
csaar

Reputation: 560

Insert Newlines as \n and horizontal tabs as \t. See also link.

> print('1\t2\n3\t4')
1       2
3       4

Upvotes: 0

Related Questions