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
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.