# Run Command

An entry's 'Run Command' field can contain a command that will be run when the Run Command action is selected. Anything that you can do in Windows' "Start → Run" can be used here.

In addition, Password Safe is able to pass arguments to the command such as the entry's username, password, etc., as described below. This allows you to 'program' fairly complex actions, such as invoking a remote access program (PuTTY, for example) and passing it the username and password associated with the entry.

In general, 'variables' are replaced with values associated with them before being passed to the computer for execution. The dollar ('$') sign is used to indicate a variable.

- Variables are specified as '$name' and are case sensitive. Variable names always start with an alphabetic character and the remaining characters are alphanumeric. The variable name is terminated either by encountering a non-alphanumeric character (such as a space) or the end of the Run Command.

- To use a variable immediately followed by an alphanumeric character in the Run Command, enclose the variable's name in curly brackets e.g., '${varname}string'.
- To use a '$' in the string as normal text (not indicating that the following text is a variable name), escape it using a back-slash ('\') e.g., '\$text' becomes '$text' and is not treated as a variable called 'text'.
- To use a back-slash before a '$' as a back-slash, escape it with a back-slash e.g. '\\$name' becomes '\$name' in the resulting text and '$name' is treated as a variable. A back-slash does not have to be escaped unless it precedes a '$' indicating a following variable.
- A variable may be followed by a numerical value within square brackets is called an 'Index value'. The index value 'n' can be preceded by '+' or '-' e.g. [-2] or [2] or [+2]. Password Safe treats an index that equates to zero as meaning the entire field.

- The following variables are currently recognised:

- Application related:

appdir | Directory containing the Password Safe application. See notes 1 & 2 below.

- Database related:

dbdir | Directory of current open database. See notes 1 & 2 below.
fulldb | Full path, filename and extension of current open database. See note 2 below.
dbname | Current open database filename.
dbextn | Current open database extension.

- Current entry related:

g or group | Group (full tree structure back to root with groups separated by periods).
G or GROUP | The group containing this entry.
t or title | Title
u or user | Username
p or password | Password
e or email | e-mail
a or autotype | AutoType. See Note 5 below.
url | URL
n or n[0] | The complete Notes field (also: notes or notes[0]). See note 3 below.
n[i] or notes[i] | ith line of the Notes field counting from the start, i.e., i=1 is the first line, i=2 means the second line etc. See note 4 below.
n[-i] or notes[-i] | ith line of the Notes field counting from the end, i.e., i=1 is the last line, i=2 means the penultimate line etc. See note 4 below.

Notes |
--- | ---
1 | Directory values (appdir or dbdir) do not have an ending '\'. Remember to add this if you need it in your Run Command.
2 | The contents of variables (including directory and full path values) may contain blanks. You may need to enclose the result in quotes in your Run Command for correct execution.
3 | If the Notes field contains more than one line, it will include any embedded carriage return (CR) or line feed (LF). This may cause issues when executed as CR and/or LF will be taken as 'enter'.
4 | Any trailing CR and/or LF are removed from single lines selected from the Notes field.
5 | If the AutoType variable is present in the Run Command, AutoType will be performed once the target window is active. The variable and its value, if present, will be removed from the string that is executed. This variable can appear anywhere in the Run Command as it only specifies what Password Safe will do after the command string has been executed. Unlike other variables, this one will be able to take a value to override the entry's current value. The format is as follows ('$autotype' can replace '$a' in the following examples):
$a | Use current entry value.
${a} | Use current entry value (used if text immediately follows the '$a' variable).
$a(value) | Use AutoType string specified between the round brackets.
${a}(value) | Use AutoType string specified between the round brackets.

Note: Be sure to add quotes around your string if required e.g., if your database directory contains spaces, to access a text file in it based on your open database name, you would need the following (with quotes):

    '$dbdir\\$dbname.txt'

Notice that the backslash is escaped so that the '$dbname' is recognised as a variable.

On Windows, the normal substitution of environmental variables (specified as %variable_name%) is performed. For example: '%windir%\notepad.exe' could become (depending on your installation) 'C:\Windows\notepad.exe'. Current values set in your environment can be determined by running 'cmd.exe' and issuing the 'set' command without any parameters. Any unknown environmental variable is left unchanged in the Run Command e.g., %xyz%\notepad.exe remains as %xyz%\notepad.exe if variable 'xyz' is not set. Note: Windows environmental variables are not case sensitive.

### Examples

- The following Run Command will cause the Putty program to connect to myHost giving the entry's username and password:

      "C:\Program Files\PuTTY\putty.exe" -ssh ${u}@myHost 22 -pw $p

- This will open a new instance of Password Safe with the specified database (Thanks to Dave for this one):

       ${autotype}(\p\n)"${appdir}\pwsafe.exe" \UNC\path\to\database.psafe3

- Make sure Manage → Options → System "Allow multiple instances" is checked for this to work
- You might also want to set the Double-Click Action for this entry to "Run Command"