-
Notifications
You must be signed in to change notification settings - Fork 0
/
set-background-image.ps1
80 lines (61 loc) · 2.16 KB
/
set-background-image.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
$CurrentLocation = $args[0]
function EnsureImagesFolderExists {
$ImagesPath = GetImagesPath
if ( -not (Test-Path $ImagesPath)){
New-Item -ItemType Directory -Path $ImagesPath
}
}
function GetImagesPath {
$CurrentPath = $CurrentLocation
"${CurrentPath}/images"
}
# By Jose Espitia
# https://www.joseespitia.com/2017/09/15/set-wallpaper-powershell-function/
Function Set-WallPaper($Image) {
<#
.SYNOPSIS
Applies a specified wallpaper to the current user's desktop
.PARAMETER Image
Provide the exact path to the image
.EXAMPLE
Set-WallPaper -Image "C:\Wallpaper\Default.jpg"
#>
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public class Params
{
[DllImport("User32.dll",CharSet=CharSet.Unicode)]
public static extern int SystemParametersInfo (Int32 uAction,
Int32 uParam,
String lpvParam,
Int32 fuWinIni);
}
"@
$SPI_SETDESKWALLPAPER = 0x0014
$UpdateIniFile = 0x01
$SendChangeEvent = 0x02
$fWinIni = $UpdateIniFile -bor $SendChangeEvent
$ret = [Params]::SystemParametersInfo($SPI_SETDESKWALLPAPER, 0, $Image, $fWinIni)
}
$SubRedditsSourceFile = "${CurrentLocation}/subreddits.txt"
EnsureImagesFolderExists
$SubReddit = @(Get-Content $SubRedditsSourceFile) | Get-Random
$Uri = "https://reddit.com/r/${SubReddit}/top/.json?limit=25&t=week"
$Response = Invoke-WebRequest -URI $Uri -UseBasicParsing
$ResponseBody = ConvertFrom-Json $Response.Content
foreach($Post in $ResponseBody.data.children)
{
$ImageUrl = $Post.data.url
if ($ImageUrl.EndsWith('.jpg') -or $ImageUrl.EndsWith('.png'))
{
$FileName = $ImageUrl.Split('/')[-1]
$ImagesDirectory = GetImagesPath
$ImagePath = "${ImagesDirectory}\${FileName}"
Invoke-WebRequest $ImageUrl -OutFile $ImagePath -UseBasicParsing
break
}
}
if (-not ([string]::IsNullOrEmpty($ImagePath))){
Set-WallPaper -Image $ImagePath
}