gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:47:03 24 4
gpt4 key购买 nike

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

出于某种原因,生成的文件在每个字母之间都有一个额外的空格。经过一番挖掘,我在 Learn AppleScript: The Comprehensive Guide to Scripting 中找到了问题的原因。

"If you accidently read a UTF-16 file as MacRoman, the resulting value may look at first glance like an ordinary string, especially if it contains English text. You'll quickly discover that something is very wrong when you try to use it, however: a common symptom is that each 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!" as a string produces a string like " H e l l o W o r l d ! ", where each " " is really an invisible ASCII 0 character."

例如我的英文本地化字符串文件有:

"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

知道我做错了什么吗?

最佳答案

这是我用来读写 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

关于ios - 如何使用 Applescript 创建和写入 UTF-16 文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4981598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com