|
| 1 | +$windir = [Environment]::GetFolderPath('Windows') |
| 2 | + |
| 3 | +function Stop-ThemeProcesses { |
| 4 | + Get-Process 'SystemSettings', 'control' -EA 0 | Stop-Process -Force -EA 0 |
| 5 | +} |
| 6 | + |
| 7 | +function Set-Theme { |
| 8 | + param ( |
| 9 | + [Parameter(Mandatory = $true)] |
| 10 | + [ValidateNotNullOrEmpty()] |
| 11 | + [string]$Path |
| 12 | + ) |
| 13 | + |
| 14 | + if (!((Get-Item $Path -EA 0).Extension -eq '.theme')) { |
| 15 | + throw "'$Path' is not a valid path to a theme file." |
| 16 | + } |
| 17 | + Stop-ThemeProcesses |
| 18 | + Start-Process -filepath $path ; timeout /t 3; taskkill /im "systemsettings.exe" /f |
| 19 | + Start-Sleep 10 |
| 20 | + Stop-ThemeProcesses |
| 21 | +} |
| 22 | + |
| 23 | +function Set-ThemeMRU { |
| 24 | + if ([System.Environment]::OSVersion.Version.Build -ge 22000) { |
| 25 | + Stop-ThemeProcesses |
| 26 | + Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes" -Name "ThemeMRU" -Value "$((@( |
| 27 | + "atlas-v0.4.x-dark.theme", |
| 28 | + "atlas-v0.4.x-light.theme", |
| 29 | + "atlas-v0.3.x-dark.theme", |
| 30 | + "atlas-v0.3.x-light.theme", |
| 31 | + "dark.theme", |
| 32 | + "aero.theme" |
| 33 | + ) | ForEach-Object { "$windir\resources\Themes\$_" }) -join ';');" -Type String -Force |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +# Credit: https://superuser.com/a/1343640 |
| 38 | +function Set-LockscreenImage { |
| 39 | + param ( |
| 40 | + [ValidateNotNullOrEmpty()] |
| 41 | + [string]$Path = "$([Environment]::GetFolderPath('Windows'))\AtlasModules\Wallpapers\lockscreen.png" |
| 42 | + ) |
| 43 | + |
| 44 | + if (!(Test-Path $Path)) { |
| 45 | + throw "Path ('$Path') for lockscreen not found." |
| 46 | + } |
| 47 | + $newImagePath = [System.IO.Path]::GetTempPath() + (New-Guid).Guid + [System.IO.Path]::GetExtension($Path) |
| 48 | + Copy-Item $Path $newImagePath |
| 49 | + |
| 50 | + # setup WinRT namespaces |
| 51 | + Add-Type -AssemblyName System.Runtime.WindowsRuntime |
| 52 | + [Windows.System.UserProfile.LockScreen, Windows.System.UserProfile, ContentType = WindowsRuntime] | Out-Null |
| 53 | + |
| 54 | + # setup async |
| 55 | + $asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { |
| 56 | + $_.Name -eq 'AsTask' -and |
| 57 | + $_.GetParameters().Count -eq 1 -and |
| 58 | + $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' |
| 59 | + })[0] |
| 60 | + Function Await($WinRtTask, $ResultType) { |
| 61 | + $asTask = $asTaskGeneric.MakeGenericMethod($ResultType) |
| 62 | + $netTask = $asTask.Invoke($null, @($WinRtTask)) |
| 63 | + $netTask.Wait(-1) | Out-Null |
| 64 | + $netTask.Result |
| 65 | + } |
| 66 | + Function AwaitAction($WinRtAction) { |
| 67 | + $asTask = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and !$_.IsGenericMethod })[0] |
| 68 | + $netTask = $asTask.Invoke($null, @($WinRtAction)) |
| 69 | + $netTask.Wait(-1) | Out-Null |
| 70 | + } |
| 71 | + |
| 72 | + # make image object |
| 73 | + [Windows.Storage.StorageFile, Windows.Storage, ContentType = WindowsRuntime] | Out-Null |
| 74 | + $image = Await ([Windows.Storage.StorageFile]::GetFileFromPathAsync($newImagePath)) ([Windows.Storage.StorageFile]) |
| 75 | + |
| 76 | + # execute |
| 77 | + AwaitAction ([Windows.System.UserProfile.LockScreen]::SetImageFileAsync($image)) |
| 78 | + |
| 79 | + # cleanup |
| 80 | + Remove-Item $newImagePath |
| 81 | +} |
| 82 | + |
| 83 | +Export-ModuleMember -Function Set-Theme, Set-ThemeMRU, Set-LockscreenImage |
| 84 | +No newline at end of file |
0 commit comments