-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoTheme.ps1
577 lines (423 loc) · 17 KB
/
AutoTheme.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
<#
.SYNOPSIS
Changes the active Windows theme based on a predefined schedule.
.DESCRIPTION
This Powershell script automatically switches the Windows theme based on Sunrise and Sunset, or hours set by the user.
Rather than using registry/system settings, it selects a given .theme file. This allows for a much higher degree of customization.
The script is designed to run in the background as a scheduled task, ensuring that the system theme is updated without user intervention.
It only connects to the internet to verify Location and Sunrise and Sunset times depending on user location.
Alternatively, it can use hours provided by the user, thus staying offline.
The script is meant to be ran from Task Scheduler, and it will automatically create the next temporary task.
If otherwise the script is run from terminal, as './AutoTheme.ps1', it only switches between the themes.
#>
# Script version
$scriptVersion = "1.0.16"
# ============= Config file ==============
$ConfigPath = "$PSScriptRoot\config.ps1"
# ============= FUNCTIONS ==============
# Determine if the script runs interactively
function IsRunningFromTerminal {
# Get the current process ID
$proc = Get-CimInstance Win32_Process -Filter "ProcessId = $pid"
# Get the parent process ID
$parent = Get-CimInstance Win32_Process -Filter "ProcessId = $($proc.ParentProcessId)"
# Check if the parent process is 'svchost.exe' and contains 'Schedule' in the command line
if ($parent.Name -eq "svchost.exe" -or $parent.CommandLine -like "*Schedule*") {
return $false
} else {
return $true
}
}
# Create the logging system
function LogThis {
param (
[string]$message,
[bool]$verboseMessage = $false # Default to false if not specified
)
# Only proceed if in debug mode
if ($log) {
<# Check for verbosity:
If the message is verbose, but verbose is false, end the Function
If the message is verbose, but verbose is true, continue
If the message is not verbose, continue #>
if ($verboseMessage -and -not $verbose) {
return # Skip logging if message is verbose and $verbose is set to false
}
# Display log output depending on session type
if (IsRunningFromTerminal) {
Write-Output "$message" # Output to console
} else {
Add-Content -Path $logFile -Value "$message" # Log to file
}
}
}
# Handle BurntToast Notifications
function Show-BurntToastNotification {
param(
[string]$Text,
[string]$AppLogo
)
# Install the BurntToast module if not already installed
<# if (-not (Get-Module -Name BurntToast -ListAvailable)) {
Install-Module -Name BurntToast -Scope CurrentUser
} #>
# for when the above is commented out or fails, we double-check.
if (Get-Module -Name BurntToast -ListAvailable) {
try {
$logoFullPath = Join-Path -Path $PSScriptRoot -ChildPath $AppLogo
New-BurntToastNotification -Text $Text -AppLogo $logoFullPath
LogThis "Displayed BurntToast notification with text: $Text and logo: $logoFullPath" -verboseMessage $true
} catch {
LogThis "Error displaying BurntToast notification: $_" -verboseMessage $true
}
} else {
LogThis "BurntToast module is not installed. Cannot display system notifications." -verboseMessage $true
}
}
# Check if the script has been run in the last hour
function LastTime {
LogThis "Checking if script was run in the last $interval minutes" -verboseMessage $true
if (Test-Path $lastRunFile) {
$lastRun = Get-Content $lastRunFile | Out-String
$lastRun = [DateTime]::Parse($lastRun)
$now = Get-Date
$timeSinceLastRun = $now - $lastRun
if ($timeSinceLastRun.TotalMinutes -lt $interval) {
LogThis "Script was run within the last $interval minutes. Exiting."
exit
}
}
}
# Update the last run time
function UpdateTime {
$now = Get-Date
$now | Out-File -FilePath $lastRunFile -Force
}
# Return coordinates for Sunrise API (may require Internet connectivity)
function LocateThis {
param (
[double]$FallbackLatitude = $UserLat,
[double]$FallbackLongitude = $UserLng,
[string]$FallbackTimezone = $UserTzid
)
LogThis "Getting location coordinates." -verboseMessage $true
# If $UseUserLoc is set to true, return user-defined coordinates and timezone
if ($UseUserLoc) {
LogThis "Using user-defined coordinates and timezone." -verboseMessage $true
return @{
Latitude = $FallbackLatitude
Longitude = $FallbackLongitude
Timezone = $FallbackTimezone
}
}
# Attempt to get location and timezone from Device Geolocation
try {
Add-Type -AssemblyName 'Windows.Devices.Geolocation'
$geolocator = New-Object Windows.Devices.Geolocation.Geolocator
$position = $geolocator.GetGeopositionAsync().GetAwaiter().GetResult()
$UserLat = $position.Coordinate.Point.Position.Latitude
$longitude = $position.Coordinate.Point.Position.Longitude
$UserTzid = [System.TimeZone]::CurrentTimeZone.StandardName
LogThis "Retrieved device location and system timezone." -verboseMessage $true
return @{
Latitude = [double]$UserLat
Longitude = [double]$longitude
Timezone = $UserTzid
}
}
catch {
LogThis "Device location and timezone retrieval failed. Trying online service." -verboseMessage $true
}
# Attempt to get location and timezone from online service
try {
$response = Invoke-RestMethod -Uri "http://ip-api.com/json"
if ($response.status -eq "success") {
LogThis "Retrieved location and timezone from online service."
return @{
Latitude = [double]$response.lat
Longitude = [double]$response.lon
Timezone = $response.timezone
}
}
}
catch {
LogThis "Online service location and timezone retrieval failed. Using fallback." -verboseMessage $true
}
# Fallback to user-defined coordinates and timezone if all else fails
LogThis "Using user-defined coordinates and timezone." -verboseMessage $true
return @{
Latitude = $FallbackLatitude
Longitude = $FallbackLongitude
Timezone = $FallbackTimezone
}
}
# Check whether shuffle is enabled in the .theme file
function DoWeShuffle {
param (
[string]$themeFilePath
)
LogThis "Checking if the theme shuffles wallpapers" -verboseMessage $true
# Read the content of the theme file
$themeContent = Get-Content -Path $themeFilePath
# Flag to indicate if we are inside the [Slideshow] section
$inSlideshowSection = $false
foreach ($line in $themeContent) {
# LogThis "Processing line: $line" -verboseMessage $true
# Check for the start of the [Slideshow] section
if ($line -match '^\[Slideshow\]') {
LogThis "Found Slideshow section" -verboseMessage $true
$inSlideshowSection = $true
continue
}
# If we are inside the [Slideshow] section, look for 'shuffle' setting
if ($inSlideshowSection) {
if ($line -match '(?i)shuffle=(\d)') { # Case-insensitive match for 'shuffle'
LogThis "Found shuffle setting: $line" -verboseMessage $true
return $matches[1] -eq '1'
}
# If we encounter the next section or end of file, break out of the loop
if ($line -match '^\[.*\]') {
LogThis "Leaving [Slideshow] section" -verboseMessage $true
break
}
}
}
# If no shuffle setting is found, return false
LogThis "No, the theme does not shuffle wallpapers" -verboseMessage $true
return $false
}
# Run the .theme file
function StartTheme {
param (
[string]$ThemePath
)
#restart Theme service, solves issues with theme not being applied
if ($themeServiceProblem) {
try {
LogThis "Restarting the Themes service..." -verboseMessage $true
Restart-Service -Name "Themes" -Force
LogThis "Themes service restarted successfully." -verboseMessage $true
} catch {
LogThis "Failed to restart Themes service: $_" -verboseMessage $true
}
}
# Check if the theme file exists
if (Test-Path $ThemePath) {
# Apply the theme
Start-Process $ThemePath
# Wait a bit for the theme to apply and the Settings window to appear
Start-Sleep -Seconds 4
# Close the Settings window by stopping the "ApplicationFrameHost" process
$settingsProcess = Get-Process -Name "ApplicationFrameHost" -ErrorAction SilentlyContinue
if ($settingsProcess) {
Stop-Process -Id $settingsProcess.Id
}
} else {
LogThis "Theme file not found: $ThemePath"
}
}
# Toggle the theme
function ToggleTheme {
# Get current theme
$CurrentTheme = (Get-ItemProperty -Path "Registry::HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes" -Name CurrentTheme).CurrentTheme
if ($CurrentTheme -match "dark") {
If (DoWeShuffle($LightPath)) {
RandomFirstWall -wallpaperDirectory $wallLightPath
}
LogThis "Selected $LightPath" -verboseMessage $true
StartTheme $LightPath
LogThis "$themeLight activated"
Show-BurntToastNotification -Text "Theme toggled. $LightPath activated." -AppLogo "autotheme.png"
}else {
If (DoWeShuffle($DarkPath)) {
RandomFirstWall -wallpaperDirectory $wallDarkPath
}
LogThis "Selected $DarkPath" -verboseMessage $true
StartTheme $DarkPath
LogThis "$themeDark activated"
Show-BurntToastNotification -Text "Theme toggled. $themeDark activated." -AppLogo "autotheme.png"
}
}
<# Prepend the substring '000_' to one randomly chosen
wallpaper filename, so as to make it first pick. #>
function RandomFirstWall {
param (
[string]$wallpaperDirectory
)
if (-Not ($RandomFirst)) {
LogThis "The first wallpaper will not be randomized." -verboseMessage $true
return
}
LogThis "Randomizing first wallpaper." -verboseMessage $true
# Retrieve file objects directly into $wallpapers
LogThis "Looking in $wallpaperDirectory" -verboseMessage $true
$wallpapers = Get-ChildItem -Path $wallpaperDirectory -File
# Check if there's already a file with "000_" prefix and rename it back
$existingRenamedWallpaper = $wallpapers | Where-Object { $_.Name.StartsWith("000_") }
if ($existingRenamedWallpaper) {
$originalName = $existingRenamedWallpaper.Name -replace '^000_', ''
$originalNameFull = Join-Path $wallpaperDirectory $originalName
Rename-Item -Path $existingRenamedWallpaper.FullName -NewName $originalNameFull
LogThis "Removed '000_' prefix from $($existingRenamedWallpaper.FullName)" -verboseMessage $true
}
# Filter out wallpapers that still have "000_" in the name (unlikely after removal)
$newWallpapers = $wallpapers | Where-Object { -not $_.Name.StartsWith("000_") }
# Ensure there are wallpapers available to rename
if ($newWallpapers.Count -eq 0) {
LogThis "No wallpapers available for renaming." -verboseMessage $true
return
}
# Select a random wallpaper from the filtered list
$RandomFirstWallpaper = $newWallpapers | Get-Random
# Construct the new name and rename
$newWallpaperName = "000_" + $RandomFirstWallpaper.Name
$newWallpaperNameFull = Join-Path $wallpaperDirectory $newWallpaperName
Rename-Item -Path $RandomFirstWallpaper.FullName -NewName $newWallpaperNameFull
LogThis "Renamed $($RandomFirstWallpaper.FullName) to $newWallpaperNameFull" -verboseMessage $true
}
# Check if a Scheduled task exists
function TaskExists {
param (
[string]$taskName
)
$task = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue
return $null -ne $task
}
<# Select the Theme depending on daylight or chosen hours,
then schedules the next appropriate temporary Task #>
function Main {
$Now = Get-Date
if ($UseFixedHours) {
# stay offline
$Sunrise = $LightThemeTime
$Sunset = $DarkThemeTime
$TomorrowSunrise = $LightThemeTime
} else {
# go online
$location = LocateThis
# Extract latitude, longitude and Timezone for API call
$lat = $location.Latitude
$lng = $location.Longitude
# Either API can be used, but the first one may have faulty control over DateTime formatting
#$url = "https://api.sunrise-sunset.org/json?lat=$lat&lng=$lng"
$url = "https://api.sunrisesunset.io/json?lat=$lat&lng=$lng"
$Daylight = (Invoke-RestMethod $url).results
LogThis "Fetched daylight data string = $Daylight" -verboseMessage $true
# Parse and adjust the dates
#$Sunrise = [DateTime]::ParseExact($Daylight.sunrise, "h:mm:ss tt", $null)
#$Sunset = [DateTime]::ParseExact($Daylight.sunset, "h:mm:ss tt", $null)
$SunriseTimeString = $Daylight.sunrise
$SunriseDateString = $Daylight.date
$SunriseString = "$SunriseTimeString $SunriseDateString"
$Sunrise = [DateTime]::ParseExact($SunriseString, "h:mm:ss tt yyyy-MM-dd", [System.Globalization.CultureInfo]::InvariantCulture)
$SunsetTimeString = $Daylight.sunset
$SunsetDateString = $Daylight.date
$SunsetString = "$SunsetTimeString $SunsetDateString"
$Sunset = [DateTime]::ParseExact($SunsetString, "h:mm:ss tt yyyy-MM-dd", [System.Globalization.CultureInfo]::InvariantCulture)
# second query for tomorrow
$TomorrowDaylight = (Invoke-RestMethod "$url&date=tomorrow").results
$TomorrowSunriseTimeString = $TomorrowDaylight.sunrise
$TomorrowSunriseDateString = $TomorrowDaylight.date
$TomorrowSunriseString = "$TomorrowSunriseTimeString $TomorrowSunriseDateString"
$TomorrowSunrise = [DateTime]::ParseExact($TomorrowSunriseString, "h:mm:ss tt yyyy-MM-dd", [System.Globalization.CultureInfo]::InvariantCulture)
LogThis "Sunrise: $Sunrise, Sunset: $Sunset, TomorrowSunrise: $TomorrowSunrise, Now: $Now" -verboseMessage $true
}
# Get current theme
$CurrentTheme = (Get-ItemProperty -Path "Registry::HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes" -Name CurrentTheme).CurrentTheme
# Determine the mode
# Afer Sunrise and Before Sunset
if ($Now -ge $Sunrise -and $Now -lt $Sunset) {
if ($CurrentTheme -match $themeLight) {
LogThis "The Mode is already set. No action needed."
exit
}
$NextTriggerTime = $Sunset
If (DoWeShuffle($LightPath)) {
RandomFirstWall -wallpaperDirectory $wallLightPath
}
# set Light theme
LogThis "Setting the theme $LightPath" -verboseMessage $true
StartTheme -ThemePath $LightPath
LogThis "$themeLight activated. Next trigger at: $NextTriggerTime"
Show-BurntToastNotification -Text "$themeLight activated. Next trigger at: $NextTriggerTime" -AppLogo "autotheme.png"
# assign name for next temporary task
$Name = "Sunset theme"
} else {
if ($CurrentTheme -match $themeDark) {
LogThis "The Mode is already set. No action needed."
exit
}
if ($Now -ge $Sunset) {
$NextTriggerTime = $TomorrowSunrise
} else {
$NextTriggerTime = $Sunrise
}
If (DoWeShuffle($DarkPath)) {
RandomFirstWall -wallpaperDirectory $wallDarkPath
}
# set Dark theme
LogThis "Setting the theme $DarkPath" -verboseMessage $true
StartTheme -ThemePath $DarkPath
LogThis "$themeDark activated. Next trigger at: $NextTriggerTime"
Show-BurntToastNotification -Text "$themeDark activated. Next trigger at: $NextTriggerTime" -AppLogo "autotheme.png"
# assign name for next temporary task
$Name = "Sunrise theme"
}
# Schedule next run
LogThis "Setting temporary Scheduled Task"
$arguments = "-WindowStyle Hidden -ExecutionPolicy Bypass -NoProfile -File `"$PSCommandPath`""
$fullCommand = "PowerShell.exe $arguments"
LogThis "Full Command: $fullCommand" -verboseMessage $true
LogThis "Creating scheduled task action..." -verboseMessage $true
$action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument $arguments
$trigger = New-ScheduledTaskTrigger -Once -At $NextTriggerTime
$userSid = $env:USERNAME
$principal = New-ScheduledTaskPrincipal -UserId $userSid -LogonType Interactive -RunLevel Highest
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable
$task = New-ScheduledTask -Action $action -Trigger $trigger -Principal $principal -Settings $settings
LogThis "Scheduled task action created." -verboseMessage $true
# Unregister the old task if it exists
if (TaskExists $Name) {
Unregister-ScheduledTask -TaskName $Name -Confirm:$false
LogThis "Unregistered existing task: $Name" -verboseMessage $true
}
# Register the new task
try {
Register-ScheduledTask -TaskName $Name -InputObject $task | Out-Null
LogThis "Registered new task: $Name" -verboseMessage $true
} catch {
LogThis "Error registering task: $_"
}
}
# ============= RUNTIME ==============
try {
# include config variables
if (-Not (Test-Path $ConfigPath)) {
Write-Error "Configuration file not found: $ConfigPath"
Exit 1
}
. $ConfigPath
# Start logging
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
LogThis "$timestamp === Script started (Version: $scriptVersion)"
# Check if the script was run recently
if($checkLastRun){LastTime}
# Running from terminal or from Scheduled Task?
if (IsRunningFromTerminal) {
LogThis "Script is running from Terminal." -verboseMessage $true
# Toggle the theme and exit
LogThis "Toggling the Theme"
ToggleTheme
exit
} else {
LogThis "Script is running from Task Scheduler." -verboseMessage $true
LogThis "Selecting Theme based on daylight"
# Main function
Main
}
# Update last run time
UpdateTime
LogThis "All done."
} catch {
LogThis "Error: $_"
}