Search code examples
extendscriptphotoshop-script

Saving per-user or per-document preferences in a Photoshop script


I'm working on a Photoshop script in JavaScript using ExtendScript. My script allows some user input, and I'd like to save it between uses. That is, I'm looking for a way to save a simple string or numeric value under a particular key so that I'll be able to access it on subsequent uses of the script. Simply put, I want to save a preference for my script. How do I do that?

Even better would be to be able to save at least some preferences on a per-document basis. Is that possible? That is, can I store an arbitrary bit of data with a document?


Solution

  • You can use put/get custom options to save preference parameters that persist across Photoshop launches:

    const kMyFlag = app.stringIDToTypeID( "myFlag" );
    const kMyNumber = app.stringIDToTypeID( "myNumber" );
    const kMySettings = "mySettings";
    
    function saveSettings()
    {
      var desc = new ActionDescriptor();
      desc.putBoolean(kMyFlag, true);
      desc.putInteger(kMyNumber, 42);
    
      // "true" means setting persists across Photoshop launches.
      app.putCustomOptions( kMySettings, desc, true );
    }
    
    function getSettings()
    {
      var desc = app.getCustomOptions( kMySettings );
      return [desc.getBoolean( kMyFlag ), desc.getInteger( kMyNumber )];
    }