The easiest method would be to just use the answer described here to fetch the RTF data in the pasteboard and pipe the data to the already available
textutil
command to convert it to plain text to stdout:
osascript -e 'the clipboard as «class RTF »' | \
perl -ne 'print chr foreach unpack("C*",pack("H*",substr($_,11,-3)))' | \
textutil -stdin -stdout -convert txt
We can then in the Hammerspoon environment use hs.execute
to run the shell command and return the converted value, so in your Lua code it's as simple as:
local text = hs.execute([[
osascript -e 'the clipboard as «class RTF »' | \
perl -ne 'print chr foreach unpack("C*",pack("H*",substr($_,11,-3)))' | \
textutil -stdin -stdout -convert txt
]])
FYI the Hammerspoon API does allow you to retrive RTF data from the pasteboard using hs.pasteboard.readDataForUTI
using the "public.rtf"
UTI, so technically you could do all this in Lua, but you would have to manually convert the RTF data yourself.