Skip to content

Commit 15a88ba

Browse files
v1.0.43
- Fixed a problem that caused the wallpaper not to change if the PC was off a long time - Improved the at-setup.ps1 script for the creation of the main scheduled task - Many minor fixes
1 parent 79fc9cc commit 15a88ba

File tree

3 files changed

+89
-63
lines changed

3 files changed

+89
-63
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,3 @@ Workarounds have been added for a number of apps which do not toggle theme grace
6767

6868
[^1]: `.theme` files are simple configuration instructions for the Windows pesonalization engine. You can find two examples included in this repository, with helpful comments.
6969
[^2]: To run a PowerShell script on Windows, you need to set `Execution Policy` in PowerShell, using this command: `Set-ExecutionPolicy RemoteSigned` as Administrator.
70-
71-

at-setup.ps1

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
# ============= Config file ==============
1717

18-
$ConfigPath = Join-Path $PSScriptRoot "Config.ps1"
18+
$ConfigPath = Join-Path $PSScriptRoot "at-config.ps1"
1919

2020
# ============= FUNCTIONS ==============
2121

@@ -178,28 +178,48 @@
178178
}
179179
}
180180

181-
# IMPROVEMENT: Task Settings for Laptops
182-
# Tasks default to "Stop if on battery." We need to disable that for theme switching.
183-
$Settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable
181+
# 1. Define common settings used by both windows 10 and 11
182+
$CommonSettings = @{
183+
AllowStartIfOnBatteries = $true
184+
DontStopIfGoingOnBatteries = $true
185+
StartWhenAvailable = $true
186+
}
184187

185188
# Create the action
186189
$Action = New-ScheduledTaskAction -Execute $exe -Argument $arguments
187190

188191
# Register the task
189192
$windowsVersion = Get-WindowsVersion
193+
$TaskDescription = "Main Auto Theme task for scheduling sunrise/sunset events."
190194

191195
try {
192-
# Using -User $userSid ensures the task is tied to the correct account context
196+
193197
if ($windowsVersion -eq "Windows 10") {
194-
LogThis "Creating scheduled task for Windows 10." -verboseMessage $true
195-
Register-ScheduledTask -TaskName $TaskName -Trigger $Triggers -User $userSid -Action $Action -Settings $Settings -Description "Main Auto Theme task." -RunLevel Highest -Compatibility Win8 -Force | Out-Null
198+
199+
LogThis "Creating scheduled task for Windows 10 (Win8 Compatibility)." -verboseMessage $true
200+
201+
$Settings = New-ScheduledTaskSettingsSet @CommonSettings -Compatibility Win8
202+
203+
Register-ScheduledTask -TaskName $TaskName -Trigger $Triggers -User $userSid `
204+
-Action $Action -Settings $Settings -Description $TaskDescription `
205+
-RunLevel Highest -Force | Out-Null
206+
196207
} else {
197-
LogThis "Creating scheduled task for Windows 11." -verboseMessage $true
198-
# Added $Settings here too
199-
Register-ScheduledTask -TaskName $TaskName -Trigger $Triggers -User $userSid -Action $Action -Settings $Settings -Description "Main Auto Theme task." -RunLevel Highest -Force | Out-Null
208+
209+
LogThis "Creating scheduled task for Windows 11 (Native Compatibility)." -verboseMessage $true
210+
211+
# Windows 11 uses the most modern task engine by default
212+
$Settings = New-ScheduledTaskSettingsSet @CommonSettings
213+
214+
Register-ScheduledTask -TaskName $TaskName -Trigger $Triggers -User $userSid `
215+
-Action $Action -Settings $Settings -Description $TaskDescription `
216+
-RunLevel Highest -Force | Out-Null
200217
}
218+
201219
LogThis "Scheduled task '$TaskName' created successfully!" -Level Success
220+
202221
} catch {
222+
203223
LogThis "Critical error registering task: $_" -Level Error
204224
Pause
205225
Exit 1

at.ps1

Lines changed: 59 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@
2020
https://github.com/unalignedcoder/auto-theme/
2121
2222
.NOTES
23-
- Fixed loading/unloading of Rainmeter skins
24-
- Added description to the Scheduled Tasks
25-
- Attempt to correct suspect interference with Power settings
26-
- Several minor fixes
23+
- Fixed a problem that caused the wallpaper not to change if the PC was off a long time
24+
- Improved the at-setup.ps1 script for the creation of the main scheduled task
25+
- Many minor fixes
2726
#>
2827

2928
# ============= Script Version ==============
3029

3130
# This is automatically updated
32-
$scriptVersion = "1.0.42"
31+
$scriptVersion = "1.0.43"
3332

3433
# ============= Config file ==============
3534

@@ -441,67 +440,73 @@
441440

442441
# Handles the creation and management of the native wallpaper slideshow task
443442
function Set-WallpaperRotationTask {
444-
param (
445-
[string]$Mode # "light" or "dark"
446-
)
443+
param (
444+
[string]$Mode # "light" or "dark"
445+
)
447446

448-
# 1. Setup Variables
449-
$TaskName = "Auto Theme wallpaper changer"
450-
$wallPathToPass = if ($Mode -eq "light") { $wallLightPath } else { $wallDarkPath }
447+
# 1. Setup Variables
448+
$TaskName = "Auto Theme wallpaper changer"
449+
$wallPathToPass = if ($Mode -eq "light") { $wallLightPath } else { $wallDarkPath }
450+
$wallpaperDesc = "Triggers wallpaper change based on user-defined intervals. Managed by the main Auto Theme task."
451451

452-
# --- THE LOGIC CHECK ---
453-
$isFolder = Test-Path $wallPathToPass -PathType Container
452+
# --- THE LOGIC CHECK ---
453+
$isFolder = Test-Path $wallPathToPass -PathType Container
454+
if ($useThemeFiles -or $slideShowInterval -le 0 -or $noWallpaperChange -or -not $isFolder) {
455+
if (Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue) {
456+
Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false
457+
Write-Log "Wallpaper rotation not needed (Fixed image or disabled). Removed task." -verboseMessage $true
458+
}
459+
return
460+
}
454461

455-
if ($useThemeFiles -or $slideShowInterval -le 0 -or $noWallpaperChange -or -not $isFolder) {
456-
if (Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue) {
457-
Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false
458-
Write-Log "Wallpaper rotation not needed (Fixed image or disabled). Removed task." -verboseMessage $true
459-
}
460-
return
461-
}
462+
# 2. Verify worker script exists
463+
$workerScript = Join-Path $PSScriptRoot "at-wallpaper.ps1"
464+
if (-not (Test-Path $workerScript)) {
465+
Write-Log "Error: Worker script not found at $workerScript. Cannot schedule rotation."
466+
return
467+
}
462468

463-
# 2. Verify worker script exists
464-
$workerScript = Join-Path $PSScriptRoot "at-wallpaper.ps1"
465-
if (-not (Test-Path $workerScript)) {
466-
Write-Log "Error: Worker script not found at $workerScript. Cannot schedule rotation."
467-
return
468-
}
469+
# 3. Define the Start Time (1 interval in the future)
470+
$triggerTime = (Get-Date).AddMinutes([int]$slideShowInterval)
469471

470-
# 3. Define the Trigger (Starting 1 interval in the future)
471-
$triggerTime = (Get-Date).AddMinutes([int]$slideShowInterval)
472-
$wallpaperDesc = "Triggers wallpaper change based on user-defined intervals. Managed by the main Auto Theme task."
473-
474-
# 4. Call the Helper
475-
# We pass the custom description here; otherwise, Register-Task will
476-
# use its match-logic to assign the wallpaper description automatically.
477-
try {
472+
# 4. Prepare specific arguments for the worker script
473+
$workerArgs = "-Path `"$wallPathToPass`""
478474

479-
# 1. Register base task
480-
Register-Task -Name $TaskName -NextTriggerTime $triggerTime -Description $wallpaperDesc
481-
482-
# 2. Get task object
475+
try {
476+
# 5. Call the Helper with the CUSTOM script and arguments
477+
# This tells the task to run 'at-wallpaper.ps1' instead of 'at.ps1'
478+
Register-Task -Name $TaskName `
479+
-NextTriggerTime $triggerTime `
480+
-Description $wallpaperDesc `
481+
-CustomFile $workerScript `
482+
-Arguments $workerArgs
483+
484+
# 6. Apply the Repetition Patch
485+
# Register-Task creates a standard trigger; we must modify it for repeating intervals.
483486
$task = Get-ScheduledTask -TaskName $TaskName
484487

485-
# 3. Format interval as ISO 8601 (PT#M)
486-
# For 20 minutes, this results in "PT20M"
487-
$isoInterval = "PT$($slideShowInterval)M"
488+
# We switch to a Daily trigger so it persists across reboots
489+
$task.Triggers[0] = New-ScheduledTaskTrigger -Daily -At ($triggerTime.ToString("HH:mm"))
488490

489-
# 4. Apply Interval and a very long Duration (Indefinite)
490-
# "P1D" means a duration of 1 day, which allows the repetition to work.
491-
# Once the day is up, the task trigger (set to 'Once') handles the rollover.
492-
$task.Triggers[0].Repetition.Interval = $isoInterval
493-
$task.Triggers[0].Repetition.Duration = "P1D"
491+
# ISO 8601 format (PT#M) prevents the XML formatting error
492+
$task.Triggers[0].Repetition.Interval = "PT$($slideShowInterval)M"
493+
$task.Triggers[0].Repetition.Duration = "" # Indefinite repetition
494494

495-
# 5. Commit changes
495+
# Ensure it runs even if the PC was off during a scheduled tick
496+
$task.Settings.StartWhenAvailable = $true
497+
$task.Settings.MultipleInstances = "IgnoreNew"
498+
496499
Set-ScheduledTask -InputObject $task | Out-Null
497500

498-
Write-Log "Native Slideshow scheduled: Every $slideShowInterval minutes."
501+
Write-Log "Native Slideshow scheduled: Every $slideShowInterval minutes using $Mode wallpapers."
502+
Write-Log "Worker: $workerScript" -verboseMessage $true
503+
499504

500505
} catch {
501506

502507
Write-Log "Failed to register wallpaper rotation task: $_"
503508
}
504-
}
509+
}
505510

506511
# Helper to launch processes without admin privileges
507512
function Start-ProcessUnelevated {
@@ -1041,14 +1046,16 @@
10411046
param (
10421047
[DateTime]$NextTriggerTime,
10431048
[String]$Name,
1044-
[String]$Description = "Managed by the Auto Theme script."
1049+
[String]$Description = "Managed by the Auto Theme script.",
1050+
[String]$CustomFile = $PSCommandPath, # Default to at.ps1
1051+
[String]$Arguments = "" # Optional extra arguments
10451052
)
10461053

10471054
# Schedule next run
10481055
Write-Log "Setting scheduled task: $Name"
10491056

10501057
# Define base PowerShell arguments
1051-
$psArgs = "-WindowStyle Hidden -ExecutionPolicy Bypass -NoProfile -NonInteractive -File `"$PSCommandPath`""
1058+
$psArgs = "-WindowStyle Hidden -ExecutionPolicy Bypass -NoProfile -NonInteractive -File `"$CustomFile`" $Arguments"
10521059

10531060
# Logic to determine the executable and arguments based on $terminalVisibility
10541061
switch ($terminalVisibility) {
@@ -1554,3 +1561,4 @@
15541561

15551562

15561563

1564+

0 commit comments

Comments
 (0)