Last active
April 24, 2020 13:29
-
-
Save elav000/f8f2aa1187bfb910d891dbee839a1c28 to your computer and use it in GitHub Desktop.
A small PowerShell script to get the en-US Bing image of the day and update the current user's profiles.json's default backgroundImage setting
This file contains 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
# $TODO: - These should be cmdlet parameters if you go that far... | |
$bingRootUrl = "https://www.bing.com" | |
$marketLocale = "en-US" | |
$numberOfImages = 1 | |
$bingImageInfoUrl = "$bingRootUrl/HPImageArchive.aspx?format=js&idx=0&n=$numberOfImages&mkt=$marketLocale" | |
# Windows Terminal profiles.json location | |
$profilesJsonLocation = "$env:USERPROFILE\AppData\Local\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json" | |
function Get-ImageOfTheDayUrl { | |
try { | |
# Get JSON regarding bing image info of the day | |
Write-Host "๐ Making request to $bingImageInfoUrl..." | |
$bingImageInfo = Invoke-RestMethod $bingImageInfoUrl | |
# Get JSON regarding bing image info of the day | |
if ($null -eq $bingImageInfo.images -or $bingImageInfo.images.length -lt 1) { | |
Write-Host "No image information could be parsed from the response." | |
exit | |
} | |
return $bingImageInfo.images[0].url | |
} catch { | |
Write-Host "๐ Unable to make a request to $bingImageInfoUrl with error:" | |
Write-Host "$_.Exception.Response.StatusCode.value__" | |
Write-Host "Exiting..." | |
exit | |
} | |
} | |
function Set-DefaultProfileToImageOfTheDay { | |
Param( | |
[Parameter(Mandatory=$True)] | |
$absoluteImageUrl | |
) | |
try { | |
Write-Host "๐พ Setting Windows Terminal default profile background image to $absoluteImageUrl..." | |
# $TODO: - check if this exists... | |
$profilesJson = Get-Content $profilesJsonLocation -Raw | ConvertFrom-Json | |
# $TODO: - check if this exists :) | |
$profilesJson.profiles.defaults.backgroundImage = $absoluteImageUrl | |
# Update! | |
$profilesJson | ConvertTo-Json -Depth 32 | Set-Content $profilesJsonLocation | |
} catch | |
{ | |
Write-Host "๐ Unable to update profiles.json with $absoluteImageUrl with error:" | |
Write-Host $_.Exception.GetType().FullName, $_.Exception.Message | |
Write-Host "Exiting..." | |
} finally { | |
} | |
} | |
# Get Image URL | |
$relativeImageUrl = Get-ImageOfTheDayUrl | |
$absoluteImageUrl = $bingRootUrl + $relativeImageUrl | |
# Set Image URL in profiles.json | |
Set-DefaultProfileToImageOfTheDay $absoluteImageUrl | |
# Success! | |
Write-Host "โ๏ธ Successfully updated Windows Terminal Settings to the Bing image of the day ๐" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment