2017-05-30 99 views
0

簡而言之,AppleScript的as «class utf8»等效於Mac Automation的JavaScript是什麼?如何使用JavaScript for Mac Automation編寫UTF-8文件?

我有一個unicode字符串,我試圖寫入使用JavaScript的Mac自動化的文本文件。

將字符串寫入文件時,存在的任何unicode字符都會成爲該文件中的問號(ASCII字符3F)。

如果這是一個AppleScript腳本而不是JavaScript腳本,我可以通過在Takashi Yoshida的博客(https://takashiyoshida.org/blog/applescript-write-text-as-utf8-string-to-file/)上解釋,添加as «class utf8»原始語句來解決此問題。

但是,該腳本已經使用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)。我添加了一個as參數下面的調用,根據StandardAdditions詞典:

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

並試過所有的以下字符串(如小寫的形式寫成,也):

  • Unicode文本
  • 的Unicode
  • 類UTF8
  • «類UTF8»
  • UTF8
  • 文本
  • UTF8文字除了「文本」(這導致了同樣的問號情況),使用上述所有字符串

產生一個零字節的文件。

我明白我可能會涉水到未知的水域在這裏,但如果有人讀這之前已經處理了這一點,並願意提供一些指點,我會非常感激

回答

2

如果你想確保你的文件被使用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明確指定要使用哪種編碼。另外,自OS X 10.4以來,writeToFile:atomically已被Apple棄用。

0

雖然我無法找到一種方法只使用JavaScript,我最終使用Objective-C Bridge來完成UTF-8文件寫入。

這是我使用的代碼。這是調用Objective-C的NSString類的JavaScript代碼,它取代了writeTextToFile功能我上面一共提到:

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

JXA無法正確執行符號類型(類型和枚舉器名稱),它根本無法執行原始的四字符代碼。要麼堅持使用AppleScript,AppleScript是唯一正式支持蘋果事件的選項,要麼使用Cocoa橋編寫NSString文件到NSUTF8StringEncoding,c.f. Patrick的解決方案。

0

@PatrickWaynethe correct solution

我已經有了這個函數在我的lib中,所以我想我會分享它。 它使用相同的鍵盤命令。

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