如何使用AppleScript重命名文件时保留特殊字符

我有一个AppleScript文件来replace通过CSV文件提供的文件名。 虽然我已经得到了脚本的工作,但它与string/文件名的编码有问题。

文件的发现完美。 重命名为像Malmö这样的结果是一个非常奇怪的编码string。

CSV来自Microsoft Excel,我怀疑是不正确的UTF-8编码。 而现在我陷入了如何正确处理编码。 (或者如何转换编码)。 据我所知它有默认的Excel编码ISO 8859-1。

set theFile to (choose file with prompt "Select the CSV file") set thePath to (choose folder with prompt "Select directory") as string set theCSVData to paragraphs of ((read theFile)) set {oldTID, my text item delimiters} to {my text item delimiters, ";"} repeat with thisLine in theCSVData set {oldFileName, newFileName} to text items of thisLine if length of oldFileName > 0 then set oldFile to thePath & oldFileName set newFile to newFileName tell application "System Events" if exists file oldFile then set name of file oldFile to newFile end if end tell end if end repeat 

所以我的问题是,如何解决编码问题,或者通过正确读取或通过编码文件(通过苹果脚本)

我真的认为文​​件名使用utf16LE编码,但我可能是错的。 无论如何,有一个你可以通过terminal访问的实用程序,名为iconv (man iconv),你可以尝试,也许重新编码的文件名。 如果您以文本或unicode文本的forms接收到do shell script的输出,那么您将返回utf16,因此在转换后它不会被破坏。 (在你使用它作为文件名之前)。

使用:

 set theCSVData to paragraphs of ((read theFile as «class utf8»))