[英]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.