Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Using Json for saving illustrator script settings.

Community Beginner ,
Apr 13, 2016 Apr 13, 2016

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

TOPICS
Scripting

Views

1.1K

Likes

Translate

Translate

Report

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

Engaged , Apr 21, 2016 Apr 21, 2016
is there a particular reason you need JSON? i find js objects easier to read than a json filehere 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...

Likes

Translate

Translate
Adobe Community Professional ,
Apr 14, 2016 Apr 14, 2016

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

Likes

Translate

Translate

Report

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

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.

Likes

Translate

Translate

Report

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

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.

Likes

Translate

Translate

Report

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

Copy link to clipboard

Copied

LATEST

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?

Likes

Translate

Translate

Report

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