Why extra characters at line breaks in AS-created .CSV file?

Hi:
Haven’t posted here for a while but I’m scripting again and am having problems with unicode text in a .csv file.

I have a long script that reads file names, looks up info in a data file based on a string in the file name and creates a line of text for each file. At the end, this text is saved in a .csv file. The problem is extra characters at the end of each line in the .csv file.

Here’s an example of the output in the .csv file. Everything is good except the characters at the end of each line. Each line should end with either “Alt1,” Back", or “Front”. Instead, there is a string beginning with “utxt” at the end of each line. What am I doing wrong?

S14_OL5959_634_a1.jpg,Plasmic Trench Jacket,OL5959_634,S14 Outerwear Outdoor,Women’s,Red Hibiscus,Alt1utxt¹
S14_OL5959_634_b.jpg,Plasmic Trench Jacket,OL5959_634,S14 Outerwear Outdoor,Women’s,Red Hibiscus,Backutxtfi
S14_OL5959_634_f.jpg,Plasmic Trench Jacket,OL5959_634,S14 Outerwear Outdoor,Women’s,Red Hibiscus,Frontutxt∆
S14_OM5839_010_b.jpg,Refueler X-Train Short,OM5839_010,S14 Sportswear HXT,Men’s,Black,Backutxt»

Here’s the section of the script that assembles the file-related data into a list, copies that line to a master string as part of a repeat loop and finally, when the repeat is done, saves the master string as a .csv file.

		
---build a string with data corresponding to one file
			set CSVStyleTotal to (CSVFileName & "," & CSVModelName & "," & CSVModelNumber & "," & "," & "," & CSVLightBox1 & "," & CSVLightBox2 & "," & "," & "," & "," & CSVcolor & "," & "," & "," & "," & CSVAltView)
			
--copy that string to the final .csv file
			copy (return & CSVStyleTotal) to end of CSVStyleTotalMasterList

---pass on the complete data to be saved in the .csv file
set theText to CSVStyleTotalMasterList

-----------CREATE THE CSV FILE AND SAVE IT TO THE DESKTOP---------------
set theFilePath to (path to desktop as string) & "test.csv" as string
set theFileReference to open for access theFilePath with write permission
write theText to theFileReference starting at eof
close access theFileReference

You are not writing the opening quote of the 1st field and closing quote of the last.
You are also not escaping the quotes, and the result is not quite a list, that’s where the utxt comes from.
Your first line should begin like so:

set CSVStyleTotal to "\""

which will produce a quote in the output. All other quotes should be changed in this manner. The parentheses are not needed.

I would not use copy in the 2nd line. Instead:

set CSVStyleTotalMasterList to CSVStyleTotalMasterList & return & CSVStyleTotal

Hi.

It looks as if your CSVStyleTotalMasterList is an AppleScript list. It’s that value you’re writing to the file.

Either start with an empty string and build it up by concatenation, or use a list as you’re doing and coerce it to text ” using an appropriate delimiter ” before writing it to the file. Since you’re prepending a return to every line, the appropriate delimiter here would be “”:

--copy that string to the final .csv file
set end of CSVStyleTotalMasterList to (return & CSVStyleTotal) -- return prepended to each line in list.

---pass on the complete data to be saved in the .csv file
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""
set theText to CSVStyleTotalMasterList as text
set AppleScript's text item delimiters to astid

Alternatively, you could not prepend the return each time and use a return as the delimiter instead. This wouldn’t put a return before the first line:

--copy that string to the final .csv file
set end of CSVStyleTotalMasterList to CSVStyleTotal -- return not prepended to each line .

---pass on the complete data to be saved in the .csv file
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to return -- . but inserted between them when the list is coerced to text.
set theText to CSVStyleTotalMasterList as text
set AppleScript's text item delimiters to astid