applescript - 如何使用 AppleScript 将文件写入为 UTF-8
问题描述
我正在尝试将 AppleScript 中的一些文本作为 .json 写入桌面上的文件。但是我不知道如何确保它写为 UTF-8。
我当前的代码如下所示:
set fileContent to "This is some text content with æøå letters"
set this_data to fileContent
set target_file to (((path to desktop folder) as string) & (time string of (current date)) & "_myfile.json")
set append_data to true
try
set the target_file to the target_file as string
set appleScriptfilePath to target_file as string
set the open_target_file to open for access file target_file with write permission
if append_data is false then set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof
close access the open_target_file
on error
try
close access file target_file
end try
end try
我看到人们引用“作为«class utf8»”,但我不知道如何将它集成到我的代码中。
确保将其保存为 UTF-8 的最佳方法是什么?
解决方案
人是对的,加点as «class utf8»
write this_data to the open_target_file starting at eof as «class utf8»
当然,as «class utf8»
当您read
的文件和文件是 UTF8 编码时,您也必须附加。
你可以删除这两条多余的行
set the target_file to the target_file as string
set appleScriptfilePath to target_file as string
推荐阅读
- google-apps-script - 如何使用带有简单 API 请求的应用脚本将文件上传到 Google Drive
- react-native - expo 如果我启用远程调试,为什么我的应用程序会更快?
- android - 我们可以为内部应用共享设置更新优先级(在应用更新中测试)吗?
- javascript - 在 React 中重定向时组件不刷新
- r - `observe` 在 `observeEvent` 或 `if` 中的行为
- rust - 为什么在 Rust 中 [有时] 不需要对泛型使用生命周期说明符?
- java - 无法以 UTF8/ISO_8859_1 编码文件
- python - Numpy.random.normal 给出不好的结果
- xamarin.forms - Xamarin 表单:数据绑定
- java - 移动自动化代码未运行并给出与诱惑相关的错误