title: How can I write UTF-8 files using JavaScript for Mac Automation?
body: <p><strong>In short - what is the JavaScript for Mac Automation equivalent of AppleScript's <code>as «class utf8»</code> ?</strong></p>
  
  <p>I have a unicode string that I'm trying to write to a text file using JavaScript for Mac Automation.</p>
  
  <p>When writing the string to the file, any unicode characters present become question marks in the file (ASCII char <code>3F</code>).</p>
  
  <p>If this was an AppleScript script instead of a JavaScript one, I could have solved this by adding the <code>as «class utf8»</code> raw statement as explained on Takashi Yoshida's Blog (<a href="https://takashiyoshida.org/blog/applescript-write-text-as-utf8-string-to-file/" rel="nofollow noreferrer">https://takashiyoshida.org/blog/applescript-write-text-as-utf8-string-to-file/</a>).</p>
  
  <p>The script, however, is already written in JavaScript so I'm looking for the JavaScript equivalent to this AppleScript statement. Apple's page about raw statements addresses only AppleScript (<a href="https://developer.apple.com/library/content/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_raw_data.html" rel="nofollow noreferrer">https://developer.apple.com/library/content/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_raw_data.html</a>).</p>
  
  <p>To write the file, I am using Apple's own <code>writeTextToFile</code> JavaScript function example (<a href="https://developer.apple.com/library/content/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ReadandWriteFiles.html#//apple_ref/doc/uid/TP40016239-CH58-SW1" rel="nofollow noreferrer">https://developer.apple.com/library/content/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ReadandWriteFiles.html#//apple_ref/doc/uid/TP40016239-CH58-SW1</a>). I added an <code>as</code> argument to the following call, according to the StandardAdditions dictionary:</p>
  
  <pre><code>// Write the new content to the file
  app.write(text, { to: openedFile, startingAt: app.getEof(openedFile), as: "utf8" })
  </code></pre>
  
  <p>And tried all of the following strings (as written and also in lowercase form):</p>
  
  <ul>
  <li>Unicode text</li>
  <li>Unicode</li>
  <li>Class utf8</li>
  <li>«class utf8»</li>
  <li>utf8</li>
  <li>text</li>
  <li>utf8 text</li>
  </ul>
  
  <p>Apart for "text" (which resulted in the same question marks situation), using all of the above strings yielded a zero-bytes file.</p>
  
  <p>I understand I might be wading into uncharted waters here, but if anyone reading this has dealt with this before and is willing to provide some pointers, I will be quite grateful </p>