Remove \n from string imported from text file

Hello,

I am attempting to create a script that will read a text file and show lists based on the content.

I import the data from the text file, then parse it using {return} as the delimiter. This is all good until I try to convert some of the content into something else (eg. an alias). It fails. I have been able to ascertain that the text after the first return is importing with a “\n” at the start (eg. the line of text “Public” is importing as “\nPublic”).

Help!

I have tried setting custom delimiters to remove it, stepping through the characters, etc with no result. I have tried using “\n” as the delimiter on import but that just gave me one long string, when what I need is a list.

Could somebody please tell me either how to remove it from the string in applescript, or ideally how to make sure it doesn’t import “\n” in the first place.

This is the subroutine I use to import and parse text.

--Reads lists from a text file
on ReadTextFiles(TheTextFile)
	open for access TheTextFile
	set TheList to read TheTextFile using delimiter {return}
	close access TheTextFile
	return TheList
end ReadTextFiles

It sounds like maybe you have a file with mixed line endings (some UNIX: LF-only; some Mac: CR-only; maybe some DOS: CR+LF?). You could try specifying each of the line ending variations as a delimiter: read TheTextFile using delimiter {return & LF, return, LF} (where LF is ASCII character 10 on Tiger, or [b]linefeed/b on Leopard). You might end up with some empty strings in results though.

Another approach might be to strip out all the LF characters after you read in return delimited lines.

set f to choose file
ReadTextFiles(f)
stripLFFromStrings(result)

--Reads lists from a text file
on ReadTextFiles(TheTextFile)
	read TheTextFile using delimiter {return}
end ReadTextFiles

to stripLFFromStrings(stringList)
	set newList to {}
	repeat with str in stringList
		set end of newList to switchText from str to "" instead of ASCII character 10
	end repeat
	newList
end stripLFFromStrings
(* switchText From: http://bbs.applescript.net/viewtopic.php?pid=41257#p41257
Credit: kai, Nigel Garvey*)
to switchText from t to r instead of s
	local d
	set d to text item delimiters
	try
		set text item delimiters to {s}
		set t to t's text items
		-- The text items will be of the same class (string/unicode text) as the original string.
		set text item delimiters to {r}
		-- Using the first text item (beginning) as the first part of the concatentation means we preserve the class of the original string in the edited string.
		tell t to set t to beginning & ({""} & rest)
		set text item delimiters to d
	on error m number n from o partial result r to t
		set text item delimiters to d
		error m number n from o partial result r to t
	end try
	t
end switchText

Either of these may work better for you, depending on the actual contents of the file you are trying to read.

Also, note that if you are just going to read the whole file in at once, open for access and close access are not needed.

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 4 Public Beta (4528.17)
Operating System: Mac OS X (10.4)

THANKYOU THANKYOU THANKYOU!

This has been frustrating me for ages. In the end it was the simple solution of adjusting the delimiters it was using to read the content in like you suggested. It did create some empty list items but it was easy to get rid of them.

Here is the final code:

--Reads lists from a text file
on ReadTextFiles(TheTextFile)
	set LF to ASCII character 10
	set TempList to read TheTextFile using delimiter {return & LF, return, LF}
	set TheList to {}
	repeat with g from 1 to count TempList
		set TempItem to item g of TempList
		if TempItem = "" then
			--do nothing
		else
			set TheList to TheList & TempItem
		end if
	end repeat
	return TheList
end ReadTextFiles

Hi,

why not simply


set f to choose file
ReadTextFiles(f)

--Reads lists from a text file
on ReadTextFiles(TheTextFile)
	return paragraphs of (read TheTextFile)
end ReadTextFiles

paragraph considers LF, CR and CRLF as a line break

Refreshing this thread nearly 10 years on, after banging my head against an oddity. Using High Sierra 10.13.6.

I have a text file with CRLF generated by AppleScript (an output log) as “character id 13 & character id 10”. No matter which way I tried to parse that to a list using specified delimiters on reading it back in, I ended up with problems:

set lstFile to (read MyFile using delimiter {return & LF, return, LF})

returns clean list items alternated with empty ones.

set lstFile to (read MyFile using delimiter {return & LF})

returns first item correctly, then every item prepended with \n.

set lstFile to (read MyFile using delimiter {LF})

returns every list item appended with \r.

I haven’t yet worked out why the identical CRLF string I’m using to generate the file is not read correctly on reading it back. That is,

set myDelim to character id 13 & character id 10
set lstFile to (read MyFile using delimiter myDelim)

results in list items prepended with \n: e.g. “\nART12B-A2”.

Stephan’s

set lstFile to paragraphs of (read MyFile)

is able to parse the CRLF correctly and returns clean list items. Thanks, Stefan.