Re: File I/O
Re: File I/O
- Subject: Re: File I/O
- From: "Serge Belleudy-d'Espinose" <email@hidden>
- Date: Thu, 10 Jan 2002 01:49:14 +0100
At 15:39 -0800 8/01/02, Stephen wrote:
>
I am relatively new to AppleScript and need some
>
simple ways to just read and write strings to and from
>
text files.
All the commands for reading/writing files are in the Standard Addition osax. You can get their syntax with you favorite script editor by using its 'open dicitionnary' command in the file menu and choosing Standard Addition in the Scripting Additions in the system folder.
You will need six commands:
open
close
read
write
get eof
set eof
You will start by using open:
set myFile to open file "MyDisk:MyFolder:MyFile"
'Open' returns the value you will need to access the file and later close it, this is why you store the result to myFile.
If you want to be able to write to the file, use the optional parameter 'with write permission'
set myFile to open file "MyDisk:MyFolder:MyFile" with write permission
When you're done with the file, you need to close it:
close access myFile
See? now you're actually using the myFile variable you declared at open.
So, what will you do with your file? You can read it, and obviously you need to store the result of 'read' in some variable. If your file is small, you can simply get all its content in a single step:
set myContent to read myFile
This will get you the whole file as a string, including return characters between lines. There are several ways to apply a specific range to your reading, but they are beyond the scope of a simple starter. Just an example:
set myContent to read myFile until return
will get you the content of the first line without the final return. Note that it will error if there's actually no line - highly unlikely, but you never know.
If you want to write to your file, provided you opened it with write permission, just use write:
write myString to myFile
Where in the file myString will be written depends on the value of the end of the file, which you can get and set using eof:
get eof of myFile
will get you the position at the end of the file. If you specify nothing, the next write will append to the end; you can however modify the value of eof. For example, setting eof to 0 means will overwrite your file:
set eof of myFile to 0
etc.
HTH,
Serge
--
\\//\//\// Serge Belleudy-d'Espinose Institut Jacques Monod
// // //
http://www.ijm.jussieu.fr/ Universite Paris VII - Jussieu
//\//\//\\
References: | |
| >File I/O (From: Stephen <email@hidden>) |