File vs stdin and stout when converting with textutil shell script

If I copy the styled text of a short, compiled Applescript such as


set inputText to text returned of (display dialog “Enter some text for your new Sticky widget…” default answer "Note: ")
do shell script “open /Library/Widgets/Stickies.wdgt”
delay 0.2
tell application “System Events”
keystroke tab
keystroke tab
keystroke inputText
end tell

then paste it into a TextEdit window and save it as rich text file on my desktop and run this shell script on it:

/usr/bin/textutil -convert html ~/Desktop/webpage.rtf -output ~/Desktop/webpage.html

the resulting html file displays the converted text almost exactly as it appeared in the rtf document.

If, however, I copy the same styled text to my clipboard, then run the following script to perform the conversion without writing to a file:


get the clipboard
set app_identifier to do shell script "echo " & quoted form of the result & space & “| textutil -convert html -stdin -stdout”
set the clipboard to result

The resulting html output doesn’t retain all the style elements such as color and basic style (bold or italic).

Why does the clipboard conversion method fail to retain all the styled text attributes?
What can I do to get the same output when converting using stdin and stdout that I get when performing the conversion on an rtf file?
Thanks.

The value of returned by the clipboard is, by default, just normal (unstyled) text (not RTF). If the sender supports it, you can extract the RTF data, but not in a form that is can be directly concatenated with other strings to form a shell command for do shell script.

You could do something gnarly like this:

-- need to use a proper temporary file and error checking in actual production code
set pp to "/tmp/foo-blah.rtf"
set f to POSIX file result
set rtfData to get the clipboard as «class RTF » -- «data RTF hex-byte-values.» can not do much with it except write it out to a file
set fRef to open for access f with write permission
set eof of fRef to 0
write rtfData to fRef
close access fRef
do shell script "textutil -convert html " & quoted form of pp & " -stdout && rm " & quoted form of pp

But there might be a better way (my 10.4 system does not support the -stdin option):

do shell script "pbpaste -Prefer rtf | textutil -convert html -stdin -stdout

There must be something about the “-stdout” function that either 1) I don’t understand, or 2) isn’t working. Probably the former choice.

Anyway, neither of your suggested scripts resulted in retaining all the style elements that were converted when I told textutil to output to file. But, considering your first suggested script, I figure if we’re going to get that gnarly, we might as well go even further, so I went on to construct this, which gets me close enough to what I want, I think:


set pp to "/tmp/foo-blah.rtf"
set f to POSIX file result
set rtfData to get the clipboard as «class RTF » 
set fRef to open for access f with write permission
set eof of fRef to 0
write rtfData to fRef
close access fRef
set dp to (pp & ".html")
do shell script "/usr/bin/textutil -convert html " & (quoted form of pp) & " -output " & (quoted form of dp)
repeat
	try
		set tdata to read dp
		exit repeat
	on error
		delay 1
	end try
end repeat
do shell script "rm" & space & (quoted form of pp) & ";rm" & space & (quoted form of dp)
set the clipboard to tdata

Then I went on to try your second method, like this:

set pp to "/tmp/foo-blah.rtf"
set dp to (pp & ".html")
do shell script "pbpaste -Prefer rtf | textutil -convert html -stdin -output " & (quoted form of dp)
repeat
	try
		set tdata to read dp
		exit repeat
	on error
		delay 1
	end try
end repeat
do shell script "rm" & space & (quoted form of pp) & ";rm" & space & (quoted form of dp)
set the clipboard to tdata

But although it was fast, it didn’t retain all the style attributes, either

But that’s not to say, of course, that your response didn’t help, so I thank you.