Skip to content

Instantly share code, notes, and snippets.

@a-gu
Last active September 6, 2021 23:51
Show Gist options
  • Save a-gu/ba204fc4f58cb39657484fb76c3ce8d0 to your computer and use it in GitHub Desktop.
Save a-gu/ba204fc4f58cb39657484fb76c3ce8d0 to your computer and use it in GitHub Desktop.
Automatically replace *.url files leading to Google Photos URLs on Windows systems. Hacky and hardcoded but mostly works.
Add-Type -AssemblyName System.Windows.Forms
# https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.sendkeys?view=netframework-4.7.2
$Path_Downloads = 'C:\Users\YOUR_USERNAME\Downloads'
function Get-LinkURL {
Param(
[string]$Path
)
Write-Host "> Processing ""$Path""."
If (-Not(Test-Path -LiteralPath $Path -PathType Leaf)) {
Write-Error -Message "Invalid path ""$Path""."
Return
}
$Content = Get-Content -Raw $Path
$Content -match 'URL=([^\n]+)' | Out-Null
$PhotosUrl = $Matches.1
If (-Not $PhotosUrl) {
Write-Error -Message "No URL found in file ""$Path""."
Return
}
If (-Not $PhotosUrl -match '^https://photos.google.com/photo/[^/]+$') {
Write-Error -Message "Link URL ""$PhotosUrl"" is not a Google Photos URL."
Return
}
# download image
$Dl_Before = (Get-ChildItem -Path $Path_Downloads).Name
Write-Host " Opening URL ""$PhotosUrl""."
Start-Process $PhotosUrl
Start-Sleep -Seconds 5
[System.Windows.Forms.SendKeys]::SendWait("%{TAB}")
Start-Sleep -Milliseconds 300
[System.Windows.Forms.SendKeys]::SendWait("%{TAB}")
Start-Sleep -Milliseconds 300
[System.Windows.Forms.SendKeys]::SendWait("+d")
# wait for new download
While (-Not $Dl_Diff) {
$Dl_After = (Get-ChildItem -Path $Path_Downloads -Exclude '*.part','*.crdownload').Name
$Dl_Diff = $Dl_After | Where-Object { $Dl_Before -NotContains $_ }
Start-Sleep -Seconds 1
}
Write-Host " Found new file ""$Dl_After"", closing tab."
[System.Windows.Forms.SendKeys]::SendWait("^w")
# replace with downloaded image
$Source = Join-Path -Path $Path_Downloads -ChildPath $Dl_Diff
$SourceExt = [IO.Path]::GetExtension($Source)
$DestinationExt = [IO.Path]::GetExtension($Path)
$Destination = $Path.Substring(0, $Path.Length - $DestinationExt.Length) + $SourceExt
Write-Host " Removing ""$Path""."
Remove-Item $Path
Write-Host " Moving file ""$Source"" to ""$Destination""."
Move-Item -Force -Path $Source -Destination $Destination
}
Get-ChildItem -Recurse -Filter '*.url' | ForEach-Object {
Get-LinkURL -Path $_.FullName
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment