Skip to main content
yshkrish
Participating Frequently
April 13, 2016
Answered

Using Json for saving illustrator script settings.

  • April 13, 2016
  • 2 replies
  • 4736 views

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

This topic has been closed for replies.
Correct answer mecdos

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.

2 replies

mecdosCorrect answer
Inspiring
April 21, 2016

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.

yshkrish
yshkrishAuthor
Participating Frequently
April 22, 2016

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.

Inspiring
April 22, 2016

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?

Silly-V
Legend
April 14, 2016

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