Weird Applescript Bug !

Hi everyone !

I’ve got the weirdest thing happening in some code I use to filter text. I’ll explain. I’m using an applescript driven table to store some information. Then I use this info to build a webpage table in html. I use template files to store the bulk of the html code which I then read into my script. The odd thing is that for a very tiny section I have to filter some text that is extracted from a data cell. Basically if there is no text in the data cell, I need to insert a " " into the html. Here’s the thing, I have certain cells that shouldn’t contain anything. However, my filter misses the “” and does NOT replace it with " " yet others work perfectly. This is bad because certain browsers like IE does bad things to my table. Is there something wrong with my code? Please have a look, because I’m baffled !!

Here’s the code:

on ExportNORMHTML()
	--prepare the HTML Template
	tell application "Finder"
		set HTMLTemplateFile to file "Library:Application Support:MyApp:HTML Templates:HTML Template.txt" of the startup disk as alias
	end tell
	set theData to read HTMLTemplateFile
	set AppleScript's text item delimiters to "<FLAG>"
	set theDataLIST to every text item in theData
	set itemcount to the count of items in theDataLIST
	set HeaderToTitle to item 1 of theDataLIST
	set AfterTitleToINFOTitle to item 2 of theDataLIST
	set AfterInfoTitleToDate to item 3 of theDataLIST
	set AfterDatetoTABLEHeigt to item 4 of theDataLIST
	set AfterTABLEHeigttoTABLE to item 6 of theDataLIST
	--set ROWBuild to item 5 of theDataLIST --not required
	set TheEND to the last item of theDataLIST
	set CellStyle to quoted form of "style6"
	set CellFontAllignment to quoted form of "center"
	set BtdB to "<tr>" & return & space & space & space & space & "<td class=" & CellStyle & "><div align=" & CellFontAllignment & ">" as string
	set Connector to "</div></td>" & return & space & space & space & space & "<td class=" & CellStyle & "><div align=" & CellFontAllignment & ">" as string
	set EtdE to "</div></td>" & return & "</tr>" & return
	
	--read the datasource
	try
		set every_row to every data row in theDataSource
		set rowcount to the count of items in every_row
	on error
		display dialog "No Info to export to Sheet " giving up after 2 buttons {"Ok"} default button 1 with icon 1 --attached to window "main"
	end try
	tell save panel
		set title to "Export to HTML"
		set prompt to "Save"
		set required file type to "html"
		set treat packages as directories to false
	end tell
	set AppleScript's text item delimiters to ".txt"
	set Session_Name to every text item in Session_Name
	set Session_Name to item 1 of Session_Name
	set SheetNAME to Session_Name
	set theResult to display save panel in directory LastUserSelectedDestination with file name Session_Name 
attached to window "main"
	
	if theResult is 1 then
		set theSaveDestination to path name of save panel
		set LastUserSelectedDestination to theSaveDestination
		set theSaveDestination to every character in theSaveDestination
		set NewSTring to ""
		set First_ to true
		repeat with i in theSaveDestination
			set i to i as string
			if i = "/" then
				set i to ":"
			end if
			if First_ = true then
				set i to ""
				set First_ to false
			end if
			set NewSTring to NewSTring & i
		end repeat
		set theSaveDestination to NewSTring as string
		set NumberofRows to count of items in every_row
		set Divider to 14.48
		set tableHeight to round NumberofRows * Divider
		set theHTMLEntryDATAMaster to {}
		set MultiRows to ""
		repeat with theRow in every_row
			set Title_ to contents of data cell "Title_" of theRow as string
			set Composer_ to contents of data cell "Composer_" of theRow as string
			set Publisher_ to contents of data cell "Publisher_" of theRow as string
			set track_no to contents of data cell "track_no" of theRow as string
			set cd_title to contents of data cell "CDTitle_" of theRow as string
			set RecordDetails_ to contents of data cell "RecordDetails_" of theRow as string
			set Usage_ to contents of data cell "Usage_" of theRow as string
			set Duration_ to contents of data cell "Duration_" of theRow as string
			--prepare HTML Data
			set Title_ to my FilterEMTYCellsForHTMLExport(Title_)
			set Composer_ to my FilterEMTYCellsForHTMLExport(Composer_)
			set Publisher_ to my FilterEMTYCellsForHTMLExport(Publisher_)
			set track_no to my FilterEMTYCellsForHTMLExport(track_no)
			set cd_title to my FilterEMTYCellsForHTMLExport(cd_title)
			set RecordDetails_ to my FilterEMTYCellsForHTMLExport(RecordDetails_)
			set Duration_ to my FilterEMTYCellsForHTMLExport(Duration_)
			set MultiRows to MultiRows & BtdB & Title_ & Connector & Composer_ & Connector & Publisher_ & Connector & cd_title & Connector & RecordDetails_ & Connector & Usage_ & Connector & track_no & Connector & Duration_ & EtdE
		end repeat
		set AppleScript's text item delimiters to ""
		set currentDateString to ((day of (current date)) & space & (month of (current date)) & space & (year of (current date))) as string
		set FinalHTMLString to HeaderToTitle & SheetNAME & AfterTitleToINFOTitle & CUESheetNAME & AfterInfoTitleToDate & currentDateString & AfterDatetoTABLEHeigt & tableHeight & AfterTABLEHeigttoTABLE & MultiRows & TheEND
		
		my write2CSV(FinalHTMLString, theSaveDestination)
		tell application "Safari"
			open theSaveDestination
		end tell
	end if
end ExportNORMHTML

on FilterEMTYCellsForHTMLExport(theStringtoFilter)
	if theStringtoFilter is equal to "" then
		set theStringtoFilter to " "
	else
		set theStringtoFilter to theStringtoFilter
	end if
	return theStringtoFilter
end FilterEMTYCellsForHTMLExport

If you got this far , THANK YOU for sitting through all that. Any help is greatly appreciated.

Thanks again !
Eugene

P.S
I discovered that it always happens on specific cells. I tried to copy the data cell contents to a text file to see if there might be some other form of formatting that might be causing my filter’s failure, but without success.

This is just a wild guess, but maybe you are running into cells with non-empty string values that contain only whitespace characters. You might try using something like the trim() handlers to trim away leading and trailing whitespace:

on FilterEMTYCellsForHTMLExport(theStringtoFilter)
	if trim({space, tab, return, ASCII character 10}, theStringtoFilter) is equal to "" then
		return "&" & "nbsp;" -- obfuscated for correct processing by MacScripter BBS applescript tag
	else
		return theStringtoFilter
	end if
end FilterEMTYCellsForHTMLExport

-- This is a modified version of the second trim handler from http://bbs.applescript.net/viewtopic.php?pid=66143
on trim(someCharacters, someText)
	local someCharacters, someText
	
	if length of someText is equal to 0 then return someText
	
	repeat until someText's first character is not in someCharacters
		if length of someText is equal to 1 then return ""
		set someText to text 2 thru -1 of someText
	end repeat
	
	repeat until someText's last character is not in someCharacters
		set someText to text 1 thru -2 of someText
	end repeat
	
	return someText
end trim

Hi Chris!

Thanks for the reply. I had another go using your suggestion , but unfortunately still no success. However I’m investigating other potential formatting issues aswell. Perhaps the best way would be to check the string before it is actually added to the data cell. A little preventative coding has never hurt anyone - I think !!!:wink:

It would still be nice to know why though. Anyway thanks again !

Cheers
Eugene