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

Writing/Reading "config' files for script variables...

Engaged ,
Sep 07, 2017 Sep 07, 2017

Copy link to clipboard

Copied

   I would like to add the functionality of saving the "default settings" of a few of my scripts, which the scripts would then load upon launching the next time.  Wondering if anyone here has created anything like this and how you went about it.

TOPICS
Scripting

Views

2.3K

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

Community Expert , Sep 08, 2017 Sep 08, 2017

How about this?

Put setting in Illustrators preferences. It will keep after Illustrator quit.

app.preferences.setStringPreference("myScriptPref", JSON_strngs);

And we can read it like below.

var myScriptPreferences = app.preferences.getStringPreference("myScriptPref");

Votes

Translate

Translate
Adobe
Valorous Hero ,
Sep 08, 2017 Sep 08, 2017

Copy link to clipboard

Copied

You can write a text file with your settings (I use JSON format), then make a statement in your code to search and read this file every time it is launched. A typical location for this settings file is in the Documents folder. Even this basic technique has the potential to save minutes of work, because it's a super-easy way to have persistence between Illustrator sessions and script runs.

Some settings you will find more trivial than others, such as 'last folder chosen', and you don't really care about those being stored in at text file or remaining after Illustrator quits and is started again. For such "less-permanent" settings, you can use a global variable. Unlike in other Adobe applications where global variables are hard to set or are not allowed, in Illustrator the global variables persist between script runs. A global variable is a named variable which is not first created with the 'var' keyword, and gets automatically added in the global scope. The next time the script runs, this variable can be read back in, but not after Illustrator is restarted.

To check for whether a global variable exists, you can use this statement:

if(typeof(MYSETTINGS) == 'undefined'){

// the variable does not exist, so we can set it:

MYSETTINGS = '{ "property_A" : "Hello World" }'; // or properly use JSON.stringify() to capture settings in a JSON string

} else {

var mySettingObj = JSON.parse(MYSETTING); // paste in a JSON object from internet, or use eval() to use JSON in string

alert(mySettingObj.property_A);

}

Votes

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 Expert ,
Sep 08, 2017 Sep 08, 2017

Copy link to clipboard

Copied

How about this?

Put setting in Illustrators preferences. It will keep after Illustrator quit.

app.preferences.setStringPreference("myScriptPref", JSON_strngs);

And we can read it like below.

var myScriptPreferences = app.preferences.getStringPreference("myScriptPref");

Votes

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 ,
Sep 08, 2017 Sep 08, 2017

Copy link to clipboard

Copied

You got me excited. But I tried using  setStringPreference as it looked like the simplest solution; however, the data does not survive a reboot. I'll have to the route of writing out a settings file.

Votes

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 Expert ,
Sep 10, 2017 Sep 10, 2017

Copy link to clipboard

Copied

Some extensions I've ever made include this gimmick and works fine all of them.

Votes

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 ,
Sep 15, 2017 Sep 15, 2017

Copy link to clipboard

Copied

LATEST

    The setStringPreference works.  I was running the script from the ESTk. When running it from within illustrator the values survive restarts of AI as well as reboots of the machine.  Thank you so much!

Votes

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 ,
Sep 08, 2017 Sep 08, 2017

Copy link to clipboard

Copied

I get an error of "JSON is undefined".  Maybe i'm using it wrong? Do I need to create some type of JSON object first?

Votes

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
Valorous Hero ,
Sep 08, 2017 Sep 08, 2017

Copy link to clipboard

Copied

You need to find some version of it and paste it in, or use #include to include it as an external file.

Try to save the contents of this repository page into a .jsx file : JSON-js/json2.js at master · douglascrockford/JSON-js · GitHub

Votes

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