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.
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");
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);
}
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");
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.
Copy link to clipboard
Copied
Some extensions I've ever made include this gimmick and works fine all of them.
Copy link to clipboard
Copied
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!
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?
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