【问题标题】:How can I write UTF-8 files using JavaScript for Mac Automation?如何使用 JavaScript for Mac Automation 编写 UTF-8 文件?
【发布时间】:2017-10-31 07:03:51
【问题描述】:

简而言之 - 什么是相当于 AppleScript 的 as «class utf8» 的 Mac 自动化 JavaScript?

我有一个 unicode 字符串,我正在尝试使用 JavaScript for Mac Automation 将其写入文本文件。

将字符串写入文件时,存在的任何 unicode 字符都将成为文件中的问号(ASCII char 3F)。

如果这是一个 AppleScript 脚本而不是 JavaScript 脚本,我可以通过添加 as «class utf8» 原始语句来解决这个问题,如 Takashi Yoshida 的博客 (https://takashiyoshida.org/blog/applescript-write-text-as-utf8-string-to-file/) 中所述。

但是,该脚本已经用 JavaScript 编写,因此我正在寻找与此 AppleScript 语句等效的 JavaScript。 Apple 关于原始语句的页面仅针对 AppleScript (https://developer.apple.com/library/content/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_raw_data.html)。

为了编写文件,我使用的是 Apple 自己的 writeTextToFile JavaScript 函数示例 (https://developer.apple.com/library/content/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ReadandWriteFiles.html#//apple_ref/doc/uid/TP40016239-CH58-SW1)。根据 StandardAdditions 字典,我在以下调用中添加了 as 参数:

// Write the new content to the file
app.write(text, { to: openedFile, startingAt: app.getEof(openedFile), as: "utf8" })

并尝试了以下所有字符串(书面形式和小写形式):

  • Unicode 文本
  • Unicode
  • utf8 类
  • «class utf8»
  • utf8
  • 文字
  • utf8 文本

除了“文本”(导致相同的问号情况),使用所有上述字符串产生一个零字节文件。

我知道我在这里可能涉足未知领域,但如果有人读过这篇文章并愿意提供一些指点,我将不胜感激????

【问题讨论】:

    标签: javascript macos applescript javascript-automation


    【解决方案1】:

    如果您想确保您的文件使用 UTF8 编码编写,请使用 NSString 的 writeToFile:atomically:encoding:error 函数,如下所示:

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

    您会认为编写从 UTF8 字符串初始化的 NSString 对象会被写为 UTF8,但我从经验中发现 writeToFile:atomically 不尊重被写出的字符串的编码。 writeToFile:atomically:encoding:error 明确指定要使用的编码。最重要的是,writeToFile:atomically 自 OS X 10.4 起已被 Apple 弃用。

    【讨论】:

      【解决方案2】:

      @PatrickWayne 拥有the correct solution

      我的库中已经有了这个函数,所以我想我会分享它。 它使用相同的键盘命令。

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

      【讨论】:

        【解决方案3】:

        虽然我无法找到仅使用 JavaScript 的方法,但我最终使用了 Objective-C Bridge 来完成 UTF-8 文件的写出。

        这是我使用的代码。这是调用 Objective-C NSString 类的 JavaScript 代码,它完全替换了我上面提到的 writeTextToFile 函数:

        objCText = $.NSString.alloc.initWithUTF8String(text);
        objCText.writeToFileAtomically(filePathString, true);
        

        【讨论】:

          【解决方案4】:

          JXA 不能正确处理符号类型(类型和枚举器名称),它根本不能处理原始的四字符代码。要么坚持 AppleScript,这是唯一官方支持的选项,它可以正确表达 Apple 事件,或者使用 Cocoa 桥将 NS​​String 写入 NSUTF8StringEncoding 文件,c.f.帕特里克的解决方案。

          【讨论】:

            猜你喜欢
            • 2010-11-03
            • 1970-01-01
            • 1970-01-01
            • 2012-04-20
            • 1970-01-01
            相关资源
            最近更新 更多