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

Using Json for saving illustrator script settings.

Community Beginner ,
Apr 13, 2016 Apr 13, 2016

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

TOPICS
Scripting
4.2K
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

correct answers 1 Correct answer

Enthusiast , Apr 21, 2016 Apr 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 ex

...
Translate
Adobe
Valorous Hero ,
Apr 14, 2016 Apr 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();

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
Enthusiast ,
Apr 21, 2016 Apr 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.

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 ,
Apr 21, 2016 Apr 21, 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.

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
Enthusiast ,
Apr 22, 2016 Apr 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?

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
Advisor ,
Aug 02, 2022 Aug 02, 2022
LATEST

It works, but how do i read this data? simply using prefs.textbox does not work in this case

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