I’m a bit of an AppleScript novice, I’ve used it irregularly but am trying to get better with it so please pardon some dumb questions.
I’m attempting to find a way to construct an RTF link and place it on the clipboard without including an styling (font, color, decoration) so that it matches whatever style is set in the field I paste it in. Essentially I want this:
{\field{\*\fldinst HYPERLINK "x-devonthink-item://53151EC1-8A15-4109-AFE9-1B4EA8C6DE2B?time=198"}{\fldrslt 00:03:18}}
I’m not sure if that’s even valid RTF on its own but I’m not seeing a linter/validator and RTF resources seem to be all over the place. I can save that into an .rtf file by itself and open it fine in TextEdit — where it then styles it with its default formatting — exactly what I want to happen on paste everywhere.
Approach One: Use textutil
I’d love to be able to do it using something like @JMichaelTX’s method to combine a Markdown link with rtf and there are two techniques for generating the RTF in that thread.
His original way, essentially:
set theLinkText to "00:03:18"
set theURL to "x-devonthink-item://53151EC1-8A15-4109-AFE9-1B4EA8C6DE2B?time=198"
set theMarkdownLink to ("[" & theLinkText & "](" & theURL & ")") as string
set theHTMLLink to ("<a href=\"" & theURL & "\">" & theLinkText & "</a>") as string
do shell script "export LANG=\"en_US.UTF-8\" && echo " & quoted form of theHTMLLink & " | textutil -format html -convert rtf -inputencoding UTF-8 -stdin -stdout | pbcopy -Prefer rtf"
set theRichTextLink to the clipboard as «class RTF »
set the clipboard to {Unicode text:theMarkdownLink, «class RTF »:theRichTextLink}
However, I don’t see a way to get textutil
to give me a rich text without styling information.
Shane’s code works but also adds styling and I frankly don’t understand what it’s doing with Foundation and AppKit yet.
Approach Two: Try to Set RTF String to «class RTF »
I was hoping it would be fairly simple to do something like:
set theRTFSource to "{\\field{\\*\\fldinst HYPERLINK \"x-devonthink-item://53151EC1-8A15-4109-AFE9-1B4EA8C6DE2B?time=198\"}{\\fldrslt 00:03:18}}"
set theRTFLink to «class RTF » of theRTFSource
But that unfortunately throws -1728 (Can’t get «class RTF » of "[…]".
)
Picking apart Jim’s script a bit it looks like the string needs to be encoded as «data RTF ... »
but I’m not sure how to approach that.
Approach Three: Convert RTF Source with NSString
I’ve also tried Mark’s script in RTF ⇄ HTML without using the clipboard? that does allow me to convert RTF source to Rich Text but it also adds NSFont
information.
set theRTFSource to "{\\field{\\*\\fldinst HYPERLINK \"x-devonthink-item://53151EC1-8A15-4109-AFE9-1B4EA8C6DE2B?time=198\"}{\\fldrslt 00:03:18}}"
set theString to current application's NSString's stringWithString:theRTFSource
set theData to theString's dataUsingEncoding:NSUTF8StringEncoding
set theAttributedString to current application's NSAttributedString's alloc's initWithRTF:theData documentAttributes:0
(NSConcreteAttributedString) 00:03:18{
NSFont = "\"Helvetica 12.00 pt. P [] (0x7fbd07e93590) fobj=0x7fbcfe06d600, spc=3.33\"";
NSLink = "x-devonthink-item://53151EC1-8A15-4109-AFE9-1B4EA8C6DE2B?time=198";
}
Questions
Obviously I know just enough to get myself into trouble here and not enough to figure out a correct approach. Any help would be greatly appreciated.
- Is this even possible or am I fundamentally misunderstanding how RTF works?
- I can keep trying to figure this out but it would be great to know which track to go down here, will one of these potentially work or should I shift direction to something else?
- If I pick up Shane’s Everyday AppleScriptObjC will that give me the foundation (ahem) to figure how Shane and Mark’s code is working or is there a better resource to go to first?
Edited to add proper links since I can’t yet have a post with them in it.