How to generate an inline annotation/comment as raw RTF to paste into Scrivener?

This is rather technical question for Keith. Lets say I have an Applescript and I want to use it to create an RTF link wrapped in an inline annotation I can paste from the system clipboard into Scrivener’s editor. The link itself is easy using textutil (with a caveat of the font styling):

echo " <a href=\"http://http://literatureandlatte.com/\">L&L</a> " | textutil -format html -convert rtf -stdin -stdout | pbcopy -Prefer rtf

The question is what is the RTF for an inline annotation or comment assuming it is something custom (doubt textutil could generate it)?

Assuming it is custom and based on a test scrivener doc (binder saved doc with a link in an inline annotation) I created a “minimal” RTF fragment:

{\rtf1\ansi\ansicpg1252\cocoartf1561\cocoasubrtf200
\f0\fs28 \cf0 L: 
\f1\fs24 \{\\Scrv_annot \\color=\{\\R=0.619608\\G=0.043137\\B=0.011765\} \\text=
\f0\fs28  {\field{\*\fldinst{HYPERLINK "http://literatureandlatte.com/"}}{\fldrslt L&L}} 
\f1\fs24 \\end_Scrv_annot\}}

Copies into the clipboard as RTF, but when pasted to Scrivener the escaping yields no working annotation (the link works however):

L: {\Scrv_annot \color={\R=0.619608\G=0.043137\B=0.011765} \text= L&L \end_Scrv_annot}

OK so lets try to see what Scrivener itself copies to the clipboard (public.rtf, found using Clipboard Viewer):

{\rtf1\ansi\ansicpg1252\cocoartf1561\cocoasubrtf200
\stshfdbch0{\fonttbl\f0\fnil\fcharset0 GraublauSlab-Regular;}
{\colortbl;\red255\green255\blue255;}
{\stylesheet {\s0 Normal;}}
{\*\expandedcolortbl;;}
\deftab720
\paperw12240\paperh15840\margl1800\margr1800\margt1440\margb1440\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\tx9360\tx10080\tx10800\tx11520\pardeftab720\sl336\slmult1\pardirnatural\qj\partightenfactor0

\f0\fs28 \cf0 L: {\chatn{\*\annotation\pard\plain\f0\fs28\b0\i0\ul0 {\field{\*\fldinst{HYPERLINK "http://literatureandlatte.com/"}}{\fldrslt L&L}}}}\f0\fs28\b0 \i0 \ul0  }

To generate this minimal fragment:

{\rtf1\ansi\ansicpg1252\cocoartf1561\cocoasubrtf200
\f0\fs28 \cf0 L: {\chatn{\*\annotation\pard\plain\f0\fs28\b0\i0\ul0 {\field{\*\fldinst{HYPERLINK "http://literatureandlatte.com/"}}{\fldrslt L&L}}}}\f0\fs28\b0\i0\ul0 }

This is different to what is saved. If I then use pbpaste I get L: wrapped in an inspector comment containing the L&L link. This would almost work. Obviously Scrivener is doing some other magic to be able to copy and paste inline annotations and deal with the font styling (I would like the pasted text to match the style).

So what I’m asking:

What is the minimal RTF plain text fragment that would trigger an inline annotation containing a link, and not change the font styling? Perhaps Applescript has some better mechanism to paste RTF to the clipboard better, but so far pbpaste -Prefer rtf does most of what is needed. I suspect you do not use public.rtf but a custom clipboard field for this?

The minimal fragment you listed first will create the RTF necessary for Scrivener to show an annotation. However, you would have to close and reopen the project for the annotation to look like an annotation - on first paste it will just show the codes. Scrivener does not run conversion for its internal codes after a paste, as it does not expect them during paste since they are intended for internal use only. Such codes are only converted when loading RTF files from the project.

A better way would just be to use regular RTF comments and then tell Scrivener to import RTF comments as inline annotations rather than as Inspector comments via Scrivener’s Preferences. If you only want to set that preference temporarily as part of the script, you can set “KBRTFImportCommentsInline” to YES on Scrivener’s com.literatureandlatte.scrivener3 Preferences property list and then set it back to NO (or its original value) afterwards. (Although I think you have to do a bit more fiddling around these days to get a Preference to take straight away when setting it via the command line).

All the best,
Keith

Hi Keith thanks for the details.

So I now have a bash script that generates the following RTF:

{\rtf1\ansi\ansicpg1252\cocoartf1561\cocoasubrtf200 {\fonttbl\f0\fswiss;} \f0\fs20 \cf0 [@zipser2017]{\chatn{\*\annotation\pard\plain\f0\fs20\b0\i0\ul0 {\field{\*\fldinst{HYPERLINK "bookends://sonnysoftware.com/27008"}}{\fldrslt \u00A7?}}}}\f0\fs20\b0\i0\ul0 }

The problem I’m having is being able to include unicode, I’m using iconv to convert say § into \u00A7? which is what I understand I’m supposed to do, but Scrivener is not understanding it:

[QUESTION 1]: Is Scrivener using the older escaping mechanism which uses some windows codepages? Is there an easy way to escape unicode to this format?


Regarding the KBRTFImportCommentsInline actually this seems to work switching the pref ON/OFF. The problem is that my script is split across two seperate components and I cannot reset after I use the second component to paste unless I make it exclusive to Scrivener which I don’t want to do, so I think this has to be an option the user explicitly enables. Still great that with the sameRTF we can get different effects…

[QUESTION 2]:Another alternative is if Scrivener could be modified to parse your custom annotation format from a plain.rtf paste (i.e. relax the assumption a little), is there a problem with doing this? This is very minor, but the general the idea is if we can’t have an API, at least we can do more when we script tools to work with Scrivener’s features…


[QUESTION 3 [most important]]: Is there a way to make the RTF fragment not change the font / font size on paste?

  1. Scrivener uses Apple’s TextKit to import and export RTF, so it’s Apple’s code for the most part (and for stuff like this). The best thing to do is try creating the RTF you want in TextEdit and examining it in a plain text editor. (Scrivener contains lots of code that extends Apple’s RTF import/export - to add support for headers, footers, footnotes, comments, and more - but the text-level code is Apple’s.)

  2. There’s probably no problem with doing this, but obviously it’s not a high priority as it’s not using Scrivener in the expected way. :wink:

  3. No. That’s just the way RTF works.

All the best,
Keith

Thanks, it seems Apple does not support RTF V1.5 (released in 1997!) \u#### unicode formatting and you must use the archaic '## codepage value or for values >32768 generate a dual entry UTF-16 limited format, there isn’t an easy method using iconv to generate this for all possible characters.

Improving communication with other apps does have some merit, though I completely agree the priority is probably quite low.

OK, what I’d like to achieve is being able to take a reference in Bookends, and get the temp citation with a comment containing the backlink to Bookends, but the rest of the text to follow the current editor formatting. I suspect there is no formal way to be able to work out what the current Scrivener font/font size is? Another alternative is RTF supports the \stylesheet{} property but I don’t think you use this for your style system right?

Anyway, here is the script so far, with the caveat of losing your current styling, it succesfully copies an RTF backlink into Scrivener:

github.com/iandol/bookends-tool … itation.sh