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
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.
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…
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)
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)
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”.
-- 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
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