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

Question:

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

Reply
Question marked as Solved
Answer:
Answer:
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

Posted on

Question marked as Helpful

Dec 4, 2008 10:17 PM in response to Joey Delli Gatti 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.

There’s more to the conversation

Read all replies

Page content loaded

Dec 4, 2008 9:32 PM in response to Joey Delli Gatti In response to Joey Delli Gatti

Joey:

You are trying to use a unix file path instead of a path that AppleScript can understand. Try it this way:


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

Dec 4, 2008 9:32 PM

Reply Helpful

Dec 4, 2008 9:53 PM in response to casdvm In response to casdvm

Does the path to Desktop work ok with a file not on the Desktop? I still get an end of file error with this:

*set OpenAccess to open for access file ((path to desktop as text) & "loginwindow.plist")*
*set theFileContents to read OpenAccess*
*close access OpenAccess*

theFileContents

Dec 4, 2008 9:53 PM

Reply Helpful
Question marked as Solved

Dec 4, 2008 10:11 PM in response to Joey Delli Gatti 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:11 PM

Reply Helpful
Question marked as Helpful

Dec 4, 2008 10:17 PM in response to Joey Delli Gatti 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 10:17 PM

Reply Helpful (1)

Dec 4, 2008 11:30 PM in response to Camelot 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?

Dec 4, 2008 11:30 PM

Reply Helpful
User profile for user: Joey Delli Gatti

Question: Open for access "end of file" error