|
20 | 20 | https://github.com/unalignedcoder/auto-theme/ |
21 | 21 |
|
22 | 22 | .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 |
27 | 26 | #> |
28 | 27 |
|
29 | 28 | # ============= Script Version ============== |
30 | 29 |
|
31 | 30 | # This is automatically updated |
32 | | - $scriptVersion = "1.0.42" |
| 31 | + $scriptVersion = "1.0.43" |
33 | 32 |
|
34 | 33 | # ============= Config file ============== |
35 | 34 |
|
|
441 | 440 |
|
442 | 441 | # Handles the creation and management of the native wallpaper slideshow task |
443 | 442 | function Set-WallpaperRotationTask { |
444 | | - param ( |
445 | | - [string]$Mode # "light" or "dark" |
446 | | - ) |
| 443 | + param ( |
| 444 | + [string]$Mode # "light" or "dark" |
| 445 | + ) |
447 | 446 |
|
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." |
451 | 451 |
|
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 | + } |
454 | 461 |
|
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 | + } |
462 | 468 |
|
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) |
469 | 471 |
|
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`"" |
478 | 474 |
|
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. |
483 | 486 | $task = Get-ScheduledTask -TaskName $TaskName |
484 | 487 |
|
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")) |
488 | 490 |
|
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 |
494 | 494 |
|
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 | + |
496 | 499 | Set-ScheduledTask -InputObject $task | Out-Null |
497 | 500 |
|
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 | + |
499 | 504 |
|
500 | 505 | } catch { |
501 | 506 |
|
502 | 507 | Write-Log "Failed to register wallpaper rotation task: $_" |
503 | 508 | } |
504 | | - } |
| 509 | + } |
505 | 510 |
|
506 | 511 | # Helper to launch processes without admin privileges |
507 | 512 | function Start-ProcessUnelevated { |
|
1041 | 1046 | param ( |
1042 | 1047 | [DateTime]$NextTriggerTime, |
1043 | 1048 | [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 |
1045 | 1052 | ) |
1046 | 1053 |
|
1047 | 1054 | # Schedule next run |
1048 | 1055 | Write-Log "Setting scheduled task: $Name" |
1049 | 1056 |
|
1050 | 1057 | # 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" |
1052 | 1059 |
|
1053 | 1060 | # Logic to determine the executable and arguments based on $terminalVisibility |
1054 | 1061 | switch ($terminalVisibility) { |
|
1554 | 1561 |
|
1555 | 1562 |
|
1556 | 1563 |
|
| 1564 | + |
0 commit comments