Convert RTFs to PDF

Hi Patrick,
I repeated the steps above with your new script and received the following error when I tried to save it in AppleScript editor as a script file. See pic.
Thanks
Kim
screenshot_106.jpg

I just tested it and it works for me. Steps to reproduce:

1.) Click “Select All” on the top of the script here on the forum
2.) Press CMD+C to copy the code
3.) Open AppleScript Editor and paste via CMD+V
4.) Press CMD+K to compile the script (this should be where your error happend)
5.) Save the script

Could you please try this approach? If it still fails, can you provide a screenshot of the while AppleScript Editor window?

Your right, I did not do the CMD-K. It works great.
Thanks again.

Seems like this script has been broken on my MacBook with a recent Mountain Lion update.

It seems that “/System/Library/Printers/Libraries/convert” doesn’t exist anymore more in ML.

Any work arounds for this?

I am sorry, I am still using Lion. It took me over a year to go to Lion. The only reason I did is that SL would not support iCoud. If it did, I would still be in SL. I am not a big fan of Lion. I probably will wait several months if I decide to switch to ML.

Reading a little bit more into the problem, it seems that convert was just a symlink to a shell script called “cupsfilter”

I’m trying to read how to create a new symlink back to cupsfilter, but having a hard time getting it going with my limited knowledge in coding.

You can read more about it here:

mail-archive.com/use-livecod … 28678.html

Thanks for the explanatory link. I have modified the script once again to work on Mountain Lion. Everyone who’s using Snow Leopard or Lion should use the old script which can be found at [url]Convert RTFs to PDF]:

-- MODIFIED TO STRIP ".rtf" and ".rtfd" from document names
-- MODIFIED for Mountain Lion to use "cupsfilter" as "convert" isn't available anymore

-- 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 : "/usr/sbin/cupsfilter "
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 & " > " & 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") & " > " & 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

Thank you, Patrick, for fixing the regression – and Tyler for pointing it out. This has always been a very valuable script since Rob originally posted it.

For anyone using “convert” in other scripts, and who needs to fix the regression in 10.8.x, I’d point out the changes Patrick made:

Changed the path to “convert”


property pstrConvert : "/System/Library/Printers/Libraries/convert -f "

to the path to “cupsfilter”


property pstrConvert : "/usr/sbin/cupsfilter "

and changed this syntax from


do shell script pstrConvert & quoted form of strRTFPath & " -o " & quoted form of strPDFPath

to this syntax


do shell script pstrConvert & quoted form of strRTFPath & " > " & quoted form of strPDFPath

The important change in the later pair is the revision of cupsfilter’s parameters from " -o " to " > "

Before using this change on 10.8.x, you might need to check permissions on /usr/sbin/cupsfilter


EDIT: See also Patrick’s note immediately following this one

And don’t forget to change this:


do shell script pstrConvert & quoted form of (strRTFPath & "/TXT.rtf") & " -o " & quoted form of strPDFPath

to this:


do shell script pstrConvert & quoted form of (strRTFPath & "/TXT.rtf") & " > " & quoted form of strPDFPath

Thanks for this Patrick.

It still isn’t working for me, though. I’m not getting an error message from applescript, but it’s not making a pdf file, either. Although it could be my computer as I messed around with the cupsfilter file …

Patrick, did you actually run this under ML?

EDIT: I figured this out. I actually has a .txt file selected…

Sorry and thanks again Patrick.

The script works here for me, using 10.8.1.