Convert a plain text file to a utf-16 (aka “Unicode text”) file and back.
The “textfile2utf16” writes a BOM header to the file (two bytes talking about the format of the file), where you can choose between “BE” (Big Endian byte-order, default in MacOs) and “LE” (Little Endian, default in Win).
OS version: OS X
(*
SAMPLE USAGE
--> text file to UTF-16 (aka "Unicode text") with Big Endian byte-order (default in Mac OS X)
textfile2utf16(choose file, "BE")
--> text file to UTF-16 with Little Endian byte-order (default in Windozee)
textfile2utf16(choose file, "LE")
--> UTF-16 file to text
utf16file2text(choose file)
*)
to textfile2utf16(theFile, BOM)
set oldContents to (read theFile)
set f to (open for access theFile with write permission)
set eof of f to 0
--> if BOM, write allways BE. If needed, it will be swapped later
if BOM ? "" then write (ASCII number 254) & (ASCII number 255) to f
write oldContents to f as Unicode text starting at eof
close access f
--> if needed, reverse byte order, including BOM
if BOM = "LE" then my reverseByteOrder(theFile)
end textfile2utf16
to utf16file2text(f)
set oldContents to (read f as Unicode text)
set f to (open for access f with write permission)
set eof of f to 0
write oldContents to f as string
close access f
end utf16file2text
to reverseByteOrder(f)
do shell script "dd conv=swab < " & quoted form of POSIX path of f & " > /tmp/utf16stuff"
--> do not use "mv", since we must mantain res-fork and metadata
set x to (read ("/tmp/utf16stuff" as POSIX file))
set f to (open for access f with write permission)
set eof of f to 0
write x to f
close access f
end reverseByteOrder
property |version| : 1.0
property author : "Pescados Software · [url=http://homepage.mac.com/julifos/soft"]http://homepage.mac.com/julifos/soft"[/url]
property |date| : date "domingo, 21 marzo 2004 17:46:58"
property license : "freeware, open-source"