Skip to content

Instantly share code, notes, and snippets.

@elav000
Last active April 24, 2020 13:29
Show Gist options
  • Save elav000/f8f2aa1187bfb910d891dbee839a1c28 to your computer and use it in GitHub Desktop.
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
# $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