Inserting a PDF into an RTF - scriptable?

I want to be able to insert/attach a PDF (displayed inline) into an RTF file, so I can make a few notes about the PDF that are permanently attached to it. I know I can drag and drop a PDF into an RTF file, but I want to script the process (one click to create a new RTF file, name it the same as the PDF, insert the PDF into the RTF, and delete the original PDF).

I’ve deduced from the Scripting Dictionary and the _Remove Attachments script that an attachment is the “text of an attribute run” (which can be removed by setting the text to “”), but I don’t see how to add such an attachment or what exactly that text consists of. Can anyone explain how I might do this? I can write the rest of the script, just need to understand what the syntax would be for this step.

Please note:

  1. I do not want to convert the PDF into an RTF – I want it to remain a PDF (which I can open independently).
  2. I don’t want to use an annotation file – this is for minor notes that simply don’t warrant the creation of a separate annotation file, but I want them visible in the document window with the embedded PDF.
  3. Finder comments won’t work because the notes may include links to other documents (not to mention that Finder comments are not visible in the document window).

What else does the attachment property say in the scripting dictionary? (And yes, I have an answer to your question, but the direction to go is right in the description of the property.)


PS: @cgrunenberg, the file name for attachments in an RTFD file always returns missing value

tell application id "DNtp"
	if (not (exists (content record))) or (type of (content record) is not in {rtf, rtfd}) then return
	tell text of (content record)
		file name of (attachments)
        --> {missing value, missing value, missing value}
	end tell
end tell

Well, I suppose you must be referring either to “This class is used mainly for make commands” or to " file name (text) : The path to the file for the attachment" – there isn’t a whole lot else there – but I confess I still don’t understand. Can you “set the file name of the attachment of theRecord to thePdfFileName” or something like that? (And why does the file name property return {missing value}?)

“This class is used mainly for make commands”

Correct (and emphasis mine). There are plenty of examples of using the make command with rich text. Here is a simple snippet of the syntax with a little error-trapping up front…

tell application id "DNtp"
	if (not (exists (content record))) or (type of (content record) is not in {rtf, rtfd}) then return
	tell text of (content record)
		make attachment at end with properties {file name:"/Users/apollox/Downloads/happy lamb.jpg"}
	end tell
end tell

Look at the Contained by section in the description and you can see they can simply be contained by the text of a rich text document.

(And why does the file name property return {missing value}?)

I don’t know, hence my question to our CTO in my previous response.

The handling of attachments by the text suite of macOS is very limited (and buggy) and is basically only useful to make new attachments.

Thanks! That was exactly what I needed and I now have a working script.

Just one question: I’d like to have three blank lines before the inline attachment for ease of inserting my comments, so I created the rtfd record including the property text:linefeed & linefeed & linefeed, followed by the tell text … make attachment at end code. However, the “at end” doesn’t seem to work; I get the inline pdf, followed by the three linefeeds. Not a big deal, but I wonder if there’s any other way to position the attachment.

tell application id "DNtp"
	set newDoc to create record with {name:"New RTFD", type:rtfd, text:""} in current group
	tell text of newDoc
		make paragraph at beginning with data (return & return & return)
		make attachment with properties {file name:"/Users/apollox/Downloads/column sets.jpg"}
	end tell
end tell

And here is an example with some of the composition syntax with paragraphs…

tell application id "DNtp"
	set newDoc to create record with {name:"New RTFD", type:rtfd, text:""} in current group
	tell text of newDoc
		make paragraph at beginning with data (return & return & return)
		make paragraph at after paragraph 3 with data (return & "This is an embedded attachment." & return)
		make attachment with properties {file name:"/Users/apollox/Downloads/column sets.jpg"}
		make paragraph at end with data (return & "This paragraph is at the end of the document.")
		make paragraph at before paragraph -1 with data (return & "This paragraph was inserted before the last paragraph." & return & return)
	end tell
end tell

Thanks again. Sometimes I just need a couple of examples to get me on the right track. You’ve been a big help. :grinning:

You’re welcome. Glad to be of service.