Convert HTML to RTF

Hi,

I’m hoping someone can shed some light on this. Using AppleScript under 10.4.2, I’d like to open an html file with TextEdit and save it as an RTF file. This is my script so far:

tell application "TextEdit"
	open file "Tiger:Users:aclay:Desktop:releasenotes.html"
	save document 1 as "RTF" in alias "Tiger:Users:aclay:Desktop:releasenotes.rtf"
	quit
end tell

The problem with the script is that TextEdit asks if I’d like to overwrite the document, which I don’t want to do. I want to create a new document, and I don’t want any user interaction during the script. What am I missing?

FWIW, after I have an RTF document, I’ll use the convert terminal command to change it to a PDF. As an alternative to solving the above issue, a script to take an html document and make it a PDF would more than acceptable.

Thanks!

barking:

You can do it all in the terminal. Use the textutil command to convert from txt or rtf to html or vice versa or whater and then pipe the result to whatever pdf converter you have and you are done. Here is the basic syntax, called from AS:

set a to choose file
set b to POSIX path of a
do shell script "textutil -convert html " & b

This converts any appropriate chosen file (as long as it is rtf, txt, rtfd, word, and a couple others, I think) to html.

I would be interested in knowing what you are using in your terminal to convert to pdf.

casdvm

Try something like this:

choose file without invisibles
set theFile to POSIX path of result
do shell script "/usr/bin/textutil -convert rtf " & quoted form of result

set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"."}
set theFile to text items 1 through -2 of theFile
set AppleScript's text item delimiters to ASTID
set theFile to theFile as text

do shell script "/System/Library/Printers/Libraries/convert -f " & quoted form of (result & ".rtf") & " -o " & quoted form of (result & ".pdf") & "; /bin/rm -f " & quoted form of (result & ".rtf")

Bruce:

I cannot find a man page or a -help file on that convert command anywhere. Does it exist?

casdvm

Barely. Using “convert ?” will list the usage (aka options).

Added support for multiple files. This could easily be converted into a droplet, if you’re in to that sort of thing.

set ASTID to AppleScript's text item delimiters

choose file with multiple selections allowed without invisibles
repeat with thisFile in result
	try
		set thisFile to POSIX path of thisFile
		
		do shell script "/usr/bin/textutil -convert rtf " & quoted form of result
		
		set AppleScript's text item delimiters to {"."}
		set thisFile to text items 1 through -2 of thisFile
		set AppleScript's text item delimiters to {""}
		set thisFile to thisFile as text
		
		do shell script "/System/Library/Printers/Libraries/convert" & ¬
			" -f " & quoted form of (result & ".rtf") & ¬
			" -o " & quoted form of (result & ".pdf") & ¬
			"; /bin/rm -f " & quoted form of (result & ".rtf")
	on error errorMsg number errorNum
		display dialog "Error (" & errorNum & "):" & return & return & errorMsg buttons "Cancel" default button 1 with icon caution
	end try
end repeat
set AppleScript's text item delimiters to ASTID

Bruce:

“Barely” is just the right word.

Thanks

casdvm

Thanks for all the suggestions. The textutil command is exactly what I needed, and will most certainly come in handy in the future.