Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Open for access "end of file" error

I just want to see if the text of a file contains "My Magic Word". I don't need to write to the file, just read it.

Right now I have the following

*set OpenAccess to (open for access "~/Desktop/myfile.txt")*
*set theFileContents to (read OpenAccess)*
*close access OpenAccess*

theFileContents

Every time I open the file for the second time, I get an "end of file" error. I tried (set eof of OpenAccess to 0) but it says it only works with write privileges.

People all over the Net have had this question. So far, the only way that I can get the read marker back to 0 is by opening the file manually, typing a space at the beginning, erasing it, and then re-saving the file. The other way is to log out and log back in to my username. close access OpenAccess doesn't seem to reset the marker back to position 0.

Also, is there a way to recognize whether the user activated the app or the LogIn Items activated it? This could sure could simplify things.

Message was edited by: Joey Delli Gatti

Message was edited by: Joey Delli Gatti

PowerBook G4 1.67 Ghz, Mac OS X (10.5.5)

Posted on Dec 4, 2008 9:09 PM

Reply
6 replies

Dec 4, 2008 10:11 PM in response to Joey Delli Gatti

I don't need to write to the file, just read it.


Then why are you bothering to open the file at all.

The only reasons to use open for access are to either write to the file, or to process the file in pieces (e.g. read a chunk of data at a time). If all you want is to read the entire file, just use read file:

set theFileContents to (read file ((path to desktop as text) & "myfile.txt))


If you do want to open/read in parts/close, use read's from parameter:

set OpenAccess to open for access file ((path to desktop as text) & "myfile.txt)
set theFileContents to read OpenAccess from 0
close access OpenAccess

Dec 4, 2008 10:17 PM in response to Joey Delli Gatti

The *path to desktop* only works for files on the desktop, or if the desktop is root folder for a file. There are similar terms available for other common locations. If you open Script Editor, and look under File in the contextual menu, you will see *Open Dictionary...* Click that, and open the dictionary for StandardAdditions, and look under the *File Commands* section, and you will see the entry for *path to*. In there are all sorts of shortcut terms for different paths.

Dec 4, 2008 11:30 PM in response to Camelot

Thank you. I was using the other approach because it was the only one I knew. I used it before in another script that I wrote, which created a preference file or changed the file. Thanks. If you have any good ideas on where to get a good AppleScript book or PDF manual or something, I'd appreciate it. Everything I know is from scouring these discussion forums and some other developer sites.

Do you know of a way for a script to know if it was started by the user or the login items?

Open for access "end of file" error

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.