In a previous article we introduced a new packager engine which enables you to create System Tray applications with PowerShell. We have now extended that a little bit and added the ability to create custom menu entries in the tray application’s context menu.
I will take the same code we used in the previous article and show you how to add the custom menu topic and the corresponding handler. The download link for the code is at the end of this article.
To enable you to do anything with the underlying engine which runs your script, we had to add an object which is available as $SAPIENHost. Since this particular object only exists in packaged engines, you need to make sure to check for its existence before using it.
So all custom menu code should be enclosed in if($SAPIENHost -ne $null) { …. }
Let’s look at the code. There are three parts:
This is all normal .NET menu stuff, so nothing exciting.
The SetMenuHandler call has two parameters: The actual menu item object (ToolStripMenuItem), and the name of a script block which will handle that menu item—in our case $Handle_OnStartup.
This script block is called each time a user selects the menu item on the tray application’s context menu.
There are a few things to consider:
As you may remember, the main code of our tray application is a loop which sleeps between iterations. Your menu handler is called from a separate thread, which can be problematic. In our example, the menu handler does not interact with anything in the main loop so there is no problem.
If you want to interact with objects in your main script loop, make sure to consider or change the current state of your main operation before accessing and modifying any objects.
You can download the script and the packaged executables here.
You will need PrimalScript 2019 7.4.122 or later to use this. PowerShell Studio 2019 should include the new engines with version 5.6.158 or after. Make sure to check the version log when in doubt.