简体   繁体   中英

How do I write a LZ compressed string to text file using JXA?

I am trying to write a JXA script in Apple Script Editor, that compresses a string using the LZ algorithm and writes it to a text (JSON) file:

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)

Everything works, except that the LZ compressed string is just transformed to a set of "?" by the time it reaches the output file, test.json .

It should look like:

{"test" : "㲃냆੠Њޱᐈ攀렒삶퓲ٔ쀛䳂䨀푖㢈Ӱນꀀ"}

Instead it looks like:

{"test" : "????????????????????"}

I have a feeling the conversion is happening in the app.write command used by the writeTextToFile() function (which I pulled from an example in Apple's Mac Automation Scripting Guide ):

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
    }
}

Is there a substitute command for app.write that maintains the LZ compressed string / a better way to accomplish what I am trying to do?

In addition, I am using the readFile() function (also from the Scripting Guide ) to load the LZ string back into the script:

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))
}

But rather than returning:

{"test" : "㲃냆੠Њޱᐈ攀렒삶퓲ٔ쀛䳂䨀푖㢈Ӱນꀀ"}

It is returning:

"{\"test\" : \"㲃냆੠Њޱᐈ攀렒삶퓲ٔ쀛䳂䨀푖㢈Ӱນꀀ\"}"

Does anybody know a fix for this too?

I know that it is possible to use Cocoa in JXA scripts , so maybe the solution lies therein?

I am just getting to grips with JavaScript so I'll admit trying to grasp Objective-C or Swift is way beyond me right now.

I look forward to any solutions and/or pointers that you might be able to provide me. Thanks in advance!

After some further Googl'ing, I came across these two posts:

I have thus altered my script accordingly.

writeTextToFile() now looks like:

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  
};

While readFile() looks like:

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)
};

Both use Objective-C to overcome app.write and app.read 's inability to handle UTF-8.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM