最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I save a text file using an InDesign script? - Stack Overflow

programmeradmin0浏览0评论

I'm extracting some data from an InDesign INDD file using a script. I'd like to save my data in a txt or json file, but my file is not saving successfully.

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.

I'm extracting some data from an InDesign INDD file using a script. I'd like to save my data in a txt or json file, but my file is not saving successfully.

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.

Share Improve this question edited Feb 18, 2016 at 21:43 Jack Steam asked Feb 18, 2016 at 16:53 Jack SteamJack Steam 5,3292 gold badges28 silver badges41 bronze badges 3
  • What language are you writing your script in? – Rowland Shaw Commented Feb 18, 2016 at 17:00
  • 1 Also, you don't seem to use the result of the saveDlg() call (not even to get the filename to write() to?) – Rowland Shaw Commented Feb 18, 2016 at 17:01
  • @RowlandShaw Thanks for your input! I'm using javascript. I've updated my code to use the result of saveDlg(), but I'm still getting the same oute. – Jack Steam Commented Feb 18, 2016 at 17:16
Add a ment  | 

1 Answer 1

Reset to default 6

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.

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()

发布评论

评论列表(0)

  1. 暂无评论