Make a so-called csv a real csv

When you create csv files from the same systems than those which are not able to properly open them, you might need afterward to clean it up. I often find myself annoyed by so-called csv files that are actually delimited by semicolons, and commas are used as decimal separators. Here is a code to first replace commas by periods, then replace semicolons by commas.

tell application "Finder" to set FileToEdit to selection as string
set AccessToFile to open for access file FileToEdit with write permission
set CSVContent to read file FileToEdit
to switchText from t to r instead of s
set d to text item delimiters
set text item delimiters to s
set t to t's text items
set text item delimiters to r
tell t to set t to item 1 & ({""} & rest)
set text item delimiters to d
end switchText
switchText from CSVContent to "." instead of ","
set FirstStepText to result
switchText from FirstStepText to "," instead of ";"
set TextToWrite to result
set eof of file FileToEdit to 0
write TextToWrite to AccessToFile
close access AccessToFile

Credit goes to member kai (

Posted by Benjamin

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.