General purpose Preference tool

Photoshop Script Snippets - Note: Full Scripts go in the Photoshop Scripts Forum

Moderators: Tom, Kukurykus

kpt

General purpose Preference tool

Post by kpt »

This is some code I wrote a while back to deal with user's preference settings in a more general way. Most larger scripts requires the programmer to write UI-code for setting various parameters. With this package you can have all your settings in a Preference file (see example below) and simply call Prefs.dialog() to let the user modify the settings, as shown in the following example:



All preference settings are stored in the Preference file.
The Preference file is something you design for your program and keep in, e.g., Folder.userData. When the user clicks "Save", the file is overwritten with new values.
Here is what the Preference file for the above example looked like:
Code: Select allPreferences["color"] = {
  "type" : "list",
  "value": 0,
  "default": 0,
  "desc":"Color:",
  "help":"Your preferred color setting.",
  "values": { "0" : "Red","1":"Green","2":"Blue" }
Preferences["instructions"] = {
  "type" : "boolean",
  "value": false,
  "default": true,
  "desc": "Instructions:",
  "help": "Turn on or off helpful instructions"
Preferences["InputFolder"] = {
  "type": "folder",
  "value": "",
  "default": "",
  "desc": "Input folder:",
  "help": "Folder where input files are kept."
Preferences["Thickness"] = {
  "type": "range",
  "min": 1,
  "max": 50,
  "value": 2,
  "default": 5,
  "desc": "Thickness:",
  "help":"Controls the border thickness (in pixels)."
Preferences["Name"] = {
  "type": "string",
  "value": "John Doe",
  "default": "John Doe",
  "desc": "Author name:",
  "help": "Fill in first and last name of author."
};

This makes it easy for the rest of your code to simply load and eval() this file.

There are currently five preference types supported: Lists, Boolean, Folder, Range and String.

A list will be displayed as a drop down list.
A boolean will be displayed as a checkbox.
A folder will be represented by a text field and a "Choose..."-button
A range will be represented by a slider.
A string is just an editable text field.

As can be seen in the Preference settings file above, each property has a current value, a default value, a text (which is shown in the dialog), and a help text (which is shown when the user hovers with the mouse over the corresponding entry). Some properties have extra parameters, for instance a list have a discrete set of values and the value is an index into this. A range has minimum and maximum values.

Using this package is easy, as shown in the following example:
Code: Select all//@include "Preferences.jsx"

// This is the Preference file
var prefs = Folder.userData + "/Preference-settings.jsx";

// This will load the preferences, open a dialog and then save the preferences again
Prefs.dialog(prefs);

Any further code that needs access to the preference parameters can simply do:
Code: Select allPrefs.loadPreferences(prefs)

and access the values like Preferences["color"]["value"] or Preferences["Thickness"]["value"].

The code that does all the work is Preferences.jsx and can be found in the attachment.

Obviously I take no responsibility if this code would e.g. launch a missile attack on North Korea, spill urine on your neighbor's door, etc. I hope that any improvements to the code can be shared with the rest of this helpful forum.