Area de-PHPizzata  —  This area is free from PHP crapware

19 giugno 2009

Read a UTF-encoded file using Applescript

Recently I had to shuffle a lot with XML, using applescript together with “TestXSLT” by Marc Liyanage.
Everything was fine while I was reading ISO-8859-1 encoded files and outputting UTF-8 (directly saved by TestXSLT), while some trouble was getting out when the input file was Unicode.

Unfortunately TestXSLT only allows command “set XML code to…” passing the text of the XML, rather than file path, then we need to read correctly Unicode text from inside Applescript.

Here it is my original handler for text reading:

on readFile(filepath)
set foo to (open for access (POSIX file filepath))
set txt to (read foo for (get eof foo))
close access foo
return txt
end readFile


I had to change it slightly in order to read UTF-16 Unicode:

on readFile(filepath)
set foo to (open for access (POSIX file filepath))
set txt to (read foo for (get eof foo) as Unicode text)
close access foo
return txt
end readFile


And this is for UTF-8:

on readFile(filepath)
set foo to (open for access (POSIX file filepath))
set txt to (read foo for (get eof foo) as «class utf8»)
close access foo
return txt
end readFile

Etichette: ,

0 Commenti:

Posta un commento

Link a questo post:

Crea un link

<< Home page