Hot Door CORE Forum
implementing a preferences file - Printable Version

+- Hot Door CORE Forum (
+-- Forum: All forums (
+--- Forum: Getting started (
+--- Thread: implementing a preferences file (/showthread.php?tid=82)

implementing a preferences file - Rick Johnson - 07-05-2016

Could someone give a brief overview and simple sample code of how to create a Preferences file, read, write, and close it? Studying the Preferences and *Pref classes hasn't proven as intuitively obvious as I'd hoped. Previously my plugins preferences were simply embedded in the AI preferences file, so I'm mostly interested in where the CORE method differs. I'm sure much of it is JSON specific, and can research that portion elsewhere. I'd be most grateful to get started in the right direction. -- Rick

RE: implementing a preferences file - garrett - 07-05-2016

We moved away from using Illustrator's preference file many years ago, due to the file becoming corrupted for no apparent reason whatsoever. This generated lots of tech support calls where the solution was inevitably telling the user to delete the Illustrator prefs file and relaunching. Since moving to our own prefs file, we have seen basically no data corruption at all (so the phone calls have been drastically reduced).

CORE's prefs data is indeed stored on disk JSON-encoded. As such, you can nest data in whatever hierarchy you'd like (as JSON objects can contain other JSON objects, etc.) We have our own classes to handle this nesting, namely ArrayPref and DictionaryPref (arrays storing indexed elements and dictionaries storing named members, as you'd expect). All other classes are named pretty obviously, and all of them have the *Pref suffix.

Here is a little sample for loading the prefs file and checking if the current plugin run is its first, or if the user has run it before.

// Assume this is not the first plugin launch
bool firstLaunch = false;

// Load prefs object from file on disk
hdi::core::Preferences prefs;
bool loaded = hdi::core::Preferences::load("/path/to/prefs/file", prefs);
    // Get the first launch pref if it exists, otherwise it will be created with a default value of "true"
    hdi::core::BoolPref firstLaunchPref;
    prefs.getOrCreateBoolPref("FirstLaunch", true, firstLaunchPref);

    // The first launch pref should only be true if it was just created, but always set to false after reading the value
    firstLaunch = firstLaunchPref.boolValue();

    // Save the prefs object to its file on disk"/path/to/prefs/file");
    // TODO show some error dialog to the user

Lastly, if you'd like to store your prefs file in the user's platform preferences folder, have a look at the hdi::core::files::platformPrefsPath() function.

RE: implementing a preferences file - Rick Johnson - 07-05-2016

Thanks, Garrett! This is very helpful. I'm eager to move to this preferences model since AI Pref file issues have been in my top three support categories, also. -- rj