Applescript and textencoding

The applescript shown below imports contacts from Contacts.app; it imports all the contacts all right except that if any string inside a contact is not in roman script (for instance, in greek ΣΥΧΩ) I get several question marks.

So the text must be ancoded. I searched a lot on the topic, and the only two snippet I thought could solve the issue are these: but trying each of the two I was not succesfull, i.e. the question marks remain. Maybe somebody may figure out how to deal with the problem.

At the bottom the two zipped files are available. By the way, if running the script the output file is empty, rename the applescript and it should work.

  1. set encoding to “utf16le”
  2. do shell script "textutil -convert txt -encoding UTF-8 " & quoted form of POSIX path of (myBackupName as text)

applescript begins:

set myBackupName to “AllContacts.vcf”

set myBackupPath to (path to desktop folder as string) & myBackupName

tell application “Contacts”

**set** myBackupFile **to** **open for access** *file* myBackupPath **with** write permission

**repeat** **with** aContact **in** *people*

	**write** (vcard **of** aContact **as** *text*) to myBackupFile

**end** **repeat**

**close access** myBackupFile

end tell

tell application “Contacts” to quit

encodings.zip (1.5 KB) refers to the two lines above the script itself.

Contacts copy.scpt.zip (2.6 KB)

added greek specimen word

I ran your script against my contacts, one of which has the character “ö” in the last name. The resulting file appears to be in the MacRoman text encoding, which is a very legacy artifact from the early days of AppleScript.

MacRoman has good coverage for most western Latin scripts, but it won’t cover multi-byte scripts like Chinese, Korean, and others, and it doesn’t have complete coverage for some single-byte scripts such as Russian. UTF8 is a much better option, as you’ve discovered.

It’s a bit of a hack, but try changing your script to read:

write (vcard of aContact as «class utf8») to myBackupFile

That should cause AppleScript to write the text as UTF8 and make your life a whole lot easier. :slight_smile:

unfortunatelly I still get the question marks. The character “ö” usually get resolved converting the macRoman output file to utf8.

I think that you too, Inserting this greek word ΣΥΧΩ into a contact, will get question marks.

Thank you for answering and have a happy new year.

Sorry about that, I put it in the wrong place. This works:

write (vcard of aContact as text) to myBackupFile as «class utf8»

Eric, on the firdst day of the year you are already man of the year. Thanks