Hammerspoon / hammerspoon

Staggeringly powerful macOS desktop automation with Lua
http://www.hammerspoon.org
MIT License
12.15k stars 587 forks source link

Feature request: hs.styledtext.new() support for link attribute #3708

Open von opened 1 month ago

von commented 1 month ago

Short version: I'd like to be able to use hs.styledtext.new() to create some styled text which is a hyerperlink but that function doesn't seem to respect the link attribute.

Details:

If I copy some hyperlinked text into the pastebuffer outside of Hammerspoon and then examine it, I see the link attribute in play:

<copy hyperlink into pastebuffer>

> st = hs.pasteboard.readStyledText()

> stt = st:asTable()

> i(stt)
{ "pertinax aliquando an", {
    attributes = {
      color = {
        __luaSkinType = "NSColor",
        alpha = 1.0,
        blue = 0.75433194637299,
        green = 0.23714117705822,
        red = 0.061691254377365
      },
      <snip>
      link = {
        __luaSkinType = "NSURL",
        url = "https://photos.google.com/"
      },
      <snip> 
      strokeWidth = 0.0,
      underlineStyle = 1.0
    },
    ends = 21,
    starts = 1
  },
  __luaSkinType = "NSAttributedString"
}

But if I try to use hs.styledtext.new() to create a hyperlink, the link attribute seems to be ignored:

> st = hs.styledtext.new("test string", { color = { __luaSkinType = "NSColor", alpha = 1.0, blue = 0.75433194637299, green = 0.23714117705822, red = 0.061691254377365 }, link = { __luaSkinType = "NSURL", url = "https://photos.google.com/" } })

> i(st:asTable())
{ "test string", {
    attributes = {
      color = {
        __luaSkinType = "NSColor",
        alpha = 1.0,
        blue = 0.75433194637299,
        green = 0.23714117705822,
        red = 0.061691254377365
      }
    },
    ends = 11,
    starts = 1
  },
  __luaSkinType = "NSAttributedString"
}

Since the hs.styledtext documentation doesn't mention link being supported, I assume the above behavior is not a bug and hence this is a new feature request.

von commented 3 weeks ago

I found the following work around: create the hyperlinked text using html and then covert it to styledtext using getStyledTextFromData().

stext = hs.styledtext.getStyledTextFromData('<a href="https:www.mit.edu">Test text</a>', "html")
hs.pasteboard.writeObjects(stext)

I also found some ways to do it using system calls as well (e.g.).