forked from ohnojoe/BingWallpaperer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BingWallpaperer.ps1
290 lines (231 loc) · 9.89 KB
/
BingWallpaperer.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
# Updates Windows desktop wallpaper from Bing's Image of the Day web service
# ###########################################################################
# Created by: Joe Blumenow (2014)
# Last Updated: 2019-03-10
# #########################################
# Parameters
# #########################################
# $size - ideal image dimensions in WxH format, not all dimensions supported
# $idx - image index, defaults to 0 (latest image), max value is 7 (at time of writing)
# $mkt - the market/region of images to uses
# $savePath - local save path of the image. Defaults to My Pictures folder if left blank.
param (
[string]$size = "",
[string]$idx = "0",
[string]$mkt = "pt-BR",
[string]$savePath = "",
[switch]$RegisterSchedule = $false,
[switch]$UnregisterSchedule = $false,
[ValidateSet("NoChange", "Center", "Tile", "Stretch", "Fit", "Fill")]
[string]$wallpaperStyle = "NoChange"
)
# .NET class to correctly set wallpaper
# Adapted from: https://social.technet.microsoft.com/Forums/en-US/9af1769e-197f-4ef3-933f-83cb8f065afb/background-change
# #########################################
Add-Type @"
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace Wallpaper
{
public enum Style : int { NoChange, Center, Tile, Stretch, Fit, Fill }
public class Setter {
public const int SetDesktopWallpaper = 20;
public const int UpdateIniFile = 0x01;
public const int SendWinIniChange = 0x02;
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
public static void SetWallpaper ( string path, Wallpaper.Style style ) {
RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
switch (style) {
case Style.Center:
key.SetValue(@"WallpaperStyle", "1");
key.SetValue(@"TileWallpaper", "0");
break;
case Style.Tile:
key.SetValue(@"WallpaperStyle", "1");
key.SetValue(@"TileWallpaper", "1");
break;
case Style.Stretch:
key.SetValue(@"WallpaperStyle", "2");
key.SetValue(@"TileWallpaper", "0");
break;
case Style.Fit:
key.SetValue(@"WallpaperStyle", "6");
key.SetValue(@"TileWallpaper", "0");
break;
case Style.Fill:
key.SetValue(@"WallpaperStyle", "10");
key.SetValue(@"TileWallpaper", "0");
break;
case Style.NoChange:
break;
}
key.Close();
// Set wallpaper after style change to ensure the change is picked up
SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
}
}
}
"@
# Get an array of possible image dimensions
# with the ideal size first in the array
# #########################################
Function Get-IdealImageDimensionsArray($idealSize) {
$validImageDimensionArray = New-Object System.Collections.ArrayList
# This should be a list of all known possible
# image dimensions.
# There are likely many more than this, but
# this should cover the main ones.
$validImageDimensionArray.Add("UHD") | Out-Null
$validImageDimensionArray.Add("1920x1200") | Out-Null
$validImageDimensionArray.Add("1920x1080") | Out-Null # 1920x1080 tends to exclude bing branding
$validImageDimensionArray.Add("1366x768") | Out-Null
$validImageDimensionArray.Add("1024x768") | Out-Null
# Remove the ideal size, in case it's in the list
$validImageDimensionArray.Remove($idealSize)
# Add the ideal size as the first
$validImageDimensionArray.Insert(0, $idealSize)
$validImageDimensionArray
}
# Downloads the specified image from
# #########################################
Function Get-Image($imageSize, $idx, $mkt) {
# Base Bing url, comes in handy later
$urlBing = "http://www.bing.com"
# Bing Image feed containing xml of specified image (always defaults to latest if idx or mkt params not valid)
$urlBingImageFeed = "{0}/HPImageArchive.aspx?format=xml&idx={1}&n=1&mkt={2}" -f $urlBing, $idx, $mkt
$urlImageBasePath = ""
$urlImage = ""
$savelocation = ""
Write-Debug ("Feed URL: " + $urlBingImageFeed)
# Initialise WebClient, proxy
$webClient = New-Object System.Net.WebClient
# Get image base path
$page = $webClient.DownloadString($urlBingImageFeed)
$regex = [regex] '<urlBase>(.*)</urlBase>'
$match = $regex.Match($page)
$urlImageBasePath = $match.Groups[1].Value
Write-Debug ("Image base path: $urlImageBasePath") -Verbose
# Build image url
$urlImage = "{0}{1}_{2}.jpg" -f $urlBing, $urlImageBasePath, $imageSize
Write-Debug "Image URL: $urlImage"
if ($savePath -eq "") {
$myPicturesFolder = [Environment]::GetFolderPath("MyPictures")
} else {
$myPicturesFolder = $savePath
}
$savelocation = [io.path]::combine($myPicturesFolder, 'bing_image_today.jpg')
Try {
Write-Debug ("Downloading image: " + $urlImage) -Verbose
$webClient.DownloadFile($urlImage, $savelocation)
## Assume something is wrong if file doesn't exist or length is less than a 1KB
if (!(Test-Path $savelocation) -or ((Get-Item $savelocation).length -lt 1kb)) {
Write-Warning "There was a problem downloading the image $urlImage"
$savelocation = ""
}
}
Catch [System.Net.WebException] {
## Assume it fails due to 404
Write-Warning "Unable to download image $urlImage"
$savelocation = ""
}
return $savelocation
}
# Gets the current screen DPI factor
# #########################################
Function Get-DpiFactor {
$DPISetting = (Get-ItemProperty 'HKCU:\Control Panel\Desktop\WindowMetrics' -Name AppliedDPI).AppliedDPI
switch ($DPISetting)
{
96 {return 1}
120 {return 1.25}
144 {return 1.5}
192 {return 2}
}
return 1
}
# Gets the current screen resolution for
# the primary monitor
# #########################################
Function Get-ScreenResolution {
[void] [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$s = [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize
$dpi = Get-DpiFactor
[decimal]$w = [math]::Floor($s.Width * $dpi)
[decimal]$h = [math]::Floor($s.Height * $dpi)
if ($h -gt 1200) { return "UHD" }
return "${w}x${h}"
}
Function Get-ScreenResolution-Height {
[void] [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$s = [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize
$dpi = Get-DpiFactor
[decimal]$h = [math]::Floor($s.Height * $dpi)
return ${h}
}
function Register-Schedule($taskPath, $taskName) {
# Make sure task doesn't already exist
Unregister-Schedule $taskPath $taskName
$argument = $script:MyInvocation.MyCommand.Path
if ($size -ne "") { $argument += " -size $size" }
if ($idx -ne 0) { $argument += " -idx $idx" }
if ($mkt -ne "en-GB") { $argument += " -mkt $mkt" }
if ($wallpaperStyle -ne "NoChange") { $argument += " -wallpaperStyle $wallpaperStyle" }
$trigger = New-ScheduledTaskTrigger -At 00:10 -Daily
$action = New-ScheduledTaskAction -Execute "Powershell.exe" -Argument $argument
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -StartWhenAvailable
$task = Register-ScheduledTask -TaskPath $taskPath -TaskName $taskName -Trigger $trigger -Action $action -Settings $settings
Write-Output " Scheduled task registered."
}
function Unregister-Schedule($taskPath, $taskName) {
$existingTask = Get-ScheduledTask | Where-Object { $_.TaskName -eq $taskName -and $_.TaskPath -eq $taskPath}
if ($existingTask) {
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false
Write-Output " Scheduled task unregistered"
} else {
Write-Output " Scheduled task doesn't currently exist"
}
}
# Main Program Entry Point
# #########################################
$taskPath = "\"
$taskName = "Bing Wallpaperer Daily Update"
Write-Output ""
Write-Output ">>> Bing Image of the Day Wallpaper Updater <<<"
if ($UnregisterSchedule) {
Write-Output "... Unregistering daily schedule task"
Unregister-Schedule $taskPath $taskName
}
if ($RegisterSchedule) {
Write-Output "... Registering daily schedule task"
Register-Schedule $taskPath $taskName
}
if ($size -eq "") {
$size = Get-ScreenResolution
}
# Get a list of the main image sizes, with the
# specified or ideal size as the first item
$imageSizes = Get-IdealImageDimensionsArray $size
# Loop through all our sizes here because not
# all sizes are available for all images
# particularly HD images, so we're doing it this
# way in case the specified image isn't available
# it will at least fallback to an alternative.
foreach ($imageSize in $imageSizes) {
# Let's try and download the image
Write-Output "... Trying to get image - size: $imageSize, idx: $idx, mkt: $mkt"
$saveLocation = Get-Image $imageSize $idx $mkt
if ($saveLocation -ne "") {
# All's good, an image was saved...
Write-Output "... Image successfully saved to: $saveLocation"
# Set the image as the desktop wallpaper
[Wallpaper.Setter]::SetWallpaper($saveLocation, $wallpaperStyle)
Write-Output "... Wallpaper set with style of $wallpaperStyle"
# We can break out of our $imageSizes loop now
break
}
# No image saved, there must have been a problem
Write-Warning "Unable to download image at this size: $imageSize"
}
Write-Output ">>> All done, goodbye! <<<"