Append Quoted Text to CSV File

I’m trying to append comma delimited text to a .csv file. I need the lines to be appended to the file in the following format:


set csvFile to "/Users/me/Desktop/test.csv"

set movieTitle to "Captain Phillips"  
set movieYear to "2013"  
set movieStars to "Tom Hanks,Catherine Keener"
set movieLang to "English"

set movieDetails to movieTitle & "," & movieYear & "," & movieStars & "," & movieLang

do shell script "echo " & quoted form of movieDetails & " >> " & csvFile

This does append the text to the file, but flattens it:

How do you script it to include the double quotes in the output?

Hi,

this solution puts the information in a list and uses text item delimiters to build the string


set csvFile to POSIX path of (path to desktop) & "test.csv"

set movieTitle to "Captain Phillips"
set movieYear to "2013"
set movieStars to "Tom Hanks,Catherine Keener"
set movieLang to "English"
set movieDetailList to {movieTitle, movieYear, movieStars, movieLang}
set {TID, text item delimiters} to {text item delimiters, (quote & "," & quote)}
set movieDetails to (quote & movieDetailList as text) & quote
set text item delimiters to TID

do shell script "echo " & quoted form of movieDetails & " >> " & csvFile

However, movie titles containing quotes will still not work. There are only a handful of movie titles containing quotes but either way to make it according the CSV guidelines you should escape quotes and (optionally) only quote field that are necessary to quote, fields that contains no special characters doesn’t have to be quoted.

set csvFile to "$HOME/Desktop/test.csv"

set movieTitle to "Captain Phillips"
set movieYear to "2013"
set movieStars to "Tom Hanks,Catherine Keener"
set movieLang to "English"

set movieDetails to createCSVRow({movieTitle, movieYear, movieStars, movieLang}, false, ",", quote)

do shell script "echo " & quoted form of movieDetails & " >> " & csvFile


on createCSVRow(theList, alwaysQuote, fieldSeparator, textIndicator)
	repeat with x from 1 to count theList
		set item x of theList to quoteCSVField(item x of theList, alwaysQuote, fieldSeparator, textIndicator)
	end repeat
	
	set oldTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to fieldSeparator
	set csvRow to theList as string
	set AppleScript's text item delimiters to oldTID
	return csvRow
end createCSVRow

on quoteCSVField(plainString, alwaysQuote, fieldSeparator, textIndicator)
	if alwaysQuote or plainString contains fieldSeparator or plainString contains textIndicator then
		set oldTID to AppleScript's text item delimiters
		set AppleScript's text item delimiters to textIndicator
		set textItems to text items of plainString
		set AppleScript's text item delimiters to textIndicator & textIndicator
		set str to textItems as string
		set AppleScript's text item delimiters to oldTID
		return textIndicator & str & textIndicator
	else
		return plainString
	end if
end quoteCSVField

Thanks Stefan, works perfectly. I haven’t seen a quoted title in my film list yet DJ Bazzie Wazzie but I’ll keep that caveat in mind.