-
-
Save bdwakefield/a8f8c3332dceb262ad6a to your computer and use it in GitHub Desktop.
Windows Powershell Script to download the latest image from the DSCOVR satellite then set as the desktop background.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# DSCOVR:EPIC Downloader | |
# | |
# | |
# | |
# This script will scrape the latest image from the DSCOVR satellite, saving it | |
# in My Pictures\DSCOVR\ and then set as the desktop background. | |
# | |
# http://epic.gsfc.nasa.gov/ | |
# | |
# | |
function getImageList($date){ | |
$date = (Get-Date $date).ToUniversalTime() | |
$year = $date.ToString("yyyy") | |
$month = $date.ToString("MM") | |
$day = $date.ToString("dd") | |
$url = "http://epic.gsfc.nasa.gov/api/images.php?date=$year-$month-$day" | |
Invoke-WebRequest -Uri $url | ConvertFrom-Json | |
} | |
function getLatestImage(){ | |
$now = Get-Date | |
$now = $now.AddDays(-2) | |
$j = getImageList($now) | |
$len = $j.length | |
while($len -lt 1){ | |
$now = $now.AddDays(-1) | |
$j = getImageList($now) | |
} | |
$j[$len - 1].image | |
} | |
#Create the folder My Pictures\DSCOVR\ if it doesnt exist | |
$outpath = [Environment]::GetFolderPath("MyPictures") + "\DSCOVR\" | |
if(!(Test-Path -Path $outpath )) | |
{ | |
[void](New-Item -ItemType directory -Path $outpath) | |
} | |
#The filename that will be saved: | |
#Uncomment this to have the files accumulate in the directory: | |
#$outfile = "$year$month$day"+"_" + $time + ".jpg" | |
#Use this to have the script just store the latest file only: | |
$outfile = "latest.jpg" | |
$img = getLatestImage | |
$url = "http://epic.gsfc.nasa.gov/epic-archive/jpg/$img.jpg" | |
try { | |
Invoke-WebRequest $url -OutFile ($outpath + $outfile) | |
} | |
catch { | |
Write-Error "Couldn't Download Image" | |
Exit | |
} | |
<# | |
Different settings for the wallpaper: | |
Tile : | |
key.SetValue(@"WallpaperStyle", "0") ; | |
key.SetValue(@"TileWallpaper", "1") ; | |
break; | |
Center : | |
key.SetValue(@"WallpaperStyle", "0") ; | |
key.SetValue(@"TileWallpaper", "0") ; | |
break; | |
Stretch : | |
key.SetValue(@"WallpaperStyle", "2") ; | |
key.SetValue(@"TileWallpaper", "0") ; | |
break; | |
Fill : | |
key.SetValue(@"WallpaperStyle", "10") ; | |
key.SetValue(@"TileWallpaper", "0") ; | |
break; | |
Fit : | |
key.SetValue(@"WallpaperStyle", "6") ; | |
key.SetValue(@"TileWallpaper", "0") ; | |
break; | |
#> | |
Write-Output "Setting Wallpaper..." | |
Set-ItemProperty -path "HKCU:Control Panel\Desktop" -name Wallpaper -value ($outpath + $outfile) | |
Set-ItemProperty -path "HKCU:Control Panel\Desktop" -name WallpaperStyle -value 6 | |
Set-ItemProperty -path "HKCU:Control Panel\Desktop" -name TileWallpaper -value 0 | |
Set-ItemProperty 'HKCU:\Control Panel\Colors' -name Background -Value "0 0 0" | |
#rundll32.exe user32.dll, UpdatePerUserSystemParameters | |
$setwallpapersource = @" | |
using System.Runtime.InteropServices; | |
public class wallpaper | |
{ | |
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 ) | |
{ | |
SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange ); | |
} | |
} | |
"@ | |
Add-Type -TypeDefinition $setwallpapersource | |
[wallpaper]::SetWallpaper(($outpath + $outfile)) | |
Write-Output "Done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment