将非ascii字符写入xml/UTF-8
我有一个脚本,它通过字符串操作组装xml文档,这是我在发现xml套件之前编写的 当包含某些字符时,例如,-en破折号和-em破折号,我怀疑所有非ascii字符都会被unicode替换字符替换� U+FFFD将非ascii字符写入xml/UTF-8,xml,applescript,javascript-automation,Xml,Applescript,Javascript Automation,我有一个脚本,它通过字符串操作组装xml文档,这是我在发现xml套件之前编写的 当包含某些字符时,例如,-en破折号和-em破折号,我怀疑所有非ascii字符都会被unicode替换字符替换� U+FFFD 只有在文档开头有xml标题时才会发生这种情况:即在AppleScript中,您可以使用将文本作为«类utf8»写入文件来写入utf8编码的文本。在JXA中不能这样做,因为无法编写原始AE代码 我一般不推荐JXA,因为它是1。四轮马车和瘸子,还有2辆。被遗弃的。如果您总体上喜欢JavaScri
只有在文档开头有xml标题时才会发生这种情况:即在AppleScript中,您可以使用将文本作为«类utf8»写入文件来写入utf8编码的文本。在JXA中不能这样做,因为无法编写原始AE代码 我一般不推荐JXA,因为它是1。四轮马车和瘸子,还有2辆。被遗弃的。如果您总体上喜欢JavaScript,那么最好使用Node。对于应用程序自动化来说,你最好还是坚持使用AppleScript:虽然它是一种蹩脚的语言,而且已经奄奄一息,但至少它能正确地表达Apple事件,并且有相当不错的文档和社区支持
如果必须使用JXA,唯一的解决方法是通过Cocoa API编写UTF8文件。尽管通过字符串混搭生成XML是邪恶的,而且很容易出现错误,但您还是应该抓住机会重写代码以使用适当的XML API。同样,对于Node,您的选择被宠坏了,最困难的部分将是确定哪些NPM库是健壮的、易于使用的,哪些是垃圾库。在AS/JXA中,要么是系统事件的XML套件(速度较慢),要么是Cocoa的XML API(复杂)。给定AppleScript代码,应该为text1变量分配什么值来重现问题?假设它被设置为text1为Oops,是的!看来我错过了顶线!当我在Atom中显式地打开它并将编码设置为UTF-8时,它不会显示字符。这是否回答了您的问题?是的,这回答了问题!在Applescript中工作,在纯JXA中没有机会。我找到了更多我选择JXA的信息,因为我需要做额外的字符串操作,而applescript是如此迟钝。需要明确的是:我所做的是读取XML模板文件,然后用文本文件中的值替换占位符字符串。将来使用XMLAPI可能会更好,但目前它是可靠的
set dt to path to desktop as text
set filePath to dt & "test1.txt"
writeTextToFile(text1, filePath, true)
-- using the example handler from the Mac Automation Scripting Guide
on writeTextToFile(theText, theFile, overwriteExistingContent)
try
-- Convert the file to a string
set theFile to theFile as string
-- Open the file for writing
set theOpenedFile to open for access file theFile with write permission
-- Clear the file if content should be overwritten
if overwriteExistingContent is true then set eof of theOpenedFile to 0
-- Write the new content to the file
write theText to theOpenedFile starting at eof
-- Close the file
close access theOpenedFile
-- Return a boolean indicating that writing was successful
return true
-- Handle a write error
on error
-- Close the file
try
close access file theFile
end try
-- Return a boolean indicating that writing failed
return false
end try
end writeTextToFile
app.includeStandardAdditions = true
function writeTextToFile(text, file, overwriteExistingContent) {
try {
// Convert the file to a string
var fileString = file.toString()
// Open the file for writing
var openedFile = app.openForAccess(Path(fileString), { writePermission: true })
// Clear the file if content should be overwritten
if (overwriteExistingContent) {
app.setEof(openedFile, { to: 0 })
}
// Write the new content to the file
app.write(text, { to: openedFile, startingAt: app.getEof(openedFile) })
// Close the file
app.closeAccess(openedFile)
// Return a boolean indicating that writing was successful
return true
}
catch(error) {
try {
// Close the file
app.closeAccess(file)
}
catch(error) {
// Report the error is closing failed
console.log(`Couldn't close file: ${error}`)
}
// Return a boolean indicating that writing was successful
return false
}
}
var text = "<?xml £"
var file = Path("Users/benfrearson/Desktop/text.txt")
writeTextToFile (text, file, true)