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

I'm extracting some data from an InDesign INDD file using a script. I'd like to save my data

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745276429a4620069.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信