Convert RTFs to PDF

Conversion of RTF to PDF is scriptable with:

/System/Library/Printers/Libraries/convert

A draft script to convert selected RTFs (or RTFs in selected groups and their descendants):

-- ILLUSTRATIVE DRAFT: Convert selected RTFs (or RTFs in selected groups and their descendants) to PDF
-- Ver 0.7 -- third draft of an attempt to add EML files to the list of convertible types
-- 		-- Adds the url of the piece of mail to the PDF record

property pstrConvert : "/System/Library/Printers/Libraries/convert -f "
property pstrDesktop : POSIX path of (path to desktop)

on run
	tell application id "DNtp"
		repeat with oSeln in (selection as list)
			set strKind to kind of oSeln
			if kind of oSeln is in {"RTF", "RTFD", "eml", "Group"} then my RTForEML2PDF(oSeln)
		end repeat
	end tell
end run

on RTForEML2PDF(oRec)
	tell application id "DNtp"
		set strKind to kind of oRec
		
		set blnInterimRTFD to false
		if strKind starts with "eml" then
			set strURL to URL of oRec
			set oRec to convert record oRec to rich
			set strKind to kind of oRec
			set blnInterimRTFD to true
		end if
		
		if strKind = "RTF" then
			set {strName, strRTFPath, strFile} to {name, path, filename} of oRec
			if strRTFPath ≠ "" then
				-- Clean the file name
				set strCleanFile to do shell script ("printf '%q' " & quoted form of strFile)
				
				-- CONVERT FROM RTF TO PDF
				set strPDFPath to pstrDesktop & strCleanFile & ".pdf"
				do shell script pstrConvert & quoted form of strRTFPath & " -o " & quoted form of strPDFPath
				set oPDF to import strPDFPath to (first parent of oRec)
				set name of oPDF to strName & ".pdf"
				
				-- DELETE THE TEMPORARY COPY FROM THE DESKTOP
				do shell script "rm " & quoted form of strPDFPath
			end if
		else if strKind = "RTFD" then
			set {strName, strRTFPath, strFile} to {name, path, filename} of oRec
			if strRTFPath ≠ "" then
				-- Clean the file name
				set strCleanFile to do shell script ("printf '%q' " & quoted form of strFile)
				
				-- CONVERT FROM RTF TO PDF
				set strPDFPath to pstrDesktop & strCleanFile & ".pdf"
				do shell script pstrConvert & quoted form of (strRTFPath & "/TXT.rtf") & " -o " & quoted form of strPDFPath
				set oPDF to import strPDFPath to (first parent of oRec)
				
				
				-- IF THE RTFD RECORD IS AN INTERIM RESULT OF AN EML CONVERSION ...
				if blnInterimRTFD then
					set name of oPDF to (texts 1 thru -6 of strName) & ".pdf" -- drop the appended " text"
					set URL of oPDF to strURL
					delete record oRec
				else
					set name of oPDF to strName & ".pdf"
				end if
				
				-- DELETE THE TEMPORARY COPY FROM THE DESKTOP
				do shell script "rm " & quoted form of strPDFPath
			end if
		else if strKind = "Group" then
			repeat with oChild in children of oRec
				if kind of oChild is in {"RTF", "RTFD", "eml", "Group"} then my RTForEML2PDF(oChild)
			end repeat
		end if
	end tell
end RTForEML2PDF

on EML2RTF(oRec)
	
end EML2RTF



Robin:

This is a very useful script - something I needed today for a large block of RTF summary files exported from DA to DTPO (it would be nice if DA made PDF summaries - but that’s another story).

Documents with hyphens or quotes in the document titles (names) do not seem to finish the process. At least, over here nothing apparent shows up in the DTPO group, but the PDF is on the desktop. Renaming the document fixes this.

Over here, it’s interesting that the conversion changes the color of embedded links to green.

Thank you for the feedback - that’s very helpful.

I’ve added a line to ‘clean’ the filename to ver 0.2 (amended above).

Does that seem to work ?

It sure does. Thank you again, this was exactly what I needed at this time. :stuck_out_tongue:

Thanks so much for this script. It’s helped me out immensely.

Is it possible to update with script so it can convert email files?

I’ve tried to manually load your script and change all the “RTF” instances with EML, but that doesn’t work.

If not, any suggestions? I have a ton of email files that I’ve imported into DevonThink Pro, that I would like to convert into pdfs.

Thanks again for the script. I had a lot of RTF files also. I’m playing around with Script Debugger and trying to learn a little more with apple scripts, but it’s pretty confusing for me right now…

@TylerGred

I tried eml’s too - what get’s printed is the raw source of the message, which can be pretty ugly for many messages.

No time, unfortunately to experiment with that now, but FWIW the route I would take might begin with:

if strKind = "eml" then
    set oRec to convert record oRec to rich
end if

Which would, I think, often generate an RTFD bundle rather than an RTF file.

You should then be able to use /System/Library/Printers/Libraries/convert -f to generate a .pdf from any resulting .rtf - possibly an .rtf file inside the RTFD bundle.

(and optionally delete the intermediate rtf(d) record afterwards)

An exercise for someone else ?

Yes, that creates an RTFD

Working on this - haven’t yet found an easy programmatic way to get the RTF file out of the RTFD bundle.

Took a quick look - here is a first experiment in adding .eml (and, in passing, .rtfd) to the list of convertible types.

-- ILLUSTRATIVE DRAFT: Convert selected RTFs (or RTFs in selected groups and their descendants) to PDF
-- Ver 0.7 -- third draft of an attempt to add EML files to the list of convertible types
-- 		-- Adds the url of the piece of mail to the PDF record

property pstrConvert : "/System/Library/Printers/Libraries/convert -f "
property pstrDesktop : POSIX path of (path to desktop)

on run
	tell application id "DNtp"
		repeat with oSeln in (selection as list)
			set strKind to kind of oSeln
			if kind of oSeln is in {"RTF", "RTFD", "eml", "Group"} then my RTForEML2PDF(oSeln)
		end repeat
	end tell
end run

on RTForEML2PDF(oRec)
	tell application id "DNtp"
		set strKind to kind of oRec
		
		-- Make a temporary RTFD version of any .eml record
		set blnInterimRTFD to false
		if strKind starts with "eml" then
			set strURL to URL of oRec
			set oRec to convert record oRec to rich
			set strKind to kind of oRec
			set blnInterimRTFD to true
		end if
		
		if strKind = "RTF" then
			set {strName, strRTFPath, strFile} to {name, path, filename} of oRec
			if strRTFPath ≠ "" then
				-- Clean the file name
				set strCleanFile to do shell script ("printf '%q' " & quoted form of strFile)
				
				-- CONVERT FROM RTF TO PDF
				set strPDFPath to pstrDesktop & strCleanFile & ".pdf"
				do shell script pstrConvert & quoted form of strRTFPath & " -o " & quoted form of strPDFPath
				set oPDF to import strPDFPath to (first parent of oRec)
				set name of oPDF to strName & ".pdf"
				
				-- DELETE THE TEMPORARY COPY FROM THE DESKTOP
				do shell script "rm " & quoted form of strPDFPath
			end if
		else if strKind = "RTFD" then
			set {strName, strRTFPath, strFile} to {name, path, filename} of oRec
			if strRTFPath ≠ "" then
				-- Clean the file name
				set strCleanFile to do shell script ("printf '%q' " & quoted form of strFile)
				
				-- CONVERT FROM RTF TO PDF
				set strPDFPath to pstrDesktop & strCleanFile & ".pdf"
				do shell script pstrConvert & quoted form of (strRTFPath & "/TXT.rtf") & " -o " & quoted form of strPDFPath
				set oPDF to import strPDFPath to (first parent of oRec)
				
				
				-- IF THE RTFD RECORD IS AN INTERIM RESULT OF AN EML CONVERSION ...
				if blnInterimRTFD then
					set name of oPDF to (texts 1 thru -6 of strName) & ".pdf" -- drop the appended " text"
					set URL of oPDF to strURL
					delete record oRec -- delete the temporary .rtfd (we only want the .pdf)
				else
					set name of oPDF to strName & ".pdf"
				end if
				
				-- DELETE THE TEMPORARY COPY FROM THE DESKTOP
				do shell script "rm " & quoted form of strPDFPath
			end if
		else if strKind = "Group" then
			repeat with oChild in children of oRec
				if kind of oChild is in {"RTF", "RTFD", "eml", "Group"} then my RTForEML2PDF(oChild)
			end repeat
		end if
	end tell
end RTForEML2PDF

Note that there may be an issue with fonts in the eml to rtfd conversion).

(Most emails in Chinese and/or Hebrew are getting through fine on my system, but I’ve noticed that the Hebrew characters were replaced with other symbols in one interleaved Hebrew + English email - presumably font boundaries got confused, and the roman font was applied to the hebrew characters)

(Testing revealed that the problem was in DEVONthink’s eml to rtfd conversion, rather than in the subsequent rtf to pdf conversion)

For completeness, I’d include kind “emlx”.

That route appears to be blocked for emlx files - it turns out that DEVONthink can’t convert emlx records to RTF …

(Fails in Applescript, and if you Ctrl-click an .emlx record, the Convert option offers plain text but no rich text)

Not sure what the technical reason for this is …

I wish I new enough about using scripts in order to use this. I could really use this feature. Could someone briefly walk me through it?

Thanks
Kim

Hello
I finally figured out how to use this script.

  1. I copied the text of the script into Apple Script Editor
  2. I saved it as a script file named: covert RTF to PDF
  3. I placed this script file in the DevonThink Pro 2 scripts folder under “tool bar” folder.

/users/~/library/application support/devonthink pro2/scripts/toolbar

  1. I went to customize tool bar and put the icon for the script in my tool bar.

This was incredible for me as I know nothing about apple scripts.

It does everything I wanted it to do except the following:

When I convert an RTF to a PDF using this script the file name does not drop the “RTF”

For example if I convert

filename example.rtf

I get

filename example.rtf.pdf.

Can someone please tell me how to change the script so it will simple convert to

filename example.pdf

Thanks
Kim

My hats off and thank you to the OP who posted this script. Not only does it do what I wanted, but it forced me to learn a little about applescripts.

Kim,

Robin would need to update the script to remove the extra .rtf extension. Meanwhile, you could try the standard DEVONthink script at Scripts > Rename > Replace Text – replacing “.rtf.pdf” with “.pdf”.

I have modified the script to strip “.rtf” and “.rtfd” from the document names. This version only works until Lion (10.7). If you need a version that works in Mountain Lion please use the script at http://forum.devontechnologies.com/viewtopic.php?f=20&t=13882&p=73834#p73834

-- MODIFIED TO STRIP ".rtf" and ".rtfd" from document names

-- ILLUSTRATIVE DRAFT: Convert selected RTFs (or RTFs in selected groups and their descendants) to PDF
-- Ver 0.7 -- third draft of an attempt to add EML files to the list of convertible types
--       -- Adds the url of the piece of mail to the PDF record

property pstrConvert : "/System/Library/Printers/Libraries/convert -f "
property pstrDesktop : POSIX path of (path to desktop)

on run
	tell application id "DNtp"
		repeat with oSeln in (selection as list)
			set strKind to kind of oSeln
			if kind of oSeln is in {"RTF", "RTFD", "eml", "Group"} then my RTForEML2PDF(oSeln)
		end repeat
	end tell
end run

on RTForEML2PDF(oRec)
	tell application id "DNtp"
		set strKind to kind of oRec
		
		-- Make a temporary RTFD version of any .eml record
		set blnInterimRTFD to false
		if strKind starts with "eml" then
			set strURL to URL of oRec
			set oRec to convert record oRec to rich
			set strKind to kind of oRec
			set blnInterimRTFD to true
		end if
		
		if strKind = "RTF" then
			set {strName, strRTFPath, strFile} to {name, path, filename} of oRec
			if strRTFPath ≠ "" then
				-- Clean the file name
				set strCleanFile to do shell script ("printf '%q' " & quoted form of strFile)
				-- MODIFICATION: Strip the .rtf from the file name
				set stripRTF to do shell script ("echo " & strCleanFile & " | sed -e 's/.rtf//'")
				
				-- CONVERT FROM RTF TO PDF
				--set strPDFPath to pstrDesktop & strCleanFile & ".pdf"
				set strPDFPath to pstrDesktop & stripRTF & ".pdf"
				do shell script pstrConvert & quoted form of strRTFPath & " -o " & quoted form of strPDFPath
				set oPDF to import strPDFPath to (first parent of oRec)
				-- set name of oPDF to strName & ".pdf"
				set name of oPDF to stripRTF & ".pdf"
				
				-- IF THE RTF RECORD IS AN INTERIM RESULT OF AN EML CONVERSION ...
				if blnInterimRTFD then
					set name of oPDF to (texts 1 thru -6 of strName) & ".pdf" -- drop the appended " text"
					set URL of oPDF to strURL
					delete record oRec -- delete the temporary .rtfd (we only want the .pdf)
				else
					set name of oPDF to stripRTF & ".pdf"
				end if
				
				-- DELETE THE TEMPORARY COPY FROM THE DESKTOP
				do shell script "rm " & quoted form of strPDFPath
			end if
		else if strKind = "RTFD" then
			set {strName, strRTFPath, strFile} to {name, path, filename} of oRec
			if strRTFPath ≠ "" then
				-- Clean the file name
				set strCleanFile to do shell script ("printf '%q' " & quoted form of strFile)
				-- MODIFICATION: Strip the .rtfd from the file name
				set stripRTF to do shell script ("echo " & strCleanFile & " | sed -e 's/.rtfd//'")
				
				-- CONVERT FROM RTF TO PDF
				set strPDFPath to pstrDesktop & stripRTF & ".pdf"
				do shell script pstrConvert & quoted form of (strRTFPath & "/TXT.rtf") & " -o " & quoted form of strPDFPath
				set oPDF to import strPDFPath to (first parent of oRec)
				
				
				-- IF THE RTFD RECORD IS AN INTERIM RESULT OF AN EML CONVERSION ...
				if blnInterimRTFD then
					set name of oPDF to (texts 1 thru -6 of strName) & ".pdf" -- drop the appended " text"
					set URL of oPDF to strURL
					delete record oRec -- delete the temporary .rtfd (we only want the .pdf)
				else
					set name of oPDF to stripRTF & ".pdf"
				end if
				
				-- DELETE THE TEMPORARY COPY FROM THE DESKTOP
				do shell script "rm " & quoted form of strPDFPath
			end if
		else if strKind = "Group" then
			repeat with oChild in children of oRec
				if kind of oChild is in {"RTF", "RTFD", "eml", "Group"} then my RTForEML2PDF(oChild)
			end repeat
		end if
	end tell
end RTForEML2PDF

Thanks Patrick
That was very kind of you.
Joe

Hi Patrick
I forgot to ask. Is the modified script you posted to be appended to the original or is it a new version to be used in whole?
Thanks again
Kim

It’s a new version.

Thanks