Write to file results in 'Binary Computer Graphics Metafile'

Hi all,
We ran an applescript program on Tiger today and noticed the exported file has strange encoding. The code we use is:

set fd to open for access file (((POSIX file OutputFolder) as string) & ":tmp" as string) with write permission
set eof of fd to 0
write theFileContents to fd
close access fd

The strings we write are on both computers exactly the same but the results differ.
Running the shell command ‘file’ gives for the 10.4 file: ‘binary Computer Graphics Metafile’
and for the 10.3.9 file: ‘ASCII text, with CR line terminators’
I assume this results in UTF16 encoding on OSX 10.4 and UTF8 on OSX10.3.9 because the 10.4 file is almost twice as big as could be expected.

Any ideas what we can do to make it run on (almost) all OSX systems?


I think some time ago the behaviour for “write” changed. Now, if you provide Unicode text as parameter, it will write the data as Unicode text -UTF16 without BOM- (though you didn’t specify such a thing, as some time ago, where data was writen as plain text by default → not UTF8).

So, you must simply specify “write (foo as text) to blah” or “write foo to blah as unicode text”, then later “read blah” (as text, by default), or “read blah as unicode text” (if you wrote previously “as unicode text”). This should ensure the same behaviour in all machines.

That doesn’t mean much - since there’s no name extension and no file type, Finder’s just guessing what it is.

The ‘write’ command’s behaviour changed in 10.4. To ensure scripts are portable across OS versions, always specify the format:

write txt to f as string
write txt to f as «class utf8»
write txt to f as Unicode text

You might also consider adding a byte order mark to UTF16LE and/or UTF8- encoded files; Unicode-aware text editors such as TextEdit will often sniff for these when deciding how to open a file. If you don’t know much about text encodings, it’s worth doing some background reading, e.g.:


There’s also some useful info on how AS handles text encodings in the documentation for TextCommands.


I’m LMAO reading this article. I wish everything was this fun to learn.