Skip to main content
Known Participant
May 5, 2012
Question

Create files with javascript?

  • May 5, 2012
  • 1 reply
  • 7132 views

Hello,

I would like make a config file for my script that i would like to put in the %appdata%\adobe folder.

  1. How can i reah the value of %appdata% inside javascript?
  2. How do i create and read files?

I am currently reading the Extendscript javascript toolsguide but i cant figure out how to create a file:

Should not this code create a file in C:\ directory?

Fille = new File('C:\test.txt');

Fille.write("Testwriting!");

Fille.close();

/ asdfasdfasdf

This topic has been closed for replies.

1 reply

John Hawkinson
Inspiring
May 5, 2012

How can i reah the value of %appdata% inside javascript?

APPDATA is an environment variable, so $.getenv("APPDATA").

Should not this code create a file in C:\ directory?

new FIle() merely creates a reference to a [potential] file. You must open the file for writing first.

But it's quite bad style to start variable names with an uppercase letter -- uppercase first letters in Javascript should be reserved for Constructors, like new File(). So you want something like:

fille = new File('C:\\test.txt');

fille.open();

fille.write("Testwriting!");

fille.close();

You must also double the backslash in a quoted string.

Known Participant
May 5, 2012

Great!

the $.getenv("APPDATA") worked like charm but unfortunately not the file code.

I run exactly the code that you wrote and it does not working. I have changed the path if i for some reason did not have any right in the C: root no

Any Ideeas?

EDIT:

With some googleing i found some code that works:

fille.open("w");

WriteMode for the win!

Thanks for the help!!

Inspiring
May 5, 2012

You should also look at the class properties of the folder object…

Folder.appData

Should give you the same path? There are several others of use there so take a look… Yes you have 4 options of file open( ) also in guide…