Applescript: leggere un file UTF-8
Scritto da Marco Balestra Wed, 22 Nov 2006 02:48:00 GMT
Mi sono trovato di recente nella necessità di dover lavorare pesantemente su file XML, ed in questo l'accoppiata del linguaggio XSLT con l'ottimo "TestXSLT" (di Marc Liyanage, www.etropy.ch) è fantastica, pilotando il tutto con Applescript.
Tutto bene finché leggevo file ISO-8859-1 ed avevo un output UTF-8 (che facevo salvare direttamente a TestXSLT), meno bene quando anche il file in lettura è stato un file UTF.
Purtroppo TestXSLT prevede il comando “set XML code to …” passando del testo, e non direttamente il path file, quindi l’UTF-8 va letto da AppleScript.
Partivo da questo:
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
Cercando ho trovato il modo di leggere anche file Unicode (UTF-16):
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
ed infine 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
HTH