Skip to content

Instantly share code, notes, and snippets.

@romero126
Created June 12, 2020 17:15
Show Gist options
  • Save romero126/94ecb7b445c7ca7a94d92dbcdce46b1c to your computer and use it in GitHub Desktop.
Save romero126/94ecb7b445c7ca7a94d92dbcdce46b1c to your computer and use it in GitHub Desktop.
Windows Terminal
function Get-WTSettingsPath {
# Determine if we are in WT
$Profile = $env:WT_PROFILE_ID
if (!$Profile)
{
throw "WT Not available"
}
$SettingsPath = Get-Item "$env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminal*\LocalState\" | Select-Object -ExpandProperty FullName
$SettingsPath
}
function Set-WTBackground {
[CmdletBinding()]
param(
[Parameter()]
[ValidateScript({
Test-Path $_
})]
[String]$Path,
[Parameter()]
[ValidateSet("jpg")]
[string]$Extension
)
begin {
# Determine if we are in WT
$Profile = $env:WT_PROFILE_ID
if (!$Profile)
{
throw "WT Not available"
}
$PathInfo = Get-Item -Path $Path
$SettingsPath = Get-WTSettingsPath
$SettingsJson = "$SettingsPath\Settings.json"
# Get MD5 of File
$FileHash = Get-FileHash -Path $Path -Algorithm SHA1 | Select -ExpandProperty Hash
$FileHash = $FileHash[-1..-5] -join ""
$ImageDestination = "$SettingsPath\Assets\$Profile-$FileHash.$($Extension)"
}
process {
Move-Item -Path $Path -Destination $ImageDestination -Force -ErrorAction SilentlyContinue
$Settings = Get-Content -Path $SettingsJson
$Settings -Replace "(`"backgroundImage`": `".*\/$Profile)-(.{5})\.(.*)`"", "`$1-$($FileHash).`$3`"" | Out-File -FilePath $SettingsPath\Settings.json -Force
}
end {
#Cleanup files
Start-Sleep .5
Get-ChildItem "$SettingsPath\Assets\$Profile*" -Exclude "$Profile-$FileHash*" | Remove-Item
}
}
function Get-BingSearchImage {
param(
[Parameter()]
[String]$Query = "Powershell Wallpaper",
[Parameter(Mandatory)]
[string]$Path
)
$Query = $Query.Replace(" ", "+")
$local:ProgressPreference = "SilentlyContinue"
$uri = "https://www.bing.com/search?q=$Query"
$RestResult = Invoke-RestMethod -Uri $Uri
$Result = ([regex]::Matches($RestResult, "src=`"(.*?)`"") | ? Value -match "OIP" | Get-Random).Groups[1].Value
Invoke-RestMethod -Uri $Result -OutFile $Path
}
function Set-BingSearchImage
{
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[String]$Query
)
$Path = New-TemporaryFile
Get-BingSearchImage -Path $Path -Query $Query
Set-WTBackground -Path $Path -Extension "jpg"
}
function Get-BingWallPaper
{
param(
[Parameter()]
[string]$Path,
[Parameter()]
[ValidateRange(1,8)]
[int]$Results = 1,
[Parameter()]
[ValidateSet("en-US")]
[string]$Region = "en-US",
[Parameter()]
[ValidateSet('1080x1920','768x1366','240x400','480x800','768x1280','240x320','360x480','480x640','768x1024','176x220','240x240','320x320','220x176','1024x768','320x240','480x360','640x480','800x600','1920x1200','1280x768','400x240','800x480','1280x720','1366x768','1920x1080')]
[string]$Resolution = "1920x1200"
)
$local:ProgressPreference = "SilentlyContinue"
$urlbase = "http://www.bing.com"
$uri = "{0}/HPImageArchive.aspx?format=xml&idx={1}&n={2}&mkt={3}" -f $urlbase, 0, $Results, $Region
$Result = Invoke-RestMethod -uri $uri
$RandomImage = $Result.Images.image | Get-Random
$ImageURI = "{0}{1}_{2}.jpg" -f $urlbase, $RandomImage.UrlBase,$Resolution
Invoke-RestMethod -Uri $ImageUri -OutFile $Path
}
function Set-BingWallPaper
{
[CmdletBinding()]
param(
[Parameter()]
[string]$Path,
[Parameter()]
[ValidateRange(1,8)]
[int]$Results = 1,
[Parameter()]
[ValidateSet("en-US")]
[string]$Region = "en-US",
[Parameter()]
[ValidateSet('1080x1920','768x1366','240x400','480x800','768x1280','240x320','360x480','480x640','768x1024','176x220','240x240','320x320','220x176','1024x768','320x240','480x360','640x480','800x600','1920x1200','1280x768','400x240','800x480','1280x720','1366x768','1920x1080')]
[string]$Resolution = "1920x1200"
)
$Path = New-TemporaryFile
Get-BingWallPaper -Path $Path -Results $Results -Region $Region -Resolution $Resolution
Set-WTBackground -Path $Path -Extension "jpg"
}
function Get-RandomCat
{
param(
[Parameter()]
[string]$Path
)
$local:ProgressPreference = "SilentlyContinue"
$uri = "http://aws.random.cat/meow"
$RestResult = Invoke-RestMethod -Uri $Uri
Invoke-RestMethod -Uri $RestResult.File -OutFile $Path
}
function Set-RandomCat
{
$Path = New-TemporaryFile
Get-RandomCat -Path $Path
Set-WTBackground -Path $Path -Extension "jpg"
}
# Quick Note
# Set your background image to match the guid of your Profile as we are looking for that to run this
# "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
# "backgroundImage": "ms-appdata:///local/Assets//{574e775e-4f2a-5b96-ac1e-a2962a402336}-9CE57.jpg",
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment