Const WALLPAPER_SINGLE_MON = "Hawaii" 'name of the wallpaper profile to be used when only a single monitor is enabled
Const WALLPAPER_MULTI_MON = "Surfing Theme" 'name of the wallpaper profile to be used when multiple monitors are enabled
Const CHECK_INTERVAL = 3 'number of seconds the script waits between checks for display configuration changes
Const WAIT_BEFORE_UPDATE = 3 'number of seconds the script waits before updating the wallpaper
Const UM_DESKTOP_EXE = "%ProgramFiles%\UltraMon\UltraMonDesktop.exe" 'full path and name of UltraMonDesktop.exe

Set sys = CreateObject("UltraMon.System")
Set sh = CreateObject("WScript.Shell")

'check if UltraMon 3 or later is installed
umVer = sh.RegRead("HKLM\Software\Realtime Soft\UltraMon\CurrentVersion")

'get the location of the wallpaper folder
dirWallpapers = ""
If umVer = "" Then
	'UltraMon 2
	MsgBox "This script requires UltraMon version 3 or later.",,  WScript.ScriptName
	WScript.Quit
Else
	'UltraMon 3 or later
	dirWallpapers = sh.ExpandEnvironmentStrings("%APPDATA%\Realtime Soft\UltraMon\" & umVer & "\Wallpapers")
End If

singleMon = False
If sys.NumActiveMonitors = 1 Then singleMon = True
LoadWallpaper singleMon

Do While True
	If sys.NumActiveMonitors = 1 And singleMon = False Then
		singleMon = True
		LoadWallpaper singleMon
	ElseIf sys.NumActiveMonitors > 1 And singleMon = True Then
		singleMon = False
		LoadWallpaper singleMon
	End If
	
	WScript.Sleep CHECK_INTERVAL * 1000
Loop

Sub LoadWallpaper(singleMon)
	wallpaper = ""
	If singleMon = True Then
		wallpaper = WALLPAPER_SINGLE_MON
	Else
		wallpaper = WALLPAPER_MULTI_MON
	End If
	
	WScript.Sleep WAIT_BEFORE_UPDATE * 1000
	sh.Run """" & UM_DESKTOP_EXE & """ /load " & dirWallpapers & "\" & wallpaper & ".wallpaper"
End Sub
