I want to add a parameter in a protocol that allows the user to configure a script that should be used later for some actions.
How should I go about validating the script name to make sure the input provided is a valid script and that would not cause issues when trying to execute it?
Hi Maria,
There are a few options you can implement depending on how simple or advanced you want your validation to be.
The simplest would be to have a string parameter and have your write parameter trigger a QAction for you to perform some validations.
To validate you could use a logic similar to the snippet below and that would already check if the input provided is a valid script.
private static bool IsValidScriptName(SLProtocol protocol, string scriptName)
{
if (String.IsNullOrWhiteSpace(scriptName))
{
return false;
}var response = (GetScriptsResponseMessage)protocol.SLNet.SendSingleResponseMessage(new GetInfoMessage(InfoType.Scripts));
var scriptNames = response.Scripts;
return scriptNames.Contains(scriptName, StringComparer.OrdinalIgnoreCase);
}
You could then use something like protocol.ShowInformationMessage to present the user with a message stating the provided input is invalid, see docs for more info about the ShowInformationMessage method, paying particular attention to the remarks that say this pop-up window will only work from QActions that are directly triggered from a client with user interaction.
This should work for your use case but I do have to add a disclaimer here that these kinds of SLNet calls are internal calls and typically not recommended to use since there is no official support and may be subject to change in the future.
If you want to go for a more advanced approach you could use the same SLNet call as above to pre-populate a dynamic discreet with possible values and allow the user to select from those.
See dependencyId for the option that allows you to create a dynamic discreet by providing a semicolon-separated list of items.
Ideally, this would be coupled with a refresh button so the user can rescan the system for new scripts.
A third option would be to do something similar to the second option I mention but instead of fetching all scripts on the system, you could limit your search to just one or a couple of folders where the script may reside.
This would reduce the list size and improve usability so the user can find what he needs faster.
See snippet below
private static Dictionary<string, List<string>> GetScriptsGroupByFolder(SLProtocol protocol)
{
GetAutomationInfoMessage message = new GetAutomationInfoMessage(21, String.Empty);
var allScripts = (GetAutomationInfoResponseMessage)protocol.SLNet.SendSingleResponseMessage(message);List<SA> list = new List<SA>();
if (allScripts != null)
{
list.AddRange(allScripts.psaRet.Psa);
}Dictionary<string, List<string>> dictionary = new Dictionary<string, List<string>>();
foreach (SA item in list)
{
if (item == null || item.Sa == null || !item.Sa.Any() || item.Sa[0] == null)
{
continue;
}string key = item.Sa[0];
List<string> list2 = new List<string>();
for (int i = 1; i < item.Sa.Length; i++)
{
string text = item.Sa[i];
if (!string.IsNullOrWhiteSpace(text))
{
list2.Add(text);
}
}dictionary.Add(key, list2);
}return dictionary;
}