How to use UTF-8 encoded command in AppleScript's do shell script on OS X 10.4

applescript, macos, shell

Solution

No problem on OS X 10.5

The `osascript`command on OS X 10.4 doesn't handle UTF-8 encoded text correctly when the string contain extended ASCII character.

Solution : Use the `MacRoman` encoding, `osascript` handle it without problem.

set myScriptAsString to quoted form of "do shell script \"touch /ü\""
do shell script "tString=$(echo " & myScriptAsString & "| iconv -t MACROMAN -f UTF8-MAC); osascript -e \"$tString\" > /dev/null 2> /dev/null & "

Problem

I have a program that generates a shell script and runs it via apple script. My output is UTF-8 encoded and I can't change that. On OS X 10.6 and above that works fine. On OS X 10.4 (Tiger) my script fails as soon as the file names /paths I need to process contain diacritical marks like é,à,Ü etc. (I don't have a 10.5 to try with so I don't know about that one yet) I assume that this is a problem with the locale. ``` do shell script "locale" ``` gives me: ``` "LANG= LC_COLLATE=\"C\" LC_CTYPE=\"C\" LC_MESSAGES=\"C\" LC_MONETARY=\"C\" LC_NUMERIC=\"C\" LC_TIME=\"C\" LC_ALL=" ``` so I figured that starting my script with: ``` export LC_ALL=en_US.UTF-8; export LANG=en_US.UTF-8; ``` might help but it turn out that it still does not work. What else can I do to make the utf-8 encoded shell script work on OS X 10.4 can I make UTF-8 work in some other way or can I convert it to the default character encoding the system uses? EDIT: It turned out that starting with the export statements I had thought of: ``` export LC_ALL=en_US.UTF-8; export LANG=en_US.UTF-8; ``` does answer my original question. This works: ``` do shell script " export LC_ALL=en_US.UTF-8; export LANG=en_US.UTF-8; touch /ü" ``` My problem turned out to be that I need to use osascript to be able to immediately return the shell to the process that spawns it like this: ``` set myScriptAsString to "do shell script \" export LC_ALL=en_US.UTF-8; export LANG=en_US.UTF-8; touch /ü\"" do shell script "export LC_ALL=en_US.UTF-8; export LANG=en_US.UTF-8; osascript -e " & quoted form of myScriptAsString & " > /dev/null 2> /dev/null & " ``` and no matter where I put the exports there it does not work with osascript...

Original source