tags:

views:

101

answers:

3

I have a txt plain file that has a list of numbers. One number per line, like:

978-972-722-649-8
978-972-722-646-7
978-972-722-627-6
978-972-722-625-2
978-972-722-594-1

etc.

This list is on a file called file.txt, was created and saved on TextEdit using Western (ISO Latin 1) encoding.

Now I am trying to use Applescript to read this file and transform it on a list.

I have this script

set myDirectory to path to desktop folder as string
set myFile to myDirectory & "file.txt"

try
    set open_file to ¬
        open for access file myFile without write permission
    set myList to read open_file using delimiter return
    close access open_file
on error
    try
        close access file open_file
    end try
end try

after running this script, myList contains just ONE item and this item is "97" (I suppose that is the first two numbers of the first entry).

What is going on?

Thanks.

+1  A: 

If you saved your file as Western (ISO Latin 1) most likely it has LF as a delimiter.

Try using the following line:

set myList to read open_file using delimiter "\n"

and see if it works for you.

I tried running your script on my laptop and it works fine with the above modifications.

Also, run the od command in the shell to see if the file is encoded right. Here is the output of my command:

vlad$ od -c file.txt 
0000000    9   7   8   -   9   7   2   -   7   2   2   -   6   4   9   -
0000020    8  \n   9   7   8   -   9   7   2   -   7   2   2   -   6   4
0000040    6   -   7  \n   9   7   8   -   9   7   2   -   7   2   2   -
0000060    6   2   7   -   6  \n   9   7   8   -   9   7   2   -   7   2
0000100    2   -   6   2   5   -   2  \n   9   7   8   -   9   7   2   -
0000120    7   2   2   -   5   9   4   -   1  \n                        
0000132
Vlad
Thanks but it says this delimiter "\n" is invalid. I replaced it withdelimiter linefeed, but the problem persists. I ran the od command and it is encoded like yours.
Digital Robot
(continued)... I have tried with other types of TXT files and in all cases the final list contains just one entry that is the first characters the file contains. A file with, 12345\n67890\n00000\n will contain one item that is "12".
Digital Robot
how do you check the list? can you post the code snippet of it? there might be a problem there.
Vlad
for example... display dialog number of items in myList gives me 1 as result... it should be 5 in this example. Another example: display dialog myList gives me 97 (the first two chars read from the file).
Digital Robot
(continued).. the most strange thing is that I can change the delimiter to whatever I want, even crazy characters as ASCII character 202 and I have the same result... 97!!!
Digital Robot
+1  A: 

The read command's using delimiter(s) parameter is problematic. Use the following instead, which works with any linefeed character(s):

set myList to every paragraph of (read open_file)
has
thanks, but using your code results on having myList with just ONE entry and that entry corresponds to the first two characters read from file, reversed... in this case myList contains ONE entry that is 79.
Digital Robot
In that case, something else is the problem. I can't reproduce your bug; when I try creating a file.txt, this solution worked for me. I'm not sure what could help us diagnose this---perhaps if you posted the file somewhere?
Antal S-Z
+1  A: 

It sounds like something weird is going on with the file, but have you tried another approach:

set myfilePPath to posix path of myFile
set myList to every paragraph of (do shell script("cat \"" & myfilePPath "\"))

The cat programme just types out the contents of the file. Even i it doesn't work, it might give you a clue to what's going on.

stib