osascript, shell scripts and Unicode

Written . Tagged AppleScript, OS X, Shell scripting.

I spent some time in encoding hell the other day, trying to read a UTF-8 encoded text file with osascript in a bash shell script.

Despite its preference for the MacRoman encoding, AppleScript does handle Unicode, but ironically, to get that to work, I had to include the string “«class utf8»” – the chevrons in which must of course be encoded just right.

After much pain, I came to realize it’d be best to just create a separate .scpt file for the AppleScript, encoded in its native MacRoman, and call that from the shell script, i.e.

1
osascript saviour.scpt

Before that moment of zen, I did this, which also works but is rumoured to kill kittens:

1
2
DIE_ENCODINGS_DIE=$(printf 'set file_contents to read (POSIX file "/tmp/some.file")  as \307class utf8\310')
osascript -e "$DIE_ENCODINGS_DIE" -e "-- Do something with file_contents"