It is currently December 3rd, 2021, 6:19 am

Show current virtual desktop name

Tips and Tricks from the Rainmeter Community
randyc
Posts: 2
Joined: August 10th, 2021, 3:14 am

Show current virtual desktop name

Post by randyc »

Problem

When you switch desktops in Windows 10, there is no indication of which desktop you just left and which you are entering.

Existing solutions
  • I have seen cases in this forum and elsewhere of people asking about solutions and never getting an answer except for one answer that claimed that it is impossible because Windows does not expose that information.
  • There is an AutoHotkey script that put the desktop number in the system tray, but I could never get it to work on any of my systems, the number was too small to be useful to me, anyway, and it’s no longer maintained.
The solution I use

Find some system command that returns the desktop name, and print that in a Rainmeter meter.

A weakness of this approach is that it is probably too slow to allow for making desktop-switching animations.

The PowerShell command

I use the Get-DesktopName PowerShell commandlet from the VirtualDesktop package. Note that this is a 3rd-party package, so you are taking on some risk by using it, but I think the author has a good reputation and has created other useful packages. I also had to modify settings to allow execution of PowerSell scripts, which, again, is a risk.

System requirements Process to install/run
  1. Run this install command in PowerShell (source: PowerShell Gallery):

    Code: Select all

    Install-Module -Name VirtualDesktop
  2. Try running the command in PowerShell:

    Code: Select all

    Get-DesktopName
    If it runs without error and prints out something like “Desktop 1”, great. Then skip the next step and move on to writing your RainMeter script.
  3. The command in step 2 probably returned an error that is something like
    Get-DesktopName : The 'Get-DesktopName' command was found in the module 'VirtualDesktop', but the module could not be
    loaded. For more information, run 'Import-Module VirtualDesktop'.
    If so,
    1. first do what the error says and run

      Code: Select all

      Import-Module VirtualDesktop
      You will probably get an error like
      Import-Module : File C:\Program Files\WindowsPowerShell\Modules\VirtualDesktop\1.3.1\VirtualDesktop.psm1 cannot be
      loaded because running scipts is disabled on this system. For more information, see about_Execution_Policies at
      https:/go.microsoft.com/fwlink/?LinkID=135170.
      If you do, keep going. If you do not, report the results in a comment for discussion.
    2. Start PowerShell in Administrator mode (there are lots of ways to do this; it is usually in a right-click context menu).
    3. Set the execution policy to allow running scripts with (a possibly improved version of)

      Code: Select all

      Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
      This may not the best way to do it, so I hope someone who knows more about PowerShell will speak up and explain all the ways that I just exposed myself to malware and what to do instead.
    4. Try running Get-DesktopName again to see if the execution policy change solved the problem

The RainMeter script

This script uses the RunCommand plugin in a measure and the !CommandMeasure Rainmeter bang. My implementation has the bang triggered by the skin update so that the skin always shows the current desktop. You could probably easily make it instead be triggered any time you use the desktop-switching shortcut keys, but I worry about being unsure about which desktop is being reported in that case (please do respond with a comment if you have a fast solution that could handle switching animations). Or you might want a widget that is a location for you to click to show the desktop number.

Code: Select all

[Rainmeter]
; Most important part:
OnUpdateAction = [!CommandMeasure MeasureVDeskName "Run"]
; Update every 0.1 seconds: 
Update = 100     
; The rest of the Rainmeter section is just settings for a simple widget
; You will probably want to change them
BackgroundMode = 2
SolidColor = 40,40,40,200
DynamicWindowSize = 1
AccurateText = 1

; Measure to get the desktop name 
[MeasureVDeskName]
Measure = Plugin
Plugin = RunCommand
Program = PowerShell
Parameter = Get-DesktopName
; Don’t open a PowerShell window 10 times per second:
State = Hide
OutputType = ANSI

; Display the name in a string meter
[MeterResult]
MeasureName = MeasureVDeskName
Meter = String
Text = %1
FontColor = 240,240,240
randyc
Posts: 2
Joined: August 10th, 2021, 3:14 am

Re: Show current virtual desktop name

Post by randyc »

Update: I think this skin has been a resource hog that severely reduced battery life, so I cannot recommend it anymore.

I have had problems with battery life recently, and Windows battery settings says that 37% of the battery use in the last 24 hours was from Powershell. I have done almost nothing else in Powershell in that time. Firefox, which I used for many hours during that time only used 30%. I had the skin set to refresh every 200ms, which is much more often than necessary. Having it update every second would likely cut down the battery drain from 37% to around 7%, but that is still far too much. If you do not mind a few seconds of delay after switching desktops, you could set it to update at a lower frequency. A better solution is likely to run the Powershell command only when the desktop changes, but I am not sure how to make that work.
User avatar
death.crafter
Rainmeter Sage
Posts: 1205
Joined: April 24th, 2021, 8:13 pm

Re: Show current virtual desktop name

Post by death.crafter »

randyc wrote: September 3rd, 2021, 8:17 pm Update: I think this skin has been a resource hog that severely reduced battery life, so I cannot recommend it anymore.

I have had problems with battery life recently, and Windows battery settings says that 37% of the battery use in the last 24 hours was from Powershell. I have done almost nothing else in Powershell in that time. Firefox, which I used for many hours during that time only used 30%. I had the skin set to refresh every 200ms, which is much more often than necessary. Having it update every second would likely cut down the battery drain from 37% to around 7%, but that is still far too much. If you do not mind a few seconds of delay after switching desktops, you could set it to update at a lower frequency. A better solution is likely to run the Powershell command only when the desktop changes, but I am not sure how to make that work.
May be try PowershellRM plugin.
from the Realm of Death