How can I write UTF-8 files using JavaScript for Mac Automation?

后端 未结 4 853
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-13 15:05

In short - what is the JavaScript for Mac Automation equivalent of AppleScript\'s as «class utf8» ?

I have a unicode string that I\'m t

相关标签:
4条回答
  • 2021-01-13 15:41

    @PatrickWayne has the correct solution.

    I already had this function in my lib, so I thought I'd share it. It uses the same key commands.

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    function writeFile(pPathStr, pOutputStr) {  //  @File @Write @ObjC
      /*  VER: 2.0  2017-03-18  
    ---------------------------------------------------------------
      PARAMETERS:
        pPathStr    | string  | Path of file to write.  May use tilde (~)
        pOutputStr  |  string  |  String to be output to file.
      */
      
      //--- CONVERT TO NS STRING ---
      var nsStr       = $.NSString.alloc.initWithUTF8String(pOutputStr)
      
      //--- EXPAND TILDE AND CONVERT TO NS PATH ---
      var nsPath      = $(pPathStr).stringByStandardizingPath
      
      //--- WRITE TO FILE ---
      //      Returns true IF successful, ELSE false
      var successBool  = nsStr.writeToFileAtomicallyEncodingError(nsPath, false, $.NSUTF8StringEncoding, null)
      
      if (!successBool) {
        throw new Error("function writeFile ERROR:\nWrite to File FAILED for:\n" + pPathStr)
      }
      
      return successBool
      
    };

    0 讨论(0)
  • 2021-01-13 15:45

    JXA can't do symbol types (type and enumerator names) right, and it can't do raw four-char codes at all. Either stick to AppleScript, which is the only officially supported option that speaks Apple events right, or use the Cocoa bridge to write a NSString to file in NSUTF8StringEncoding, c.f. Patrick's solution.

    0 讨论(0)
  • 2021-01-13 15:58

    While I was unable to find a way to do it with JavaScript only, I ended up utilizing the Objective-C Bridge to accomplish UTF-8 file write-out.

    Here is the code that I used. This is JavaScript code that invokes the Objective-C NSString class, and it replaces the writeTextToFile function that I mentioned above altogether:

    objCText = $.NSString.alloc.initWithUTF8String(text);
    objCText.writeToFileAtomically(filePathString, true);
    
    0 讨论(0)
  • 2021-01-13 16:02

    If you want to ensure your file gets written with UTF8 encoding, use NSString's writeToFile:atomically:encoding:error function, like so:

    fileStr = $.NSString.alloc.initWithUTF8String( 'your string here' )
    fileStr.writeToFileAtomicallyEncodingError( filePath, true, $.NSUTF8StringEncoding, $() )
    

    You would think that writing an NSString object initialized from a UTF8 string would get written out as UTF8 but I've found from experience that writeToFile:atomically does not honor the encoding of the string being written out. writeToFile:atomically:encoding:error explicitly specifies which encoding to use. On top of that, writeToFile:atomically has been deprecated by Apple since OS X 10.4.

    0 讨论(0)
提交回复
热议问题