PowerShell Studio: Custom PrimalSense
- Details
- Written by David Corrales
- Last Updated: 13 June 2016
- Created: 15 March 2016
- Hits: 9928
Custom PrimalSense Lists
The customizable PrimalSense feature allows users to tailor their PrimalSense experience. The contents of the custom PrimalSense can be derived from a static list or from a dynamically created list using a PowerShell script.
These custom PrimalSense lists can be triggered via a shortcut name, similar to snippet shortcuts. For example, if you type the following shortcut:
To display the custom PrimalSense list, press [Ctrl + Alt + P] or [TAB].
The shortcut will be replaced with the selected item in the PrimalSense list.
In addition, custom PrimalSense lists can be triggered by typing a particular cmdlet / parameter combination or a specific parameter regardless of command.
The custom list in this case is triggered when the user presses [SPACE] after typing the Name parameter of Get-Process.
Let’s look at the types of lists in more detail.
Static Lists
Static lists are the simplest method for creating a custom PrimalSense list. These lists are simply read from a static data file source, such as a CSV file, when the list is triggered. Let’s look at the supported data file formats.
Data File Formats
There are two supported data file formats: CSV and XML files.
CSV Format
The CSV is the simplest format. It can consist of simple list where there can only be one list item per line.
For example, GetProcess_Name.csv data file contains the following:
Note: Since this is a CSV file format, you must escape double quotes using another double quote “”.
You have the option to associate each item with a pre-defined picture (via index) and a value using the following format:
Name, Index, Value
- Name – This is the text displayed in the PrimalSense list.
- Index – (Optional) The numeric index of the image you that will be displayed next to the name in the PrimalSense list. Use –1 if you do not wish to associate an image. See below for a list of image indexes.
- Value – (Optional) The value that is inserted into the editor when the item is selected from the PrimalSense list.
Here is an example csv that uses images and values:
The resulting PrimalSense list:
The inserted value:
The following table contains the list of supported image indexes:
Index | Image |
0 | Keyword |
1 | Folder |
2 | Code Snippet |
3 | Namespace |
4 | Assembly |
5 | Assembly Checked |
6 | Public Class |
7 | Private Class |
8 | Public Property |
9 | Public Field |
10 | Public Interface |
11 | Public Delegate |
12 | Public Structure |
13 | Public Method |
14 | Public Enumeration |
15 | Enumeration Item |
16 | Cmdlet |
17 | Alias |
18 | Variable |
19 | Function |
20 | Document Function |
21 | Global Function |
22 | Static Method |
23 | Static Property |
24 | Static Field |
25 | Snapin |
26 | Module |
27 | Operator |
28 | Help |
29 | Object Browser |
30 | Parameter |
31 | Parameter Alias |
32 | Job |
33 | File |
34 | External Tool |
35 | Workflow |
36 | Configuration |
37 | Event |
38 | Breakpoint |
39 | Tracepoint |
40 | Caret |
41 | Last Edit Position |
42 | Syntax Error |
43 | Debug Position |
44 | Region |
45 | Bookmark |
46 | Parameter Set |
47 | Task |
48 | Task Completed |
49 | Computer |
50 | Gear |
XML Format
The second supported data format is XML. Use the XML file format to insert more complex values, such as a multiple line script.
The XML file consists of a root element named List, which contains a child element for each item named Item.
The item element contains the following attributes:
- name – contains the display name of the item in the PrimalSense list.
- image – contains the image index of the item.
A CDATA block contains the multi-line text that is inserted when the item is selected in the PrimalSense list.
For example, the functions shortcut triggers the following list of functions:
When an item is selected, a complete function is inserted:
Dynamic Lists
For the cases where a static list isn’t enough, PowerShell Studio gives you the option to dynamically create the data file before displaying it in the PrimalSense list. This is accomplished, by running a custom PowerShell script that generates the necessary data file. Next, we will look at how to configure triggers and create scripts that generate dynamic lists.
Configuring Triggers
The custom PrimalSense list triggers are stored in the following XML file:
C:\ProgramData\SAPIEN\PowerShell Studio 2016\CustomSense\Triggers.xml
The XML file is made up of a Triggers root element and each individual list trigger is defined by a child element named Trigger.
Each Trigger element has the following attributes:
- shortcut – (Optional) The shortcut name that triggers the custom PrimalSense list when the user presses [Ctrl + Alt + P]. The shortcut must not contain spaces.
- command – (Optional) The command/cmdlet that the trigger is bound to. Use an asterisk * to denote all commands. This field requires the parameter attribute.
- parameter – (Optional) The parameter that triggers the custom PrimalSense list. This field requires the command attribute.
- type – [cached|dynamic] (Default: cached) Determines if the list is cached in memory or is read every time the list is triggered.
- datafile – (Required) Specifies the data file that is to be loaded (CSV or XML).
- elevated – (Optional) (Default: False) Determines if the script needs to be elevated.
- timeout – How long PowerShell Studio should wait for the process to complete before quitting.
- autoquote – [True|False] (Default: True) If set to True, PowerShell Studio will automatically wrap quotes around the values contained in a CSV file when necessary.
- scriptfile – Specifies the script to run in order to create the data file.
Note: If you modify the Triggers.xml file, you will need to reload PowerShell Studio in order for the changes to take effect.
Script Files
When you specify a script file in the triggers XML file, PowerShell Studio will execute said script when the list is triggered.
Let us look at the contents of the sample GetProcess_Names.ps1 script:
Get-Process | Select-Object -ExpandProperty Name -Unique > "$CustomSensePath\GetProcess_Name.csv" |
The script simply pipes out the results of Get-Process to a basic csv file. Notice the $CustomSensePath variable, this is a special variable that contains path information. You can utilize the following variables in your script:
$CustomSensePath
This variable contains the path to the CustomSense folder. For example:
C:\ProgramData\SAPIEN\PowerShell Studio 2016\CustomSense\
Note: The data file must be located in the same folder as the triggers.xml.
$CurrentFile
This variable contains the file path to the active script in PowerShell Studio. This value will be empty for new unsaved files.
You should now have a better understanding of how to use PowerShell Studio’s Custom PrimalSense feature. This feature is another tool you can use to tailor PowerShell Studio to fit your specific needs.
If you have any comments or feedback, please feel free to post in our PowerShell Studio product forum.
For licensed customers, use the forum associated with your product in our Product Support Forums for Registered Customers.
For users of trial versions, please post in our Former and Future Customers - Questions forum.