How to create and write a UTF-16 text file using Applescript?

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
            --write «data rdatFEFF» 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
            --write «data rdatFEFF» 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?

+3
2

, UTF16. "create file". , . "appendText" true false. False , true . , .

on writeTo_UTF16(targetFile, theText, appendText)
    try
        set targetFile to targetFile as text
        set openFile to open for access file targetFile with write permission
        if appendText is false then
            set eof of openFile to 0
            write (ASCII character 254) & (ASCII character 255) to openFile starting at eof -- UTF-16 BOM
        else
            tell application "Finder" to set fileExists to exists file targetFile
            if fileExists is false then
                set eof of openFile to 0
                write (ASCII character 254) & (ASCII character 255) to openFile starting at eof -- UTF-16 BOM
            end if
        end if
        write theText to openFile starting at eof as Unicode text
        close access openFile
        return true
    on error theError
        try
            close access file targetFile
        end try
        return theError
    end try
end writeTo_UTF16

on readFrom_UTF16(targetFile)
    try
        set targetFile to targetFile as text
        targetFile as alias -- if file doesn't exist then you get an error
        set openFile to open for access file targetFile
        set theText to read openFile as Unicode text
        close access openFile
        return theText
    on error
        try
            close access file targetFile
        end try
        return false
    end try
end readFrom_UTF16
0

, , , "( thru j someText) " - " [1]. , , . ( ) : 'text thru j of someText' (p179-181).

OTOH, [2], , , UTF16 MacRoman . , Unicode .


[1] p179 , , , . [3]

[2] IIRC p501 , .. "⃞H⃞e⃞l⃞l⃞o", "H e l l o", , . [3]

[3] Apress.

0

Source: https://habr.com/ru/post/1791756/


All Articles