如果我使用标准附加项编写文本文件,我显然可以在参数包中配置编码。在AppleScript中,我会写«class utf8»,但是在JXA中使用哪个值呢?
我尝试了字符串"UTF8"、"utf8"、"class utf8",但都没有成功。错误始终为:"Error: Can't convert types. (-1700)"。如果我使用"text",则文件将以MACROMAN编写。
以下是独立的示例:
我尝试了字符串"UTF8"、"utf8"、"class utf8",但都没有成功。错误始终为:"Error: Can't convert types. (-1700)"。如果我使用"text",则文件将以MACROMAN编写。
以下是独立的示例:
var exportFileWriter = fileWriter('/Users/norman/mini.txt');
exportFileWriter.write('äöü');
exportFileWriter.close();
function fileWriter(pathAsString) {
'use strict';
var app = Application.currentApplication();
app.includeStandardAdditions = true;
var path = Path(pathAsString);
var file = app.openForAccess(path, {writePermission: true});
/* reset file length */
app.setEof(file, {to: 0});
return {
write: function(content) {
/* How can I write UTF-8? */
app.write(content, {to: file, as: 'text'});
},
close: function() {
app.closeAccess(file);
}
};
}
在foo的回答之后添加:
和
要使用ObjectiveC桥接。这个示例是有效的。
str = $.NSString.alloc.initWithUTF8String('foo')
str.writeToFileAtomically('/tmp/foo', true)
如果我理解正确,以下示例应该可以工作,因为ObjectiveC方法在桥接中的命名惯例(去掉冒号,添加驼峰式),但实际上它并不起作用。没有文件被写入,返回值为false。
str = $.NSString.alloc.initWithUTF8String('foo')
str.writeToFileAtomicallyEncodingError('/tmp/foo', true, 'UTF-8', null);
我做错了什么?我甚至不知道如何传递一个正确的错误处理程序作为第四个参数,以及第三个参数是否具有正确的值。