简体   繁体   English

如何使用InDesign脚本保存文本文件?

[英]How do I save a text file using an InDesign script?

I'm extracting some data from an InDesign INDD file using a script. 我正在使用脚本从InDesign INDD文件中提取一些数据。 I'd like to save my data in a txt or json file, but my file is not saving successfully. 我想将数据保存在txt或json文件中,但文件未成功保存。

var data = 'string of data';
var filename = 'CS111.json';

var file = new File(filename);
var path = file.saveDlg(); //returns a valid path, but with spaces as '%20'
file.changePath(path);
var write = file.write(data); //returns false
file.close();

What am I missing here? 我在这里想念什么? The file doesn't show up in the chosen folder. 该文件未显示在所选文件夹中。

There are at least four steps in saving a file using InDesign Javascript: get a path and filename, create a file object, open the file, write to the file, and close the file. 使用InDesign Javascript保存文件至少需要四个步骤:获取路径和文件名,创建文件对象,打开文件,写入文件并关闭文件。

I find that the write step will sometimes fail if I don't set the encoding. 我发现如果不设置编码,写入步骤有时会失败。

//Define path and file name
var path = '~/Documents/';
var filename = 'filename.txt';

//Create File object
var file = new File(path + filename);

file.encoding = 'UTF-8';
file.open('w');
file.write('data here');
file.close();

Documentation: File class , .open() , .write() 文档: File.open() .write()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM