Read textfile into list in Applescript

applescript, file, text

Solution

The error is that you are trying to read the contents of a file (the first file you read) as a file. Getting the paragraphs of text will break it apart at return/linefeed boundaries, which usually works better than trying to guess what end of line character(s) were used in the file.

You also don't need the whole open for access thing when just reading files, so your script can be reduced to just

set listOfShows to {}
set Shows to paragraphs of (read POSIX file "/Users/anders/Desktop/test.txt")
repeat with nextLine in Shows
    if length of nextLine is greater than 0 then
        copy nextLine to the end of listOfShows
    end if
end repeat
choose from list listOfShows

Problem

I tried to create an AppleScript that reads a text file and puts the contents into a list. The file is a simple text file where every line looks like this: `example-"example"` The first is a filename and the other is a folder name. Here is my code now: ``` set listOfShows to {} set theFile to readFile("/Users/anders/Desktop/test.txt") set Shows to read theFile using delimiter return repeat with nextLine in Shows if length of nextLine is greater than 0 then copy nextLine to the end of listOfShows end if end repeat choose from list listOfShows on readFile(unixPath) set foo to (open for access (POSIX file unixPath)) set txt to (read foo for (get eof foo)) close access foo return txt end readFile ``` When I run that the output I get this: ``` error "Can not change \"Game.of.Thrones-\\\"Game Of \" to type file." number -1700 from "Game.of.Thrones-\"Game Of " to file" ``` My list looks like this: Game.of.Thrones-"Game Of Thrones" and two more lines like that.

Original source