Read a file without opening

I know there’s a simple way but I’m not familiar with the details. (File is plain text format).

Do you mean you want to avoid having to script a particular application (like TextEdit), or do you mean you want to avoid using open for access?

Either way, read from StandardAdditions should do what you need. It can be used with or without open for access, and because it is part of StandardAdditions, your script would not need to get any applications involved.

set anAlias to choose file

set allLines to read anAlias
set anAlias to choose file

set fileref to open for access anAlias
try -- use error handling to make sure the file ref is always closed
	set allLines to read fileref
	set firstLine to read fileref from 0 before ASCII character 10
	set secondLine to read fileref before ASCII character 10
	close access fileref
on error m number n from o partial result r to t
	close access fileref
	error m number n from o partial result r to t
end try
{firstLine, secondLine, allLines}

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari Version 3.1 (4525.13)
Operating System: Mac OS X (10.4)

Thanks. Can you write without using open for access?

No. However, you might find this helpful:

Thanks, but I’m a noob so I have no idea how that works.

Is there any way to ADD text to a text file in the same manner??

To add text to a file you can either use the shell command ‘cat’ or you could used the write command from standard editions and append to the end of the text file as illustrated in Bruce’s write_to_file routine that is linked earlier.

Or you could do this:

set myData to (read alias ([insert path to myData.txt here]) & [insert new data here])
set myFile to "myData.txt"
set myFilePath to path to desktop as Unicode text
set fileref to open for access myFilePath & myFile with write permission
   set eof of fileref to 0 -- this erases it, don't do it to append
   write myData to fileref
   close access fileref
on error err -- this is to assure that you always close the file.
   close access fileref
   display dialog err
end try

It’s a modified version of Adam Bell’s script.


I apologize but it is not necessary to use open for access to be able to write in a text file.

set dossierLocal to path to documents folder as Unicode text
set nomDoc to "just_a_test.txt"
set documentLocal to dossierLocal & nomDoc
tell application "System Events"
	if exists file documentLocal then delete file documentLocal
	make new file at end of folder dossierLocal with properties {name:nomDoc}
end tell -- System Events
write "I'm a poor lonesome cowboy!" as «class utf8» to file (documentLocal)
write return & "I'm really lonesome" as «class utf8» to file (documentLocal) starting at eof

Yvan KOENIG (from FRANCE jeudi 17 avril 2008 18:14:53)


What are the advantages of not opening for access?

I would imagine that if you do not have the file ‘opened for access’ than you could run into a problem if multiple sources tried to access/manipulate the file in question.

So in terms of advantages I can’t really think of a concrete one, but I would imagine that actually opening for access would be considered best practice.



The more evident advantage is that we are sure that the file will not remain in “open” state after some failure.
This odd state is quite often described by users which don’t know how to reset a clean status.

The main advantage of “open for access” is that when Peter as open a file this way, Paul is unable to open it getting rid of possible conflicts.

For a single user, I see no reason to use the “open for access” protocol.

Yvan KOENIG (from FRANCE lundi 21 avril 2008 09:17:15)

I forgot to say that here, the thread was open with the title: “Read a file without opening:wink:

It’s a little more complicated than that. :slight_smile:

If you can bear my dense writing style, the first part of my article in unScripted goes into a lot of detail.