I write Applescript to parse the iOS localization file (/en.lproj/Localizable.strings), translate the values and output the translation (/fr.lproj/Localizable.strings) to disk in UTF-16 (Unicode).
For some reason, the generated file has extra space between each letter. After some digging, I found the cause of the problem in Learn AppleScript: The Complete Scripting Guide.
“If you accidentally read a UTF-16 file as MacRoman, the resulting value may look at a glance like a regular line, especially if it contains English text. You will quickly find that something is very wrong when you try to use it, however : A common symptom is that every visible character in your “string” seems to have an invisible character in front of it, for example, reading a UTF-16 encoded text file containing the phrase “Hello World! "Because a line creates a line like" H ello W orld! "where each" "is actually an invisible ASCII 0".
So, for example, my English localization string file has:
"Yes" = "Yes";
And the generated French localization string file has:
" Y e s " = " O u i " ;
Here is my createFile method:
on createFile(fileFolder, fileName)
tell application "Finder"
if (exists file fileName of folder fileFolder) then
set the fileAccess to open for access file fileName of folder fileFolder with write permission
set eof of fileAccess to 0
write ((ASCII character 254) & (ASCII character 255)) to fileAccess starting at 0
close access the fileAccess
else
set the filePath to make new file at fileFolder with properties {name:fileName}
set the fileAccess to open for access file fileName of folder fileFolder with write permission
write ((ASCII character 254) & (ASCII character 255)) to fileAccess starting at 0
close access the fileAccess
end if
return file fileName of folder fileFolder as text
end tell
end createFile
And here is my writeFile method:
on writeFile(filePath, newLine)
tell application "Finder"
try
set targetFileAccess to open for access file filePath with write permission
write newLine to targetFileAccess as Unicode text starting at eof
close access the targetFileAccess
return true
on error
try
close access file filePath
end try
return false
end try
end tell
end writeFile
Any idea what I'm doing wrong?