Thursday, May 23, 2019

#1 2009-06-08 10:24:01 pm

Chuckles66
Member
Registered: 2006-04-26
Posts: 66

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.

Applescript:

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


Filed under: text, return, hidden, string

Offline

 

#2 2009-06-08 11:15:22 pm

chrys
Member
From:: McKinney, TX, USA
Registered: 2007-06-26
Posts: 442

Re: Remove \n from string imported from text file

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 linefeed(?) 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.

Applescript:

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: [url]http://bbs.applescript.net/viewtopic.php?pid=41257#p41257[/url]
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)


--
Chris

Offline

 

#3 2009-06-08 11:31:48 pm

Chuckles66
Member
Registered: 2006-04-26
Posts: 66

Re: Remove \n from string imported from text file

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:

Applescript:

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

Offline

 

#4 2009-06-09 02:29:28 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11560
Website

Re: Remove \n from string imported from text file

Hi,

why not simply

Applescript:


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


regards

Stefan

Offline

 

#5 2019-05-16 10:58:15 am

abeness
Member
Registered: 2007-01-31
Posts: 1

Re: Remove \n from string imported from text file

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:

Applescript:

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

returns clean list items alternated with empty ones.

Applescript:

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

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

Applescript:

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,

Applescript:

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

Applescript:

set lstFile to paragraphs of (read MyFile)

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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)