Saving Preferences - Is This Possible?

From within my plugin panel, I would like to have a selection of check boxes which when ticked / un-ticked save out some user preferences to a file and then the panel reads that file upon launch.

Is this possible with Photoshop UXP? and if so does anyone have any code examples.


Yes; you can just use localStorage. (Or the file system if you need access to the actual preferences file.)

For example:

function getPreferredFontSize() {
    const savedPreference = localStorage.getItem("preferred-font-size");
    return (savedPreference === undefined) ? 10 : Number(savedPreference);
function setPreferredFontSize(size) {
    localStorage.setItem("preferred-font-size", size.toString());

Then, whenever your panel is shown, you can read the preferences and then save them out when they are modified.

If you’re going to have a lot of preferences, it may make more sense to have a preference object and then serialize that to local storage with JSON.stringify (and you can reload that with JSON.parse) so that you aren’t saving a lot of small keys (loading one large object is faster than, say, 500 small ones) and avoiding a lot of repetitive boilerplate. But for a few checkboxes, the above method works just fine.

When you talk about localStorage, where is this actually been saved to

And what are the advantages/disadvantages of using localStorage over UXP’s getDataFolder()? I’ve been using getDataFolder() to save/get values like this. Should I switch it to localStorage?

Also, with previous versions of PS, localStorage was getting purged periodically by Adobe, it seemed. Want to make sure this won’t be happening anymore, like with Photoshop updates.

The file system and localStorage both use plugin-specific storage locations, and the storage policies are the same. In that way there’s no difference between the two (you can see them on disk in sibling folders). localStorage is just better suited for key/value storage, while the file system is more general purpose. If you really need something to be persistent, make sure that you let the user save the data somewhere on their machine. (This could be an export of your preferences, for example, if they are laborious to configure.) As such, if what you’ve got is working for you, there’s no benefit to changing.