Sg2.9
Save array data to external f...
Recent RSS
Save array data to external file?
Date:  12. May 2010, 15:28

Is there any way to save data, passed to an array, to an external file (textformat) to later be loaded and parsed within Scriptographer?

I want to make a script where the user passes arbitrary inputs to constructed objects in AI. If many objects where created it would a pain to manually fill in that data at a later point so a separate data file should be nice to have.

Is there any way of achieving this?

Re: Save array data to external file?
From:  Jamie
Date:  14. May 2010, 14:45

Not hijacking, adding support! I'd like to know the answer to this too. For myself to create a path with a "squared spiral", is there a path offset feature to do you have to give absolute positions? I could calculate but it sure would be easier!

J

Re: Save array data to external file?
From:  Jürg Lehni
Date:  14. May 2010, 15:21

Hi Håkan,

There's the currently undocumented File object that helps you do these kind of things easily. For pure JS data (strings, numbers, booleans, objects and arrays) you can then combine this with JSON for a simple and flexible way of storing and retrieving data from and to files.

So to store JS data, you would do something like this:

var data = [false, 1, 2, 3, 'four', { five: 6 }];
var file = new File(script.file.parent, 'file.json');
print(file);
// If file exists, we need to remove it first in order to overwrite its content.
if (file.exists())
    file.remove();
file.open();
file.write(Json.encode(data));
file.close();

After executing this, you can examine the file 'data.json' that you will find in the same directory as the script, due to the script.file.parent that was passed as the parent to the File constructor. You will find this JSON describing your data: [false,1,2,3,"four",{"five":6}].

Now in order to read that back, you can do:

var file = new File(script.file.parent, 'file.json');
file.open();
var data = Json.decode(file.readAll());
file.close();
print(data);

And this will print: false,1,2,3,four,[object Object]. Note that toString() does not describe the contents of objects, whereas Json.encode() does.

I hope this helps?

Re: Save array data to external file?
Date:  14. May 2010, 17:45

Great!

Thank you very much, Jürg. I will try this out as soon as time permits.
Can't say how much fun your little creation brings me and I will for sure donate some to further your work on Scriptographer. Just as soon as I get my salary.

Thanks again!

Re: Save array data to external file?
From:  Jürg Lehni
Date:  14. May 2010, 18:00

Håkan, that's nice to hear! And you are already donating lots by making scripts, helping out on the forum and asking the right questions.

So thanks back to you!

Re: Save array data to external file?
From:  Jay Weeks
Date:  8. December 2010, 09:32

awesome, I was hoping I could do this. thanks for the tip!

Re: Save array data to external file?
From:  Benedikt
Date:  26. May 2012, 13:08

just came across a simliar problem ... the script shows how to export pathItems to geoJSON. very basic and rudimentary but probably a good starting point.

Re: Save array data to external file?
From:  Kumar
Date:  12. September 2012, 15:35

Dear Helper,

My requirement: I have html form where user can input the form and i want to store the user entered form details into a text file without using the server side technology.

So i want to write the user input form details into a file which should be at the server side, where my html is hosted in the machine.

I tried used above code but its is throwing error at line "var file = new File(script.file.parent, 'file.json');" To solve this error do i have include any script libraries in my html page?

Thanks for your quick response

Scripts
08.08.14, 15:24
15.05.14, 14:23
02.03.14, 19:16
18.11.13, 14:48
22.03.13, 03:05
22.02.13, 15:45
Posts
10.01.17, 16:37
19.02.16, 06:03
19.02.16, 06:00
17.01.16, 11:00
12.01.16, 13:10
25.11.15, 08:19
Script of the Moment
Stroke Tone Line 10.03.09