Incredibly Frustrating Read File Issue...

ok so all i need to do is read a text file so that i can parse it with a little parser i have already written. problem is, the only way i can actually read the file is by doing a “choose file” command. but i have a big old list of these that i need to parse so the manual selection is not gonna fly. but i can’t for the life of me get the file to read any other way. here’s what i’ve got:

WORKS JUST FINE

set theFile to (choose file)
open for access theFile
set fileContents to (read theFile)
close access theFile

DOESN’T WORK AT ALL

set theFile to (path to desktop as Unicode text) & "SomeStupidFile.txt"
open for access theFile
set fileContents to (read theFile)
close access theFile

it keeps throwing me this error on the (readFile) part: Can’t make “Macintosh HD:Users:me:Desktop:SomeStupidFile.txt” into type file.

i’m stumped. anyone got any ideas? really appreciate it!

Hi,

choose file returns an alias, so the syntax

open for access theFile

works.
In your second example your have a path string, then you must use

open for access file theFile

or

open for access alias theFile

If you want only read a file without writing into it, open for access ist not necessary.
This is sufficient

set theFile to (choose file)
set fileContents to read theFile

Assuming your big old list of files is in a folder:

set F to choose folder
tell application "Finder" to try
	set BOL to files of entire contents of F as alias list
on error -- if there's only one file
	set BOL to files of entire contents of F as alias as list
end try

BOL is you big old list of aliases to the files in the chosen folder (even if they’re in internal folders).

awesome guys, thanks! i wound up going with:


set theFile to (path to desktop as Unicode text) & "textfile.txt" as alias
open for access theFile
set fileContents to read theFile
close access theFile

opening for access even though i don’t need to but might want to later. as for the batch, these files will always have consistent names so i’m just putting them all into a static list for now.

thanks again. :slight_smile: