如何使用Applescript创建和编写UTF-16文本文件?

时间:2011-02-13 00:26:16

标签: ios applescript

我正在编写 Applescript 来解析 iOS Localization 文件( /en.lproj/Localizable.strings ),翻译这些值并以 UTF-16(Unicode)编码将转换( /fr.lproj/Localizable.strings )输出到磁盘。

由于某种原因,生成的文件在每个字母之间都有一个额外的空格。经过一番挖掘,我在学习AppleScript:脚本综合指南中找到了问题的原因。

  

“如果您意外地阅读了UTF-16文件   作为MacRoman,结果值可能   乍一看就像一个普通人   字符串,特别是如果它包含   英文文本。你很快就会发现   你什么都不对   然而,尝试使用它:一个常见的   症状是每个可见的角色   在你的“字符串”似乎有一个   在它面前看不见的角色。   例如,读取UTF-16编码   包含短语“Hello”的文本文件   世界!“作为一个字符串产生一个字符串   比如“H e l l o W o r l d!”,其中   每个“”实际上是一个不可见的ASCII   0个字符。“

例如,我的英文本地化字符串文件包含:

"Yes" = "Yes";

生成的法语本地化字符串文件包含:

 " Y e s "  =  " O u i " ;

这是我的 createFile 方法:

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

这是我的 writeFile 方法:

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

知道我做错了吗?

2 个答案:

答案 0 :(得分:0)

这是我用来读写UTF16的处理程序。您不需要单独的“创建文件”处理程序。如果文件不存在,写处理程序将创建该文件。将“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

答案 1 :(得分:0)

如果你在每个角色之间得到实际的空格,你可能会在你的代码[1]中得到'(某些文字的j个字符)作为字符串'反模式。这会将字符串拆分为字符列表,然后将其强制转换为字符串,并在每个字符之间插入当前文本项分隔符。获得子字符串的正确(即快速和安全)方式是:'text i thru j of someText'(p179-181)。

OTOH,如果你在每个字符[2]之间得到不可见的字符,那么是的,这将是一个编码问题,通常使用MacRoman或其他单字节读取UTF16编码的文件编码。如果您的文件具有有效的字节顺序标记,则任何精通Unicode的文本编辑器都应使用正确的编码来读取它。


[1] p179指出这个成语是不安全的,但忘记提供它引起的问题的实际证明。 [3]

[2] IIRC p501上的例子意味着用矩形符号来表示不可见的字符,即“⃞H⃞e⃞l⃞l⃞o”而不是“H e l l o”,但不是那么出来所以可能被误读为意味着可见的空间。 [3]

[3]随意向Apress提交勘误表。