How do I save a string to a CSV file using applescript?

Hi,

I have an applescript that returns a string in the form:

Value1,Value2,Value3,Value4
Value5,Value6,Value7,Value8
Value9,Value10,Value11,Value12
Value13,Value14,Value15,Value16 etc.

I would like to be able to save this string as a CSV file on my desktop but I’m not sure how to do this.

Could someone help me out with this?

Thanks,

Nick

Richard, I wanted to comment on your code. It’s great that you are sharing and I encourage you to do it more. However I need to point out a major flaw in your code. One of the most common mistakes you can make is to tell an application to perform a command that it’s not designed to handle. This is very inefficient and often leads to unexpected errors. Since you are sharing your code and thus are teaching others to program, I don’t think you want to teach bad habits.

To explain… you have all of the code inside a “tell application Finder” block. Only 2 or 3 lines of your code needs to be performed by the Finder. The rest of it can be done by applescript itself. So please separate the Finder code from the rest of the code. As mentioned it’s a good habit to have to avoid errors.

Below is the code I use to write to files. It has error checking and doesn’t use the Finder at all. I hope this helps. Here’s an example of how to perform the task the user needs, but the “writeTo” handler can be used in all scripts. CSV files are just normal text files (so we write the data as text) with a “csv” file extension.

set filePath to (path to desktop as text) & "theString.csv"
set theString to "Value1,Value2,Value3,Value4
Value5,Value6,Value7,Value8
Value9,Value10,Value11,Value12
Value13,Value14,Value15,Value16"

set theResult to writeTo(filePath, theString, text, false)
if not theResult then display dialog "There was an error writing the data!"

on writeTo(targetFile, theData, dataType, apendData)
	-- targetFile is the path to the file you want to write
	-- theData is the data you want in the file.
	-- dataType is the data type of theData and it can be text, list, record etc.
	-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
	try
		set targetFile to targetFile as text
		set openFile to open for access file targetFile with write permission
		if apendData is false then set eof of openFile to 0
		write theData to openFile starting at eof as dataType
		close access openFile
		return true
	on error
		try
			close access file targetFile
		end try
		return false
	end try
end writeTo

This post was extremely helpful. I have created a csv file, written to it and then read it.Very pleased. The initial / default state appeared to overwrite the values but if the character count of the new data was less then the previous, tail end of of old data remained. So I then introduced set eof to 0. This appears to have altered the file permanently so the previous working script fails. My questions are: Is there anyway of finding the EOF status of a file and its write permissions. Should this value reset when the file is closed. Is the other EOF value 1.Do I need to clear the file if so how.
Write script works:
–open for access file “Desktop:test csv new.csv” with write permission
–set eof of “Desktop:test csv new.csv” to 0
set theString to “56,pigs,cats,Value4\nValue5,Value6,Value7,Value8\nValue9,Value10,Value11,Value12\nValue13,Value14,Value15,Value16”
write theString to file “Desktop:test csv new.csv”

The commented out lines produce errors: file already open, Can’t make “test csv new.csv” into type file.

Read script did work but now fails:
set my_data to read file “Desktop:test csv new.csv”
display dialog my_data
result:Applescript Error End of file error.
Any pointers greatly received. Roger

Model: Mac pro 5.1
AppleScript: 2.3
Browser: Safari 537.36
Operating System: macOS 10.14

Yeah. You have to do things properly.

The ‘file already open’ error comes from the fact that the file was opened for access with write permission on a previous run and not closed again. (There can only be one write-permission access to it at any one time.) If you’re running the script in Script Editor, Script Editor will own that access, so if you close Script Editor, its access channels to any open files should be released.

When using the File Read/Write commands, always use the access code returned by ‘open for access’, use the correct syntax in all the commands, and make sure any errors are trapped so that the script keeps going long enough to close the access again.

set theString to "56,pigs,cats,Value4
Value5,Value6,Value7,Value8
Value9,Value10,Value11,Value12
Value13,Value14,Value15,Value16"

-- Open a write-permission access and get the id returned for it.
set accessRef to (open for access file ((path to desktop as text) & "test csv new.csv") with write permission)
-- Use the returned id in all the following file actions. Use a 'try' block to catch any errors that occur and stop the script from crashing before the access is closed.
try
	set eof accessRef to 0
	write theString to accessRef
	close access accessRef
on error errMsg
	close access accessRef
	display dialog errMsg
end try

Could you please use MacScripter’s [applescript] and [/applescript] tags when posting AppleScript code here? There’s a button for them just above the “Message” field on posting pages.

Do your error handling homework! :wink:

It’s highly recommended to use the entire error handling as suggested in post #2. It guarantees that the file is always closed reliably.

If the file is already opened insert these lines at the beginning of the script

try
	close file "Desktop:test csv new.csv"
end try

run the script once and delete the lines.

PS: Consider that the file path is only valid if there is a disk “Desktop” and the file “test csv new.csv” is located on the top level.