-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSave-UnsplashWallpaper.ps1
More file actions
75 lines (57 loc) · 2.98 KB
/
Copy pathSave-UnsplashWallpaper.ps1
File metadata and controls
75 lines (57 loc) · 2.98 KB
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
function Save-UnsplashWallpaper {
# https://source.unsplash.com/3840x2160/?nature
# Invoke-WebRequest -Uri 'https://source.unsplash.com/3840x2160/?boat,sail' -UseBasicParsing
# Save location for the image
$UnsplashImageSaveLocation = "D:\Unsplash_Walls\"
# Array of possible categories
$PreferredCategories = @("nature","water","trees","mountain","sail","yacht","snow","lava","ocean","scenic","moon","peak","summit","cloud","sky")
# Choose two random categories
$SelectedCategories = (Get-Random -Count 2 -InputObject $PreferredCategories) -join ','
try {
# Check if the save location exists and is a directory
If (-Not (Test-Path -Path $UnsplashImageSaveLocation -PathType Container)) {
# Save location does not exist or is not a directory
throw "The specified save location does not exist or is not a directory: $UnsplashImageSaveLocation"
}
# Get the current timestamp
$CurrentTimestamp = Get-Date -Format s | ForEach-Object {$_ -replace ":", "."}
# Get the display resolution of the current monitor
function Get-CurrentMonitorResolution {
try {
# Get the Win32_DesktopMonitor class
$Monitor = Get-WmiObject -Class Win32_DesktopMonitor -ErrorAction Stop -ErrorVariable Error
# Get the screen width and height
$ScreenWidth = $Monitor.ScreenWidth
$ScreenHeight = $Monitor.ScreenHeight
# Validate the input
If ($ScreenWidth -le 0 -or $ScreenHeight -le 0) {
throw "Invalid screen resolution: $ScreenWidth x $ScreenHeight"
}
$ScreenResolutionXValue = "$ScreenWidth x $ScreenHeight" -replace '\s+', ''
# Return the screen resolution as a string
[String] $ScreenResolutionXValue
} catch {
# An error occurred
Write-Error "An error occurred while getting the display resolution: $($Error.Exception.Message)"
}
}
$DesktopResolution = Get-CurrentMonitorResolution
# Construct the URL for the image
$URL = "https://source.unsplash.com/$DesktopResolution/?$SelectedCategories"
# Construct the output file path
$Output = "$UnsplashImageSaveLocation$CurrentTimestamp-$SelectedCategories.jpeg" -replace ",","."
try {
# Download the image from Unsplash
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$WebClient = New-Object System.Net.WebClient
$WebClient.DownloadFile($URL, $Output)
} catch {
# An error occurred while trying to download the image
Write-Error "An error occurred while trying to download the image: $($_.Exception.Message)"
}
} catch {
# An error occurred
Write-Error "An error occurred: $($_.Exception.Message)"
}
}
Save-UnsplashWallpaper