Capturing Rendered RTF Formatting

I am wanting to generate some RTF text following a template. Ideally, I would like to be able to create an Apple Notes item with that text.

I can do the following process manually:

  1. create the RTF as a text file,
  2. change the file suffix to .rtf,
  3. open it in TextEdit,
  4. copy the entire formatted text,
  5. open a new Notes item, and
  6. paste the text in.

It works. But it takes many steps and multiple apps. I cannot simply paste the RTF into the Note, it appears in the Note as RTF code. Similarly with TextEdit. I have to save the RTF code as a text file and then change the file type to RTF, then use TextEdit to render formatted text that I paste in elsewhere.

Ideally, I would:

  1. generate the RTF and put it into the clipboard,
  2. massage the clipboard from RTF code to formatted text, and
  3. paste the formatted text into a new Notes item.

But that middle step is a doozy.

Does anyone know of tools that do this kind of thing, rather than opening TextEdit to render the formatting?

UPDATE The above does not work. I misremembered what I created in what app and what I pasted from where to where. Pasting formatted text from Notes to TextEdit works and preserves font sizes. It does not work From TextEdit to Notes.

Styled text is always tricky, because it is handled inconsistently between applications (which is sad after 30 years of styled text!).

For example, if I copy this text from TextEdit with bold, italic, size, and fonts:

image

and paste it in to Notes, I get:

image

Basically, it ignores the font and size, but it does retain the bold and italic.

Is that sufficient for your needs?

As for RTF to rich text on the clipboard, you can probably do that just using the Read a File action.

So you could make your RTF raw text, save it using the Write to a File action, as a text file perhaps, and then rename it using the Move or Rename a File action and then read it to the clipboard and then Paste it in.

That's not what I'm seeing, which is why I've been considering that route. For me it keeps the sizes, mostly. Maybe it's because I'm still on Mojave.

UPDATE: Peter was right and I was wrong. Copy and Paste preserves font sizes when copying from Notes to TextEdit, but not the reverse.

Here's my RTF, without breaking it up into template pieces:

{\rtf1\ansi\ansicpg1252\cocoartf1671
{\fonttbl\f0 HelveticaNeue-CondensedBlack;\f1 HelveticaNeue;\f2 .SFNSSymbols-Regular;}
{\f1\fs144{\f0\b\fs12[{\fs800K}] }Keyboard Maestro{\f2\u8997\u8984}K}}

This is intended to be an identifying window for my KBM Desktop Workspace. It defines three fonts and then produces a line of text, defaulting to 72pt, starting with a 6pt "[", a 400pt "K", a 6pt "]" and a space, then in 72pt "Keyboard Maestro", then two Unicode characters, ⌥⌘, then a "K", still in 72pt. [Note that RTF dimensions are in half-points.]

When I save this RTF as text, change the file type to .rtf, and then open it in TextEdit, all the fonts and sizes work as I expected. When I ⌘A⌘C to copy the formatted text in TextEdit and then paste it into a new Notes item, all the fonts and sized transfer as I hoped. I have even made those 6pt "[" and "]" characters 1pt high and had it work. The "[" and "]" are invisible, but they appear in the window title,
"[K] Keyboard Maestro ⌥⌘K"

This is different from the results I get when I use @_jims 's technique of spelling out some HTML in AppleScript and inserting that HTML as the Note body. Using HTML, specifying 6pt and 1pt text gets converted to 12pt when it appears in Notes, but the changes to 72 and 400 pt work as expected.

Weirdly, when I make a Note using AppleScript HTML and it converts the font to 12pt, I cant make the characters smaller, but not the 12pt space; it reverts to 12pt when I change it. And when I paste in the sequence with the tiny fonts from TextEdit, Notes won't let me change those sizes; the "[" pasted in at 1pt size reverts to that if I try to change it to 12pt.

So there's a lot of weird, inexplicable, and invisible stuff going in Notes formatting the seems to depend on how the formatting was generated. As you said,

Thanks for that suggestion. I'll try it. It does required a temporary file, rather than doing it totally in the clipboard, but at least it avoids having to open TextEdit as a temporary window to do the rendering.

Try this thing I was playing with a while back:

rtfVarToClipboard

Takes the RTF "code" held in Local_boilerPlate and loads the System Clipboard with that converted to pasteable RTF data. It was working in Mail, haven't tried anywhere else -- and it hasn't been heavily tested so use with caution!

3 Likes

This can be done with AppleScript and HTML, although I can't guarantee anything past Mojave.

Quickly Make an Apple Note With Note on Top - #13 by ccstone

Thanks @Nige_S, I will try that approach too.

I am a little surprised at myself that while I was saying "It does required a temporary file" I did not at all think about a shell pipeline, especially given how many thousands of pipelines I've used and dozens of articles I've written about using them. I guess I've been so focussed on learning AppleScript to use within KBM that I've overlooked how well KBM interfaces with the Shell as well.

While looking up references for pbcopy, I came across this snippet:

textutil -stdin -format html -convert rtf -stdout | pbcopy

which is not exactly what I was looking for, but it suggests that textutil may be part of what I'm looking for too.

Thanks.

1 Like

Since you've got raw RTF boilerplate, textutil isn't needed. Great for converting from HTML etc though.

Sidenote for anyone who wants to play around with "raw" RTF: Make your pretty text in TextEdit, in "Rich Text Format" ("Format" menu), though that's normally the default and should anyway convert as soon as you style something. Save and close your document. Open your document again but this time select "Options" in the "Open" dialog and tick "Ignore Rich text Commands". Boom! Raw RTF, ready to use in a KM variable or wherever else you need it.

1 Like

Hi Nige,
Could you show a version of what's in that variable that works for you?

When I try it, whether I paste into a Mail message or into a Note, I get a distorted version of the original RTF code, where the code has been mangled by all the instances of a backslash being deleted, along with the following character. So \font becomes ont, etc., and worse.

So my code:

{\rtf1\ansi\ansicpg1252\cocoartf1671
{\fonttbl\f0 HelveticaNeue-CondensedBlack;\f1 HelveticaNeue;\f2 .SFNSSymbols-Regular;}
{\f1\fs144{\f0\b\fs12[{\fs800T}] }To Do Lists {\f2\u8997\u8984}T}}

becomes this on the clipboard:

{
tf1ansiansicpg1252\cocoartf1671
{onttbl0 HelveticaNeue-CondensedBlack;1 HelveticaNeue;2 .SFNSSymbols-Regular;}
{1s144{0s12[{s800T}] }To Do Lists {289978984}T}}

and it looks like this in Notes:
image

I tried taking my variable text and doubling every backslash. Running the KBM script to put that into the clipboard, when I pasted that into Notes, I got the text, but no formatting.
image

And of course it's the formatting I'm trying to capture and preserve.

I'll be able to bang on this more this evening, including trying Peter's temp file suggestion. That the pipeline worked for you is encouraging, but it isn't working for me, yet.

Thanks.

To save time, here's the whole thing -- boilerplate text followed by a bunch of search and replaces to create a new, rich text, Mail message. IIRC I was working with someone on a poor man's mail merge...

Test Mail Macro.kmmacros (10.9 KB)

Just tested, and still working fine for me. If it doesn't for you we'll probably have to start comparing environment variables.

Edit to add:

Not all, just the control characters -- \r for Return, \f for Formfeed...

Just checked after a flash of inspiration -- my "Set Variable" action is set to "Process Nothing", and I'll bet that solves it for you too. IE it's KM processing tokens that is causing the problem, not the shell script.

That was indeed one of the issues. Making that change works the same as doubling all the backslashes as I showed above; it produces the text with bold and regular, but no size changes.

So I went back a step and I discovered that I overlooked an assumption that I'll have to track down. It's straightforward, it's just going to take some experimenting.

I created the original text, with font and size changes in TextEdit. In that original TE construction, I copied the rendered, formatted text and pasted it into a New Note. Everything transferred as expected.

Because I wanted to use this in a KBM macro, probably in AppleScript or something (now a Shell script), I began to look for how I could simplify the RTF. I started stripping down the RTF in VIM and then looking at the resulting file in TE. I came up with the minimum RTF file that would display in TE as I wanted it to. That's what I've been trying to create the New Note from.

But I totally assumed that if it didn't look any different in TextEdit, then it would copy and paste into Notes the same way. It doesn't. While the stripped down RTF looks the same in TE, when I copy and paste the rendered, formatted version into Notes, all I get is the text with some bold, just like I've been getting when I put that same RTF into Notes using your Shell script.

So the difference is that the RTF created by TextEdit will render as something that I can copy and paste into Notes, but when I strip down the RTF, it still renders in TE looking the same, but it doesn't copy and paste into Notes the same at all.

I assumed that RTF code for things like page margins wouldn't matter when it was pasted into Notes, and maybe those particular elements don't, but something that I deleted from the RTF, which made no difference in TextEdit, made a lot of difference by the time it got to Notes.

Thanks for the copy of your Test Mail Macro, but I don't think I need it right now. I've got another rabbit hole to explore. I think it would come out the same whether I'm using your cat <<EOF | pbcopy script or Peter's temp file version because the embedded data that I want isn't there. At this point I have to figure out what RTF will paste into Notes as I want it to.

1 Like

I've done some exhaustive back and forth testing tonight and realized that my description above is wrong, so my real questions are different from what I asked.

I didn't actually do it the way I described, I did it the other way around. I didn't generate the design in TextEdit and paste it into Notes, I generated it in Notes and pasted it into TextEdit.

(Yes, another reminder of the importance of making "Lab Notes" while I am experimenting, because 3 days later, I've forgotten exactly what I did and then make erroneous assumptions of what worked and what didn't.)

I am now convinced my original workflow was this:

  • I created my multi-font, multi-size design initially in Notes.
  • I wanted to be able to generate new notes to that template using KBM.
  • I copied and pasted the Notes formatted text into TextEdit in hopes of being able to create an RTF template.
  • I save the TextEdit as RTF.
  • I reviewed the RTF and verified that all the font and size changes were included.
  • I discovered JimS's "MACRO: New Apple Note" using HTML instead of RTF.
  • I tried to create the same format using HTML to make the Note by modifying @_jims' "MACRO: New Apple Note". But I couldn't get HTML to generate smaller than 12pt type in Notes nor could I access the condensed font that I wanted.
  • I tried to figure out how to use that RTF to make new Notes with the same format.
  • I asked for help here and forgot which program I had originally used to generate the design. It was Notes, not TextEdit. I used TextEdit in hopes of being able to do in RTF what I couldn't do in HTML. But neither works for what I'm trying to do, even though both work for generic text.

... So I'm looking at other methods and have more questions.

First up: Is there a way to save what's on the clipboard when I copy it from Notes that keeps all the formatting intact? Is it binary or ASCII? Will KBM named clipboards work to hold snippets to assemble the formatting?

Second: Can I write a KBM script that generates the desired Notes format from scratch each time, successfully and consistently?

To keep this thread from misdirecting someone who comes across it in the future, I'll go back and make updates to my descriptions above, so that I'm not claiming something works that doesn't.