RTF to HTML

Hi,
I’ve been haunting this forum for some weeks, but never posted before because I’ve always managed to find what I wanted amongst previous posts - so can I begin by thanking all the gurus here for sharing your knowledge so generously.

I’m trying to write a script that will convert rtf files to html with TextEdit. I know there are dozens of posts already about this, but I still can’t get exactly what I want.

This script, using textutil works perfectly:

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 html " & 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
		
	end try
end repeat
set AppleScript's text item delimiters to ASTID

But using it I’ve discovered that textutil writes a slightly less clean form of html than TextEdit. Basically, TextEdit preferences allows me to set html saving options as I want them - ie XHTML 1.0 Strict, No CSS and Unicode (UTF-8).

So my first question is: Is it possible to access preferences for textutil ?

My next attempt was using GUI scripting:

tell application "TextEdit"
	set new_file to (choose file with prompt "Open file to be edited:")
	open new_file
end tell
tell application "System Events"
	tell process "TextEdit"
		keystroke "S" using command down -- shift-command-S
		delay 1
		key code 55
		tell pop up button 1 of group 1 of sheet 1 of window 1 -- file type selector
			click
			delay 1
			key code 125 -- down arrow
			delay 1
			key code 125 -- down arrow
			delay 1
			key code 36 -- select from menu
			delay 1
			key code 48
			key code 48
			
			delay 1
		end tell
	end tell
end tell

It worked at first, and then suddenly stopped working with the following error message:

click pop up button 1 of group 1 of sheet 1 of window 1 of process "TextEdit"
	"System Events got an error: Can't get pop up button 1 of group 1 of sheet 1 of window 1 of process \"TextEdit\". Invalid index."

Any comments would be much appreciated. Many thanks in advance.

Mike

I don’t know about ‘preferences’ for a CLI command. Generally, all you can do is in the command’s manpage.

The textutil manpage has this option:

but that is as close as it gets to setting the HTML version.

Hartstikke bedankt, Alastor933,

As it happens, I did know about -excludedelements but was hoping there might be an easier solution. Just lazy, I guess ! Looks like I’m off on a new journey into terminal commands…

tot ziens,

Mike

Well - I haven’t managed to get the textutil version to write the html I want, but have got the gui script version working after downloading UIElementInspector. The only mistake in my earlier version was that the file type selector should be: ‘pop up button 1 of group 1 of group 1’.

tell application "TextEdit"
	set new_file to (choose file with prompt "Open file to be edited:")
	open new_file
end tell
tell application "System Events"
	tell process "TextEdit"
		keystroke "S" using command down -- shift-command-S
		delay 1
		tell sheet 1 of window 1
			tell pop up button 1 of group 1 of group 1 -- file type selector
				click
				key code 125 -- down arrow
				key code 125 -- down arrow
				key code 36 -- select from menu
				key code 36 -- save
			end tell
		end tell
	end tell
end tell