# ShotBox folder sync for Windows: downloads every screenshot in your ShotBox account into a folder. # Usage: $env:SHOTBOX_TOKEN="sb_..."; irm https://shots.fullstackpartner.com/sync/shotbox-sync.ps1 | iex # Default folder: Pictures\ShotBox (set $env:SHOTBOX_DIR to change). Checks every 5 seconds. Ctrl+C stops it. $Site = "https://shots.fullstackpartner.com" $Token = "$env:SHOTBOX_TOKEN".Trim() if ($Token -notmatch '^sb_[A-Za-z0-9]{40}$') { Write-Host "Set `$env:SHOTBOX_TOKEN to your sync key (menu > Desktop sync app on $Site)."; return } $Dir = if ($env:SHOTBOX_DIR) { $env:SHOTBOX_DIR } else { Join-Path ([Environment]::GetFolderPath('MyPictures')) 'ShotBox' } New-Item -ItemType Directory -Force -Path $Dir | Out-Null $StateFile = Join-Path $Dir '.shotbox-sync.json' $Seen = @{} if (Test-Path $StateFile) { (Get-Content $StateFile -Raw | ConvertFrom-Json) | ForEach-Object { $Seen[$_] = $true } } $Headers = @{ Authorization = "Bearer $Token" } $Since = 0 Write-Host "ShotBox sync: saving screenshots to $Dir" while ($true) { try { $r = Invoke-RestMethod -Uri "$Site/api/shots?since=$Since&limit=500" -Headers $Headers -TimeoutSec 60 $list = @($r.shots); [array]::Reverse($list) foreach ($s in $list) { if ($s.created -gt $Since) { $Since = $s.created } if ($Seen.ContainsKey($s.id)) { continue } $stamp = [DateTimeOffset]::FromUnixTimeSeconds($s.created).LocalDateTime.ToString('yyyy-MM-dd HH.mm.ss') $label = if ($s.title) { $s.title } elseif ($s.device) { $s.device } else { 'Screenshot' } $label = ($label -replace '[\\/:*?"<>|]+', '').Trim(); if ($label.Length -gt 60) { $label = $label.Substring(0, 60) } $path = Join-Path $Dir "$stamp $label $($s.id).$($s.ext)" Invoke-WebRequest -Uri $s.img -Headers $Headers -OutFile $path -TimeoutSec 120 -UseBasicParsing (Get-Item $path).LastWriteTime = [DateTimeOffset]::FromUnixTimeSeconds($s.created).LocalDateTime $Seen[$s.id] = $true ($Seen.Keys | Sort-Object) | ConvertTo-Json | Set-Content $StateFile Write-Host "saved $(Split-Path $path -Leaf)" } } catch { if ($_.Exception.Response.StatusCode.value__ -eq 401) { Write-Host "Sync key not accepted. Make a new one on the website."; return } Write-Host "waiting for network: $($_.Exception.Message)" } Start-Sleep -Seconds 5 }