and it works. But it breaks if I use a character outside the Mac Roman encoding. For instance, this
set myData to "Dziękuję proszę pana" as Unicode text
breaks as soon as it is compiled, which makes me think the problem is AppleScript’s handling of Unicode text. On the other hand, this
set myData to «data utxt0044007A00690119006B0075006A0119002000700072006F0073007A0119002000700061006E0061» as Unicode text
do shell script "echo " & quoted form of myData
works just fine.
So what can I do to get around this problem? (I’m using Mac OS X v10.3.9, AppleScript v1.9.3.)
I’m afraid that the answer might be to upgrade to Tiger. Your “broken” script both compiles and works properly in OS X 10.4.6, AppleScript 1.10.6, when I tested it. Otherwise you might have to convert it all to data since that works in both Tiger & Panther.
The issue is that you can’t compile real Unicode text in a script editor (ie, in a Script Editor window), but only text in the primary encoding on your OS (ie, MacRoman). No issues at run-time, though. You can hold in memory whatever supported applescript type.
Anyway, if you think you need such Unicode text at compile-time, you can do two things: the one you did or store it statically in a property (ie, “property x : the clipboard”). If you chose the first way, you may find useful this recent tip at Code Exchange: http://bbs.applescript.net/viewtopic.php?id=16638
The compile problem is only in the example. In the actual script, data is obtained from iTunes, thus:
tell application "iTunes"
set myData to (name of item 1 of selection) as Unicode text
end tell
do shell script "echo " & quoted form of myData
If the title of the track selected in iTunes contains a character outside Mac Roman, AppleScript returns an error (“Can’t make [track title] into a string.”] But if, instead of sending the Unicode text to do shell script, I write it to a file, then it works. It also works if myData is data. So it seems that the problem is that, if the variable contains Unicode text, AppleScript attempts to coerce it to a string before sending it to do shell script, whereas if the variable contains data, it’s sent to do shell script as it is.
That’s interesting, but it doesn’t work for me. The convertor does its job, but the result is treated by AppleScript as a string. I don’t know how to make it accept it as data.