Satimage Previous
Reading and writing data in files with AppleScript
Home Documentation Smile Computing AppleScript maths Reading and writing data in files with AppleScript  
If you would like to experiment with the snippets below, you may first run once for all the following block, which will create a temporary file f where the snippets will work. For brevity, the snippets are sequential: a given one may assume you have run the ones above.

To import the whole set of examples presented in this page, click the link below.
Import all scripts

set f to (path to "temp" from user domain as text) & (make new name)
open for access file f with write permission
close access f
set f to f as alias
  • AppleScript can write into a file all the data types that it can handle, using the write command. To open the dictionary for write, select the term and press ⇧⌘F.
    set ref_num to open for access f with write permission
    set eof of ref_num to 0
    write 2.67133413610864E+185 to ref_num
    close access ref_num
  • By default the read command reads the file as ASCII, one byte into one character.
    read f
      --  "foo bar?"
  • To read a quantity which is not a string, you specify a data type with as when reading the file.
    read f as real
      --  2.67133413610864E+185
  • If you specify a data type with a fixed length, which is the case for all the numerical data types, by default read will attempt to read the whole file and return a list of values rather than the first one only.
    set the_int to 3 * 65536 + 2
    set ref_num to open for access f with write permission
    set eof of ref_num to 0
    write the_int to ref_num
    write 2 * the_int to ref_num
    write 3 * the_int to ref_num
    write 4 * the_int to ref_num
    close access ref_num

    read f as integer
      --  {196610, 393220, 589830, 786440}
  • The as parameter of the read command supports additional types, that you can use - in addition to integer and real - to read various data block sizes and explore a file which was written using an unknown format, block alignment, or unknown padding blocks. Here are all the numerical data types that you may want to use.
    small integer (2 bytes)
    read f as small integer
      --  {3, 2, 6, 4, 9, 6, 12, 8}
    integer (4 bytes)
    read f as integer
      --  {196610, 393220, 589830, 786440}
    double integer (8 bytes)
    read f as double integer
      --  8.4443352045978E+14

    Note: in some versions of AppleScript, as double integer does not return a list, because of a bug.
    small real (4 bytes)
    read f as small real
      --  {2.75509291070902E-40, 5.51018582141805E-40, 8.26527873212707E-40, 1.10203716428361E-39}
    real (8 bytes)
    read f as real
      --  {4.17205592655959E-309, 1.2516167777736E-308}

    You can use these additional types to write to a file. For instance, you can write double integers (8 bytes).
    set ref_num to open for access f with write permission
    set eof of ref_num to 0
    write (2 ^ 63 + 2 ^ 30) to ref_num as double integer
    close access ref_num

    read f as small integer
      --  {-32768, 0, 16384, 0}
  • AppleScript writes real numbers in binary format, as two possible data types.
    real (8 bytes) (the default)
    small real (4 bytes)
    set the_real to pi
    set ref_num to open for access f with write permission
    set eof of ref_num to 0
    repeat with i from 0 to 3
        write i * pi to ref_num as small real
    end repeat
    close access ref_num

    (read binary f as real) as list of real
      --  {5.32864626443882E-315, 1.28317129238601E+4}
    (read binary f as small real) as list of real
      --  {0.0, 3.141592741013, 6.283185482025, 9.424777984619}
Version française
Copyright ©2008 Paris, Satimage