Copy link to clipboard
Copied
I am new to illustrator.I written a basic code on illustrator script and working fine.I want to save the values in txtbox in json file on butten click and retrieve from the same file to textbox on another button click. could any one help me to write this program.such that i could use json objects easily.my code is given below.please help me in adding json.
var w = new Window ("dialog");
var docRef = app.activeDocument;
var txtbox =w.add('edittext', undefined, "");
var btnsave=w.add ("button",[0, 0, 50, 50], "save");
var btnretrive=w.add ("button",[0, 0, 50, 50], "retrieve");
w.show ();
1 Correct answer
is there a particular reason you need JSON? i find js objects easier to read than a json file
here is what i do:
make an object with the preferences:
var prefs = {}
prefs.txtbox = "something"
prefs.btnsave = "something"
prefs.btnretrive = "something"
save object to file:
var file = File(directory);
file.open('w')
file.write(prefs.toSource())
file.close()
open saved object:
file.open("r")
prefs = eval(file.read())
file.close();
btw, you can use any ex
...Explore related tutorials & articles
Copy link to clipboard
Copied
To make a json string, I use the JSON object which I paste in from any place where there's a JSON object. I just grab whichever one works, from here: GitHub - douglascrockford/JSON-js: JSON in JavaScript.
Using it in above code it would look like this:
var jsonString = JSON.stringify({myText : txtbox.text});
To write a file you do the following code:
var f = File(<write your file's directory here. Ex: "~/Desktop/myFile.txt">);
f.open('w'); // w for 'write'
f.write(jsonString);
f.close();
Copy link to clipboard
Copied
is there a particular reason you need JSON? i find js objects easier to read than a json file
here is what i do:
make an object with the preferences:
var prefs = {}
prefs.txtbox = "something"
prefs.btnsave = "something"
prefs.btnretrive = "something"
save object to file:
var file = File(directory);
file.open('w')
file.write(prefs.toSource())
file.close()
open saved object:
file.open("r")
prefs = eval(file.read())
file.close();
btw, you can use any extension you want for the file, it will read just the same. i made a custom extension for all my script preferences.
Copy link to clipboard
Copied
Thanks Te_co,
could you tell me how to validate this xml. since when i have txtbox value already exists.it must alert that it was already exists.else save data.
suppose,
prefs.txtbox = "Illu"
//button clicked it saved
*************************
then again
prefs.txtbox = "Illu"
//button clicked alert "already exists"
Please help me.
Thank you.
Copy link to clipboard
Copied
i don't know. i don't fully understand what you are trying to accomplish.
does it matter if prefs.txtbox exists? why not just overwrite it silently?
Copy link to clipboard
Copied
It works, but how do i read this data? simply using prefs.textbox does not work in this case

