Various past posts have recommended using osascript as a way to
invoke AppleScript
from Java. I have used this technique many times with apparent
success. However, I just
discovered a problem: conflicting text encodings when passing command
line arguments.
The upshot is that many of the simple examples shown in past posts
will fail when used
with a filename containing non-ASCII characters.
The problem: When using Runtime.exec, Java encodes command line
arguments using UTF-8. When AppleScript decodes command line arguments, it
uses MacRoman. If your command line argument contains non-ASCII characters,
it will probably be garbled. (The same is true for environment variables.)
I am not aware of any way to alter the encoding used
either by Java or AppleScript on this path. Does anyone know of one?
The only workaround I know of is to pass text (including file names)
in temporary files,
which allows the encoding to be controlled at both ends. For details
on the AppleScript
end, see: