Chrome下载图片自动压缩实现求助:PowerShell与AutoHotkey脚本问题排查及替代方案咨询
Hey there! It sounds like you were really close to getting this working with AutoHotkey and Pingo—let's break down what might have gone wrong, fix up your scripts, and also look at a more reliable PowerShell approach.
First: Troubleshooting Your AutoHotkey Script
Looking at your code, the biggest red flag is that you're using a %folder% variable but never defining it! That's probably why it stopped working after you made changes—you might have removed or moved the line that sets this path. Let's fix that and address a few other small issues:
- Define your download folder path: You need to explicitly set where Chrome saves your downloads (e.g.,
C:\Users\JohnPC\Downloads). - Avoid re-compressing already processed files: Your current script will re-run Pingo on every image every 5 seconds, which is unnecessary. We'll add a way to track which files have already been compressed.
- Temporarily remove the
Hideparameter: This will let you see if Pingo throws any error messages (like permission issues or incorrect file paths) that were hidden before.
Here's a revised version of your AHK script:
; Replace this with your actual Chrome download folder path folder := "C:\Users\JohnPC\Downloads" ; Store paths of already compressed files to avoid repeats processedFiles := {} ; First, compress existing images in the folder ProcessImages() ; Start monitoring for new images Loop { ProcessImages() Sleep, 5000 ; Check every 5 seconds—adjust as needed } ProcessImages() { global folder, processedFiles Loop, Files, %folder%\*.* { ; Target only image formats we care about if (A_LoopFileExt in ".jpg,.png,.bmp") { ; Skip files we've already compressed if !processedFiles.HasKey(A_LoopFileFullPath) { ; Run Pingo without hiding first—add ",, Hide" back once it works RunWait, "C:\Users\JohnPC\Documents\TOOLS\ping64\pingo.exe" -s9 "%A_LoopFileFullPath%" ; Mark this file as processed processedFiles[A_LoopFileFullPath] := true } } } }
Quick checks to run:
- Make sure the
folderpath matches your actual Chrome download location. - Double-check that the Pingo path is correct (no typos in the file path).
- Run the script and watch for any Pingo error windows—this will tell you if there's a permission issue (e.g., Pingo can't write to the download folder) or corrupted files.
PowerShell Alternative: Use FileSystemWatcher (More Efficient)
Instead of polling the folder every 5 seconds, PowerShell's FileSystemWatcher can detect when a new file is added instantly. This is more efficient and less likely to miss files or process them before they're fully downloaded.
Here's a working PowerShell script:
# Set your Chrome download folder path $downloadFolder = "C:\Users\JohnPC\Downloads" # Path to your Pingo executable $pingoPath = "C:\Users\JohnPC\Documents\TOOLS\ping64\pingo.exe" # Image formats to target $targetExtensions = @(".jpg", ".png", ".bmp") # Create the folder watcher $watcher = New-Object System.IO.FileSystemWatcher $watcher.Path = $downloadFolder $watcher.Filter = "*.*" $watcher.IncludeSubdirectories = $false $watcher.EnableRaisingEvents = $true # Define what happens when a new file is created $fileCreatedHandler = Register-ObjectEvent $watcher "Created" -Action { $filePath = $Event.SourceEventArgs.FullPath $fileExt = [System.IO.Path]::GetExtension($filePath).ToLower() # Only process our target image formats if ($targetExtensions -contains $fileExt) { # Wait 1 second to ensure the file finishes downloading (prevents "file in use" errors) Start-Sleep -Seconds 1 # Run Pingo with your desired compression level & $pingoPath -s9 $filePath } } Write-Host "Monitoring $downloadFolder for new images... Press Ctrl+C to stop monitoring." # Keep the script running until you interrupt it do { Start-Sleep -Seconds 1 } while ($true)
To use this:
- Save it as
CompressDownloadedImages.ps1. - Right-click and run with PowerShell (or run it from a PowerShell window).
- If you get a execution policy error, run
Set-ExecutionPolicy RemoteSigned(as admin) to allow local scripts to run.
Other Things to Check
- Pingo Permissions: Make sure Pingo has write access to your download folder. If you're running the script as a standard user, this should be fine, but if you've changed folder permissions, that could block it.
- Chrome Download Settings: Ensure Chrome isn't set to "Ask where to save each file"—this would break the folder monitoring, since files might go to different locations.
- Alternative Tools: If Pingo is giving you trouble, you could try command-line versions of tools like ImageOptim or MozJPEG, but Pingo is a great choice for lossless/lossy compression.
Let me know if you hit any snags with either script—I can help you debug further!
备注:内容来源于stack exchange,提问作者JohnTry

