mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-01-11 16:39:10 +01:00
Update switch-wallpaper.ps1
This commit is contained in:
parent
c2b4be5e37
commit
49c61532f1
@ -2,9 +2,7 @@
|
|||||||
.SYNOPSIS
|
.SYNOPSIS
|
||||||
Switches to a random wallpaper
|
Switches to a random wallpaper
|
||||||
.DESCRIPTION
|
.DESCRIPTION
|
||||||
This script download a random photo and sets it as desktop wallpaper.
|
This script downloads a random photo and sets it as desktop wallpaper.
|
||||||
.PARAMETER Style
|
|
||||||
Specifies either Fill, Fit, Stretch, Tile, Center, or Span (default)
|
|
||||||
.EXAMPLE
|
.EXAMPLE
|
||||||
PS> ./switch-wallpaper
|
PS> ./switch-wallpaper
|
||||||
.NOTES
|
.NOTES
|
||||||
@ -13,8 +11,6 @@
|
|||||||
https://github.com/fleschutz/PowerShell
|
https://github.com/fleschutz/PowerShell
|
||||||
#>
|
#>
|
||||||
|
|
||||||
param([string]$Style = "Span")
|
|
||||||
|
|
||||||
function GetTempDir {
|
function GetTempDir {
|
||||||
if ("$env:TEMP" -ne "") { return "$env:TEMP" }
|
if ("$env:TEMP" -ne "") { return "$env:TEMP" }
|
||||||
if ("$env:TMP" -ne "") { return "$env:TMP" }
|
if ("$env:TMP" -ne "") { return "$env:TMP" }
|
||||||
@ -22,57 +18,14 @@ function GetTempDir {
|
|||||||
return "C:\Temp"
|
return "C:\Temp"
|
||||||
}
|
}
|
||||||
|
|
||||||
function SetWallPaper { param([string]$Image, [ValidateSet('Fill', 'Fit', 'Stretch', 'Tile', 'Center', 'Span')][string]$Style)
|
|
||||||
|
|
||||||
$WallpaperStyle = switch($Style) {
|
|
||||||
"Fill" {"10"}
|
|
||||||
"Fit" {"6"}
|
|
||||||
"Stretch" {"2"}
|
|
||||||
"Tile" {"0"}
|
|
||||||
"Center" {"0"}
|
|
||||||
"Span" {"22"}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($Style -eq "Tile") {
|
|
||||||
New-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name WallpaperStyle -PropertyType String -Value $WallpaperStyle -Force
|
|
||||||
New-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name TileWallpaper -PropertyType String -Value 1 -Force
|
|
||||||
} else {
|
|
||||||
New-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name WallpaperStyle -PropertyType String -Value $WallpaperStyle -Force
|
|
||||||
New-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name TileWallpaper -PropertyType String -Value 0 -Force
|
|
||||||
}
|
|
||||||
Add-Type -TypeDefinition @"
|
|
||||||
using System;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
|
|
||||||
public class Params
|
|
||||||
{
|
|
||||||
[DllImport("User32.dll",CharSet=CharSet.Unicode)]
|
|
||||||
public static extern int SystemParametersInfo (Int32 uAction,
|
|
||||||
Int32 uParam,
|
|
||||||
String lpvParam,
|
|
||||||
Int32 fuWinIni);
|
|
||||||
}
|
|
||||||
"@
|
|
||||||
|
|
||||||
$SPI_SETDESKWALLPAPER = 0x0014
|
|
||||||
$UpdateIniFile = 0x01
|
|
||||||
$SendChangeEvent = 0x02
|
|
||||||
$fWinIni = $UpdateIniFile -bor $SendChangeEvent
|
|
||||||
$ret = [Params]::SystemParametersInfo($SPI_SETDESKWALLPAPER, 0, $Image, $fWinIni)
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$Reply = "Give me a second", "Gimme a second", "Give me a moment", "Just a moment", "Just a second", "Wait a second", "Hold on" | Get-Random
|
& "$PSScriptRoot/give-reply.ps1" "Loading one from Unsplash..."
|
||||||
& "$PSScriptRoot/give-reply.ps1" "$Reply"
|
|
||||||
|
|
||||||
"Downloading random photo from Unsplash..."
|
|
||||||
$Path = "$(GetTempDir)/next_wallpaper.jpg"
|
$Path = "$(GetTempDir)/next_wallpaper.jpg"
|
||||||
& wget -O $Path "https://unsplash.it/3840/2160/?random"
|
& wget -O $Path "https://picsum.photos/3840/2160"
|
||||||
|
if ($lastExitCode -ne "0") { throw "Download failed" }
|
||||||
|
|
||||||
"Switching the wallpaper..."
|
& "$PSScriptRoot/set-wallpaper.ps1" -ImageFile "$Path"
|
||||||
SetWallPaper -Image $Path -Style $Style
|
|
||||||
|
|
||||||
"✔️ Done."
|
|
||||||
exit 0 # success
|
exit 0 # success
|
||||||
} catch {
|
} catch {
|
||||||
"⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))"
|
"⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))"
|
||||||
|
Loading…
Reference in New Issue
Block a user