← Back to Docs

Scripting with Variables

Most useful for free-tier users, who have one Action slot and want it to behave differently per desktop. Pro users can just create one Action per desktop.

A single Action can do different things for different desktops if its command branches on {to_name} or {to_id}. Set When to Any desktop switch so the Action fires on every switch, then let the command decide what to do based on which desktop you just landed on.

For a concrete worked-out application of this pattern, see the Rainmeter layouts per desktop guide.

Inline branching with cmd

For two or three desktops, an if chain inside the command itself is enough:

  • Command: if "{to_name}"=="Work" (start "" "C:\projects\app\dev.bat") else if "{to_name}"=="Focus" (start "" "C:\Tools\dnd-on.exe") else echo No action for {to_name}

Past three or four branches this gets unreadable. Move the logic into a script file instead.

Dispatch from a PowerShell script

Put the per-desktop logic in C:\scripts\on-switch.ps1:

param([string]$ToName, [string]$FromName)

switch ($ToName) {
  "Work" {
    & "C:\projects\app\dev.bat"
  }
  "Personal" {
    Start-Process "C:\Users\me\Documents\todo.md"
  }
  "Focus" {
    & "C:\Tools\dnd-on.exe"
  }
  default {
    # No action for other desktops
  }
}

Then point your Action at the script:

  • When: Any desktop switch
  • Command: powershell -NoProfile -ExecutionPolicy Bypass -File "C:\scripts\on-switch.ps1" -ToName "{to_name}" -FromName "{from_name}"

To add a new desktop, edit the switch in the script. Nothing in WorkTop has to change.

Branching on desktop ID instead of name

Branching on names breaks the moment you rename a desktop. If you rename often, or want to share the same script across machines, branch on {to_id} (the desktop GUID) instead. You can find the GUIDs in the app log; pin them in the script:

switch ($ToId) {
  "11111111-2222-3333-4444-555555555555" { # Work
    & "C:\projects\app\dev.bat"
  }
  "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee" { # Personal
    Start-Process "C:\Users\me\Documents\todo.md"
  }
}

The trade-off is the script is harder to read at a glance. Names are friendlier; IDs are stable.

When to upgrade instead

The single-Action pattern works but gets harder to maintain as the script grows. If you find yourself editing it every time you add a desktop, or you want different events per desktop (some firing on switch-to, some on switch-away), creating separate Actions is much simpler. That's what the Pro tier is for.