Quantcast
Channel: MicrosoftPro.nl » Windows Server 2008
Viewing all articles
Browse latest Browse all 10

Using the daily Bing Wallpaper as Desktop background

$
0
0

Update 2015-01-19 : Arjan Mensch pointed out I used the wrong source to download the daily image, in the current function below Bing.com is used and the HD image is downloaded.

As you may or may not know Microsoft offers a daily wallpaper through www.bing.com, as I kinda like most of these wallpapers and dislike staring at a simple and the same wallpaper every day I started searching for an app or something that would use the current Bing.com wallpaper on my Desktop.

As you might guess by now I did not succeed in this quest to find a clean and simple method to do so, with this in mind I created a little Powershell script that downloads the current wallpaper and saves it to a specified folder. With another little function I change my current wallpaper to this new download wallpaper.

I used a previous post of mine to schedule this script and voila My Desktop changes every day.

The script:

function get-BingWallpaper
{
param(
[Parameter(Mandatory=$True,Position=1)]
[string]$destinationPath,
[Parameter(Mandatory=$True,Position=2)]
[string]$destinationName

)
<#
.Synopsis
Downloads the current Bing Wallpaper from Bing.com.
.Description
Downloads Bing Wallpaper.
.Parameter destinationPath
The path where the Wallpaper will be stored.
.Parameter $destinationName
The file name to which the wallpaper will be stored.
.Example
Get-BingWallpaper -destinationPath “c:\Wallpapers” -destinationName “wallpaper.jpg”
.Link

.Notes
Name: Get-BingWallpaper
Author: Michael Verbeek
Lastedit: 19/01/2015
#>

$web = New-Object Net.WebClient

$baseUrl=”http://www.bing.com”
$jsonUrl=”http://www.bing.com/HPImageArchive.aspx?format=js&mbl=1&idx=0&n=1&cc=us”

$json=Invoke-WebRequest -Uri $jsonUrl | ConvertFrom-Json

$webclient = New-Object System.Net.WebClient

$errors = @()

$url = $baseUrl+$json.images.url.toString().trim();

# Requires “http://” for URL
if ($url.indexOf(“http://”) -eq -1) {
$url = “http://” + $url
}
if ($url -ne “”) {
$localImagePath = Join-Path $destinationPath $destinationName

try {
$webClient.DownloadFile($url, $localImagePath)
}
catch [Exception] {
$errors += $url
Write-Host “ERROR: ” + $_.Exception.ToString() -foregroundcolor “red”
}
}
Write-Host “done. errors: ”
$errors

return $localImagePath
}

 

function Set-Wallpaper
{
param(
[Parameter(Mandatory=$true)]
$Path,

[ValidateSet(‘Center’, ‘Stretch’)]
$Style = ‘Stretch’
)

<#
.Synopsis
Sets a specific wallpaper as Desktop Background.
.Description
Sets the desktop background.
.Parameter Path
The path where the Wallpaper is stored.
.Example
Set-Wallpaper -Path “c:\Wallpaper\Wallpaper.jpg”
.Link

.Notes
Name: Set-Wallpaper
Author: Michael Verbeek
Lastedit: 01/19/2015
#>

Add-Type @”
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace Wallpaper
{
public enum Style : int
{
Center, Stretch
}
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 ) {
SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
RegistryKey key = Registry.CurrentUser.OpenSubKey(“Control Panel\\Desktop”, true);
switch( style )
{
case Style.Stretch :
key.SetValue(@”WallpaperStyle”, “2”) ;
key.SetValue(@”TileWallpaper”, “0”) ;
break;
case Style.Center :
key.SetValue(@”WallpaperStyle”, “1”) ;
key.SetValue(@”TileWallpaper”, “0”) ;
break;
}
key.Close();
}
}
}
“@

[Wallpaper.Setter]::SetWallpaper( $Path, $Style )
}

Save this script as BingWallpaper.psm1.

The script exists of two functions, the first (Get-BingWallpaper) downloads the current wallpaper and saves it to the specified path. The second (Set-Wallpaper) set your current wallpaper to the specified wallpaper (this can be the wallpaper you downloaded with Get-BingWallpaper, but ik can be used to set your wallpaper to any JPG you specify.

Example:

Download the current Bing Wallpaper:
Set-BingWallpaper -destinationPath “c:\Wallpapers” -destinationName “Wallpaper.jpg” (mind that the destination folder needs to exist)

Set your background:
Set-Wallpaper -Path “c:\Wallpapers\Wallpaper.jpg”

The result will be:

Current www.bing.com:
bing

My Desktop after running the script:
Day1

The function can be use for multiple purpose for example changing the background of your RDWebAccess page like my colleague Arjan Mensch describes in his blog

http://msfreaks.wordpress.com/2015/01/14/rd-web-access-automate-bing-wallpaper-integration/


Viewing all articles
Browse latest Browse all 10

Trending Articles