Generating HTML; extra characters

I have created an applescript that takes a opened QT movie gets the parameters and creates an HTML file that embeds the movie. Everything works fine on my computer. (MacBookPro) but when I transfer it to a couple of production G5s the HTML generated places hidden characters between each character generated.

I have used several dialog boxes to capture user info and then create the page using a series of set statements like:

set htmlText to "<html>" & return & "<head>" & return & "<title>"

I then write the file with an if:

if my write_to_file(htmlText, target_file, false) is false then
	set the clipboard to the HTML_text
	beep
	-- if the sub-routine returns a false value then display this error dialog
	display dialog "There was a problem writing to the HTML file. " & ¬
		"Is the HTML file open? If so, close the file and try again." & ¬
		return & return & "The HTML text has been placed on the clipboard instead." buttons {"Cancel"} default button 1
end if

Any suggestions?

I am wondering if I need to do write_to_file((htmlText as string))… or something

Are the G5s servers or are they running your script (which you didn’t show)?

The “hidden characters” between every character tell you that the result (I don’t know whether the document or the web page as viewed) is in UTF-16; Unicode text in other words. Without seeing your “writer” I don’t know why.

The G5s are not servers. Though the script got on to the G4s via servers (Win2000 via smb). I had save them as applications first but they kept being thought of as Classic apps. I eventually got it over by removing the .app and saving as application bundle.

Here is the write_to_file:

on write_to_file(this_data, target_file, append_data)
	try
		set the target_file to the target_file as text
		set the open_target_file to ¬
			open for access file target_file with write permission
		if append_data is false then ¬
			set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end write_to_file

How this presented itself is that everything worked fine on my setup. I sent the script around and someone used it and they started getting foreign characters on the web browser display of the html file. They opened up the html in Text Wrangler and turned on hidden characters and could see hidden characters (upside down ?) between each character.

Since my posting I have made a change :

if my write_to_file((htmlText as string), target_file, false) is false then…

(“as string” is new)

and that seems to have helped, does that make sense?

Perfect sense here. Unicode text in AppleScript is UTF-16 (as Adam mentioned); If this is written to a file and then opened in another application that isn’t expecting UTF-16, the file will be read incorrectly. (The “hidden characters” are actually null bytes, or ASCII character 0.)

I would remove your change above, and instead change your write_to_file handler to produce UTF-8 instead:

write this_data to the open_target_file as «class utf8» starting at eof