Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Create files with javascript?

Community Beginner ,
May 05, 2012 May 05, 2012

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

TOPICS
Scripting
6.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
LEGEND ,
May 05, 2012 May 05, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 05, 2012 May 05, 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!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
May 05, 2012 May 05, 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…

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 05, 2012 May 05, 2012

Great info! Thanks!

Although i think i solved it a little bit wierd, i dont know if i am using it wrong or if it is a bug but i cant use the open("e");

Example:

This should work but i t does not, it reads the data and change the DefaultSavePath correctly, but it cant save it.


var appdata = $.getenv("APPDATA")

var xmlFile = new File(appdata + "\\adobe\\Settings.xml");

xmlFile.open("e");

var xml = new XML (xmlFile.read());

xml.DefaultSavePath = "Testing AGAIN";

xmlFile.close();

I solved it with:


var appdata = $.getenv("APPDATA")

var xmlFile = new File(appdata + "\\adobe\\Settings.xml");

xmlFile.open("r");

var xml = new XML (xmlFile.read());

xml.DefaultSavePath = "Testing AGAIN";

xmlFile.close();

xmlFile.open("w");

xmlFile.write(xml);

xmlFile.close();

Do you see what i am doing wrong?

EDIT: I have no idea why the code for some reason ended up in some kind of tables, but whatever!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
May 06, 2012 May 06, 2012

Which option did you pick… to paste the code in? You should click 'Use advanced editor' top right corner and go to the blue chevrons…

Screen shot 2012-05-06 at 11.29.03.png

I had one go all weird on me yesterday with bolding and colours…

var xmlFile = new File( Folder.appData + '/Adobe/Settings.xml' );

xmlFile.open( 'w' );

var xml = new XML ( xmlFile.read() );

//xml.DefaultSavePath = 'Testing AGAIN'; // Where did you find this default thing???

xml.addNamespace( 'http://www.adobe.com/test' ); // I don't know XML so a guess…

xmlFile.write( xml );

xmlFile.close();

xmlFile.execute();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 06, 2012 May 06, 2012

Aah, nice, the Advanced Editor

So it is hiding over there!

The "xml.DefaultSavePath" comes from my xml file wich looks like this:

<ScriptRoot>

  <DefaultSavePath>Testing</DefaultSavePath>

</ScriptRoot>

Example:

var xml = new XML (

<ScriptRoot>

  <DefaultSavePath>Testing</DefaultSavePath>

  <DefaultSavePath>C:\program files\adobe</DefaultSavePath>

  <DefaultSavePath>B:\adobe</DefaultSavePath>

</ScriptRoot>

);

alert(xml.DefaultSavePath[0]) // Returns: Testing

alert(xml.DefaultSavePath[1]) // Returns: C:\program files\adobe

alert(xml.DefaultSavePath[2]) // Returns: B:\adobe


So what i am trying to do is to change that value and write that to the file but i cant do it with the e option.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 06, 2012 May 06, 2012

Example:

This should work but i t does not, it reads the data and change the DefaultSavePath correctly, but it cant save it.

...
Do you see what i am doing wrong?

EDIT: I have no idea why the code for some reason ended up in some kind of tables, but whatever!

Err, I don't understand your question.

It sounds like you are asking why your first example does not save your change to DefaultSavePath.

The answer is that you are not even attempting to save the change.

You read the contents of Settings.xml into a variable called xml and then you modify that variable. There is no persistent connection between the variable and that variable and the original file. You should have no expectation that changes to the variable will change the file. They won't. It's just a data structure in memory. Did you have some reason to think otherwise?

With respect to tables, the "new" version of the forum software does this when the first line of pasted code starts with a blank space. As MuppetMark said, use the Java syntax highlighting. But also, just make sure the first line that you copy does not start with a space.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 06, 2012 May 06, 2012

OMG sorry, forgot the .write method when i posted it!

For clearing up!

This does not work!

var appdata = $.getenv("APPDATA")

var xmlFile = new File(appdata + "\\adobe\\ExportAsPNG_Settings.xml");

xmlFile.open("e");

var xml = new XML (xmlFile.read());

xml.DefaultSavePath = "Testing AGAIN";

xmlFile.write(xml);

xmlFile.close();

This Works:

var appdata = $.getenv("APPDATA")

var xmlFile = new File(appdata + "\\adobe\\ExportAsPNG_Settings.xml");

xmlFile.open("r");

var xml = new XML (xmlFile.read());

xml.DefaultSavePath = "Testing AGAIN";

xmlFile.close();

xmlFile.open("w");

xmlFile.write(xml);

xmlFile.close();

This is what i have to do:

  1. Open the file in Read mode
  2. Read the file into a variable
  3. Modify the variable
  4. Close the file
  5. Open the file in write mode
  6. Write the variable to the file
  7. Close the file.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 06, 2012 May 06, 2012

Opening a file read/write merely means that it is possible to both read and write to the file.

I'm not sure that it's such a good idea to try to do both at once.

How does your code using xmlFile.write() fail? You don't say!

I would expect that the contents of Settings.xml would be duplicated.

When you read and when you write to a file, it advances the file pointer to which subsequent reads and writes are relative.

In your first example, you call .read(), which reads the file entire and advances the file pointer to the end. Then you call .write(), which should write the string representation of your xml varible to the file relative to the file pointer. So your file should have two copies of the xml: the original unmodified, followed by your modified version.

Is that what happens? (Again, all you said was "does not work"; that is pretty much never sufficient! There is always more information you can provide that will help people help you.)

If you really want to do it this way, you can call xmlFile.seek(0); before you write.

I'm not sure, though, whether the .close() method will truncate the file. If you shorten the xml data, will the original excess still be there?

You could, I suppose, call xmlFile.tell(); before you close, and then set xmlFile.length to that value afterwards.

I suppose you might still have this problem if you open the file for writing. I'm not sure. Something to test.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 06, 2012 May 06, 2012

Sorry for the lack of information!!

My first example that does not work just does not work

I does not get any error message and the modified date for the file does not change. Nothing happens

If i put an alert in the code:

var appdata = $.getenv("APPDATA")

var xmlFile = new File(appdata + "\\adobe\\ExportAsPNG_Settings.xml");

xmlFile.open("e");

var xml = new XML (xmlFile.read());

xml.DefaultSavePath = "Testing AGAIN";

alert(xml);

xmlFile.write(xml);

xmlFile.close();

It will alert the xml variable with correct changes.

In my other example that works the whole file will be replaced with my xml variable and that is how i want it to be

I am okey with using both W and R mode, I just thought it was weird that the E option did not work.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 07, 2018 Oct 07, 2018
LATEST

Hi,

is it possible to access files(create,execute) in "common files" folder from extension?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines