[英]How do I write a LZ compressed string to text file using JXA?
我正在尝试在Apple脚本编辑器中编写一个JXA脚本,它使用LZ算法压缩字符串并将其写入文本(JSON)文件:
var story = "Once upon a time in Silicon Valley..."
var storyC = LZString.compress(story)
var data_to_write = "{\x22test\x22\x20:\x20\x22"+storyC+"\x22}"
app.displayAlert(data_to_write)
var desktopString = app.pathTo("desktop").toString()
var file = `${desktopString}/test.json`
writeTextToFile(data_to_write, file, true)
除了将LZ压缩字符串转换为一组“?”之外,一切正常。 到达输出文件时, test.json 。
它应该看起来像:
{"test" : "㲃냆Њޱᐈ攀렒삶퓲ٔ쀛䳂䨀푖㢈Ӱນꀀ"}
相反,它看起来像:
{"test" : "????????????????????"}
我有一种感觉,转换发生在writeTextToFile()函数使用的app.write命令中(我从Apple的Mac自动化脚本编写指南中的示例中提取):
var app = Application.currentApplication()
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
}
}
是否有app.write的替代命令维护LZ压缩字符串/更好的方法来完成我想要做的事情?
另外,我使用readFile()函数(也来自Scripting Guide )将LZ字符串加载回脚本:
function readFile(file) {
// Convert the file to a string
var fileString = file.toString()
// Read the file and return its contents
return app.read(Path(fileString))
}
但不是回归:
{"test" : "㲃냆Њޱᐈ攀렒삶퓲ٔ쀛䳂䨀푖㢈Ӱນꀀ"}
它正在回归:
"{\"test\" : \"㲃냆੠Њޱᐈ攀렒삶퓲ٔ쀛䳂䨀푖㢈Ӱນꀀ\"}"
有没有人知道这方面的解决方案?
我知道可以在JXA脚本中使用Cocoa ,所以解决方案可能就在其中吗?
我刚刚开始掌握JavaScript,所以我承认尝试掌握Objective-C或者Swift现在已经远远超出了我。
我期待您能够提供给我的任何解决方案和/或指示。 提前致谢!
经过一些Googl'ing之后,我发现了这两个帖子:
因此我改变了我的剧本。
writeTextToFile()现在看起来像:
function writeTextToFile(text, file) {
// source: https://stackoverflow.com/a/44293869/11616368
var nsStr = $.NSString.alloc.initWithUTF8String(text)
var nsPath = $(file).stringByStandardizingPath
var successBool = nsStr.writeToFileAtomicallyEncodingError(nsPath, false, $.NSUTF8StringEncoding, null)
if (!successBool) {
throw new Error("function writeFile ERROR:\nWrite to File FAILED for:\n" + file)
}
return successBool
};
而readFile()看起来像:
ObjC.import('Foundation')
const readFile = function (path, encoding) {
// source: https://github.com/JXA-Cookbook/JXA-Cookbook/issues/25#issuecomment-271204038
pathString = path.toString()
!encoding && (encoding = $.NSUTF8StringEncoding)
const fm = $.NSFileManager.defaultManager
const data = fm.contentsAtPath(pathString)
const str = $.NSString.alloc.initWithDataEncoding(data, encoding)
return ObjC.unwrap(str)
};
两者都使用Objective-C来克服app.write和app.read无法处理UTF-8。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.