I am trying to read a text file that has different file delimiters, some with carriage return and others use linefeed.
Is it possible to rewrite the file but have them come out with say, one uniform delimiter such as carriage return? I checked the Scripting Additions dictionary but can’t find anything about writing to file “using delimiters…” type command.
I’d read the entire file to a variable, then find and replace carriage returns in that variable with line feeds or vice versa. Then I’d write the variable to a new text file.
You really shouldn’t change the line terminators of files… I could see why but typically you just need to determine what character(s) are used as linefeeds. Here is a function (handler) I use…
property eol_win : return & linefeed as string
property eol_mac : return as string
property eol_unix : linefeed as string
on get_eol_type(theString)
if (offset of eol_win in theString) > 0 then return "win"
if (offset of eol_mac in theString) > 0 then return "mac"
if (offset of eol_unix in theString) > 0 then return "unix"
return 0 -- no line terminator found
end get_eol_type
pass the entire file’s contents to that handler, and it’ll return what type of text file it is.
You can read just the paragraphs of the file into a list.
AppleScript treats LF, CR and CRLF as paragraph delimiters.
Then flatten the list using a specified text item delimiter and write the file back to disk
something like this
set theFile to (choose file)
set theText to paragraphs of (read theFile)
set {TID, text item delimiters} to {text item delimiters, return} -- use CR
set theText to theText as text
set text item delimiters to TID
try
set ff to open for access theFile with write permission
set eof of ff to 0
write theText to ff
close access ff
return true
on error
try
close access file theFile
end try
return false
end try