JorgTheElder

Set-RedditWallpapersAsDesktop.ps1

Feb 18th, 2018
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Set-RedditWallpapersAsDesktop.ps1 - JorgTheElder@Outlook.com
  2. # Great help from: https://www.kittell.net/code/powershell-remove-windows-wallpaper-per-user/
  3.  
  4. #$DebugPreference = 'Continue'; #uncomment if you want to see all the steps
  5.  
  6. #region types
  7.  
  8. Add-Type @"
  9. using System;
  10. using System.Runtime.InteropServices;
  11. using Microsoft.Win32;
  12. namespace Wallpaper
  13. {
  14.   public class Setter {
  15.  
  16.      public const int SetDesktopWallpaper = 20;
  17.      public const int UpdateIniFile = 0x01;
  18.      public const int SendWinIniChange = 0x02;
  19.  
  20.      [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  21.      private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
  22.  
  23.      public static void SetWallpaper ( string path ) {
  24.         SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
  25.      }
  26.   }
  27. }
  28. "@
  29.  
  30. #endregion types
  31.  
  32.  
  33. #region functions
  34.  
  35. Function Invoke-Update {
  36.  
  37.   $cmd = $env:windir + '\System32\RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters';
  38.   Invoke-Expression -Command $cmd;
  39.  
  40. }
  41.  
  42. Function Set-WallPaper
  43. {
  44.   param (
  45.  
  46.     [Parameter(Mandatory)]
  47.     [ValidateScript({ Test-Path $_ })]
  48.     [string]$WallPaper,
  49.  
  50.     [ValidateSet('Fill', 'Fit', 'Stretch', 'Center', 'Tile', 'Span')]
  51.     [string]$WallpaperStyle = 'Fill',
  52.  
  53.     [switch]$AutoColorization
  54.  
  55.   )
  56.  
  57.   #Affected Reg Keys
  58.   #AutoColorization REG_DWORD 1 or 0
  59.   #WallpaperStyle REG_SZ 10, 6, 2, 0, 22
  60.   #Wallpaper REG_SZ path-to-image
  61.  
  62.   #remove cached files to help change happen
  63.   #Remove-Item -Path "$($env:APPDATA)\Microsoft\Windows\Themes\CachedFiles" -Recurse -Force -ErrorAction SilentlyContinue    
  64.  
  65.   $fit = @{ 'Fill' = 10; 'Fit' = 6; 'Stretch' = 2; 'Center' = 0; 'Tile' = '99'; 'Span' = '22' }
  66.  
  67.   if($AutoColorization) {
  68.  
  69.     Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name AutoColorization -value 1;
  70.  
  71.   } else {
  72.  
  73.     Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name AutoColorization -value 0;
  74.  
  75.   }
  76.  
  77.   if($WallpaperStyle -eq 'Tile') {
  78.  
  79.     Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name WallpaperStyle -value 0;
  80.     Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name TileWallpaper -value 1;
  81.  
  82.   } else {
  83.  
  84.     Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name WallpaperStyle -value $fit[$WallpaperStyle];
  85.     Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name TileWallpaper -value 0;
  86.  
  87.   }
  88.  
  89.   [Wallpaper.Setter]::SetWallpaper($WallPaper);
  90.  
  91. }
  92. #endregion functions
  93.  
  94. $folder = Join-Path ([Environment]::GetFolderPath("MyDocuments")) 'RedditWallPapers';
  95.  
  96. if(-not(Test-Path $folder)) { $nul = New-Item $folder -ItemType Directory -ErrorAction Stop; }
  97.  
  98. $folder = Join-Path $folder (Get-Date).ToString('yyyy-MM');
  99.  
  100. if(-not(Test-Path $folder)) { $nul = New-Item $folder -ItemType Directory -ErrorAction Stop; }
  101.  
  102. $base = 'https://www.reddit.com/r/wallpapers/new/.rss?sort=top&t=day';
  103. Write-Debug ('$base: ' + $base);
  104.  
  105. $data = Invoke-WebRequest -Uri $base;
  106.  
  107. $xml = [xml]$data.Content;
  108.  
  109. $links = $xml.feed.entry.link.href;
  110.  
  111. $images = @();
  112.  
  113. $links | ForEach-Object {
  114.   $commentXML = [xml](Invoke-WebRequest ($_ + '.rss')).Content;
  115.   if($commentXML.feed.entry[0].content.InnerText -match '"([^"]+\.(jpg|png))">\[link\]') {
  116.     $images += $matches[1];
  117.   }
  118. }
  119.  
  120. $count = $images.Count;
  121.  
  122. if($count -lt 1) {
  123.   throw 'Failed to get reddit images';
  124.   return;
  125. }
  126.  
  127. $use = Get-Random -Maximum $count;
  128. $forDesktop = '';
  129.  
  130. $currentWP = (Get-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name WallPaper).WallPaper;
  131.  
  132. for ($i=0;$i -lt $count;$i++) {
  133.  
  134.   $imageURI = $images[$i]
  135.   Write-Debug ('$imageURI: ' + $imageURI);
  136.  
  137.   $file = Split-Path $imageURI -Leaf; #filename for local use
  138.  
  139.   $savePath = Join-Path $folder $file;
  140.   Write-Debug ('$savePath: ' + $savePath);
  141.  
  142.   if($i -eq $use -or $savePath -eq '') {
  143.    if($forDesktop -ne $currentWP) { $forDesktop = $savePath; } #don't use current image
  144.   }
  145.  
  146.   #don't download if it already exists
  147.   if(Test-Path $savePath) {
  148.     Write-Host ('Image exists: ' + $file);
  149.     continue;
  150.   }
  151.  
  152.   Write-Host ('Retreiving image: ' + $file);
  153.   $result = Invoke-WebRequest -Uri $imageURI -OutFile $savePath;
  154.  
  155. }
  156.  
  157. Write-Host ('Setting wallpaper: ' + $forDesktop);
  158.  
  159. Set-wallpaper -Wallpaper $forDesktop -WallpaperStyle Fill -AutoColorization;
Add Comment
Please, Sign In to add comment