-
AppleScript reads and writes numbers in binary format (short and long integers, single and double precision reals), so you can use AppleScript for data input/output.
-
To read text from ASCII files and to write text into ASCII files, use the basic set of AppleScript file commands: open for access, close access, get/set eof, read, and write.
For information on AppleScript's file commands, select File > Open dictionary > Scripting Additions > StandardAdditions.osax, then in the window select Index > File Read/Write.
set f to alias ((path to "docs" as text) & "myTextFile.txt")
set s1 to "whatever "
set s2 to "1 2 3 4"
write s1 to f -- write from the beginning of the file
write s2 starting at (1 + (get eof f)) to f -- write at the end of the file
set s to read f
-- "whatever 1 2 3 4"
-
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}
-
To create a new file use open for access [...] with write permission. A call to open for access has to be balanced with a call to close access.
set f to ((path to "docs" as text) & "myTextFile.txt")
set nref to open for access file f with write permission -- this creates an empty file
close access nref
write "whatever" to file f
-
To overwrite an existing file, reset its EOF (the eof of file marker) with the set eof command.
set f to ((path to "docs" as text) & "myTextFile.txt")
set nref to open for access file f with write permission
set eof nref to 0
write "whatever" to file f
close access nref
-
To write numbers to ASCII in fixed format use the format command. In the example below up to 5 digits are printed on the left of the decimal separator, 3 digits are printed on the right. Leading zeros are replaced with white spaces, trailing zeros are printed.
format 16/5 into "^^^^^.000"
-- "␣␣␣␣3.200"
-
To make a number into an ASCII string in a scientific format, use cformat or printf. The printf command formats a sequence of arguments and inserts them in text. Its behavior is similar to the printf command in C.
printf "%s is %d years old. He is %1.2fm tall." parameters {"Fred", 15, 1.7}
-- "Fred is 15 years old. He is 1.70m tall."
-
To read or write sequentially a file, encapsulate read and write commands within open for access [...] close for access. Within such a wrapper, refer to the file with the reference number returned by open for access rather than with the file path.
set f to ((path to "docs" as text) & "myTextFile.txt")
set nref to open for access file f with write permission
set s1 to read nref until " " -- read until first space character
set s2 to read nref -- read the rest of the file
close access nref
|