Question

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?

Was it helpful?

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 )];
}

OTHER TIPS

You have some options. You can create a text file and write to it using the File object:

var prefs = new File("~/desktop/prefs.txt");
prefs.open("w"); // or "a" to append
prefs.writeln("user:lenny;favorite_color:ff6600;likes:sunsets;");

...if you wanted your preferences tied to the script itself.

If you want per-document preferences you could write a string to one of the metadata fields of the file your working on using Document.info like this (using the 'instructions' field but you could use any writable field):

var doc = app.activeDocument;
doc.info.instructions = "user:lenny;favorite_color:ff6600;likes:sunsets;";
//alert(doc.info.instructions); // see, it works!

As for how to actually format the string you could just do it like a simple config file or, if you have a complex user preferences object you could use the XML object to construct and serialize it. JSON would be great for this but there is no JSON object in Extendscript, unfortunately.

For per-document prefs I suggest the use of the XMP Metadata. You can find example snippet here: http://forums.adobe.com/thread/790973. You can leverage AdobeXMPScript library to create your own namespace like it is suggested in the link by Paul Riggott.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top