The commands for working with binary files are read binary and write binary. To open the dictionary about these terms, select one and press ⇧⌘F.
The example below reads 10,000 values out of a file generated by a single-precision FORTRAN code.
set a to (read binary alias "myFile" as small real skip 4 length 10000)
Here we skip four bytes, assuming that FORTRAN - as it often does - bracketed data blocks with blocks of four null bytes.
When you write data to a binary file with write binary you can specify at what offset you want to write the data. write binary writes single-precision real numbers (4 bytes) so when you use read binary on data that you stored with SmileLab, do specify read binary [...] as small real.
To have write binary create the file use the file f construct.
set f to (path to "docs" as text) & "temp.temp"
write binary file f with data a
AppleScript supports two classes to refer to files, the file specification, e.g. file "Startup:Applications:Calculator", and the alias, e.g. alias "Startup:Applications:Calculator". An alias must refer to an existing file. Finder prefers aliases.
If you want to insert paddings of four null bytes, interleave the write binary commands with write 0 in f lines. write 0 in f writes four null bytes.
Above we have used read binary and write binary with file references. Instead you can use the reference number provided by AppleScript's built-in open for access command. If you issue many calls successively, using that reference number instead of the absolute file reference will speed up the file access.
set f to (path to "docs" as text) & "temp.temp"
set refNum to open for access (f as alias)
write binary refNum with data a
close access refNum
|