General purpose Preference tool

photoshop script snippets - note full scripts go in the photoshop scripts forum

Moderator: PS-Moderators

General purpose Preference tool

Postby kpt » Thu Apr 22, 2010 12:59 am

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 all
Preferences["color"] = {
  "type" : "list",
  "value": 0,
  "default": 0,
  "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

Any further code that needs access to the preference parameters can simply do:
Code: Select all

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.
(3.13 KiB) Downloaded 325 times
Posts: 196
Joined: Mon Apr 23, 2007 3:54 pm

Return to Photoshop Scripting: Code Snippets

Who is online

Users browsing this forum: No registered users and 1 guest