The FooDoo Lounge

Basic File I/O

Fast and reliable handlers for reading from and writing to text files. More or less standard. Should work under any version of AppleScript. All parameters are strings.

No coercions are performed, so things other than text can be written & read, but I've only tested them with strings. Some versions of AS are buggy with other data types - like lists - so use them with caution. This also means that pathString must be a string, not an alias or file specification.

Read a file

Read the data fork of a file - a simple read.

USAGE: set fileContents to simpleFileRead from "Mac HD:Desktop Folder:myTestFile"
        --> anything - whatever is in the file

Input: string - full path to an existing file.
Output: string - the contents of the file's data fork.
Error: passes the error if it can't successfully read the file for any reason.

-- simpleRead -- by Richard Morton 2001
-- Read a file, returning its contents. Pass a valid path string.
to simpleRead from pathString
    try
        set ofaNum to open for access file pathString
        set fileData to read ofaNum
        close access ofaNum
    on error errMsg number errNum
        try
            close access file pathString
        on error
        end try
        error errMsg number errNum
    end try
    return fileData
end simpleRead

Create or overwrite a file

Write a string to a text file, creating if required & overwriting an existing file, if found. Pass a path string and the string (text) to write.

USAGE: overWriteFile at "Mac HD:Desktop Folder:myTestFile" from "Hello World"
        --> "Mac HD:Desktop Folder:myTestFile"

Input:
        pathString string - full valid path including legal file name.
        writeString string - some other data types are possible, none guaranteed.
Output: string (full path to the file) - data will be written to the specified file which will be created if required, overwritten if existing.
Error: passes the error if it can't successfully write a file for any reason.

-- overWriteFile -- by Richard Morton 2001
-- Write to a file, overwriting existing contents, creating file
-- if required. Pass a valid path string and the string to write.
to overWriteFile at pathString from writeString
    try
        set ofaNum to open for access file pathString with write permission
        set eof of ofaNum to 0
        write writeString to ofaNum
        close access ofaNum
        return pathString
    on error errMsg number errNum
        try
            close access file pathString
        on error
        end try
        error errMsg number errNum
    end try
end overWriteFile

Create or append to a file

Write a string to a text file, creating it if required & appending to the contents of an existing file, if found. Pass a valid path string and the string (text) to write.

USAGE: As above

Input:
        pathString string - full valid path including legal file name.
        writeString string - some other data types are possible, none guaranteed.
Output: string (full path to the file) - data will be written to the specified file which will be created if required, appended to if existing.
Error: passes the error if it can't successfully write a file for any reason.

-- writeWithAppend -- by Richard Morton 2001
-- Write to a file, appending to existing contents, creating file
-- if required. Pass a valid path string and the string to write.
to writeWithAppend at pathString from writeString
    try
        set ofaNum to open for access file pathString with write permission
        write writeString to ofaNum starting at eof
        close access ofaNum
        return pathString
    on error errMsg number errNum
        try
            close access file pathString
        on error
        end try
        error errMsg number errNum
    end try
end writeWithAppend

Read a chunk from the beginning of a file

Reads the first n bytes of a file. Use it for making sure you have the right file. Pass a path string to an existing file & the number of bytes to read.

USAGE: set fileHeader to readFirstBytes from "Mac HD:Desktop Folder:myTestFile" for 5
        --> string - the first 5 bytes of the file

Input:
        pathString string - full path to an existing file.
        numBytes integer - the number of bytes to read from the beginning of the file.
Output: string - the bytes that were read from the file's data fork.
Error: passes the error if it can't successfully read the file for any reason.

-- readFirstBytes -- by Richard Morton 2001
-- Read the first n bytes of a file. Use to identify files. Pass a
-- path string to an existing file and the number of bytes to read.
on readFirstBytes from pathString for numBytes
    try
        set ofaNum to open for access file pathString
        set fileData to read ofaNum for numBytes
        close access ofaNum
    on error errMsg number errNum
        try
            close access file pathString
        on error
        end try
        error errMsg number errNum
    end try
    return fileData
end readFirstBytes

 

The FooDoo Lounge is Copyright © Richard Morton 2002-2005

|| url: http://www.foodoo.sunreal.com.au/code/file_io.html
|| created: 4-Aug-03, 10:11 PM; updated: 4-Aug-03, 11:55 AM
|| size: 33237 bytes