如何通过终端自动抓取网站图片并更新桌面壁纸?
Hey Tom, I feel your pain—manually updating that wallpaper multiple times a day is such a tedious distraction when you’re focused on astronomy work. Let’s walk through simple, OS-specific solutions that require zero C language skills and minimal command line effort:
Windows Solution
We’ll use a PowerShell script (built right into Windows, no extra software needed) and Task Scheduler to handle auto-updates:
Grab the direct image URL:
- Open your clear sky chart page, right-click the actual weather graph, and select Copy image address. For your Yerkes Observatory link, this should be something like
http://www.cleardarksky.com/c/YerkesObILc.gif.
- Open your clear sky chart page, right-click the actual weather graph, and select Copy image address. For your Yerkes Observatory link, this should be something like
Create the PowerShell script:
- Open Notepad, paste the code below, replace
YOUR_IMAGE_URLwith the copied address, then save it asSet-ClearSkyWallpaper.ps1(make sure to select "All Files" as the save type to avoid a.txtextension):
# Download the latest clear sky chart to your temp folder $imagePath = "$env:TEMP\ClearSkyChart.gif" Invoke-WebRequest -Uri "YOUR_IMAGE_URL" -OutFile $imagePath # Set the image as desktop wallpaper (using fill mode) Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name Wallpaper -Value $imagePath Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name WallpaperStyle -Value 10 # 10=Fill, 6=Stretch, 0=Center Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name TileWallpaper -Value 0 # Force wallpaper update RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters 1, True- Open Notepad, paste the code below, replace
Schedule automatic runs with Task Scheduler:
- Open Task Scheduler → Create Basic Task
- Name it "Update Clear Sky Wallpaper", set a trigger (e.g., every hour, or specific times of day when you need frequent updates)
- For the action, select Start a program, enter
powershell.exeas the program/script, and add-ExecutionPolicy Bypass -File "C:\Path\To\Your\Set-ClearSkyWallpaper.ps1"as arguments (replace the path with where you saved the script) - Finish the wizard, and your wallpaper will refresh automatically on schedule!
macOS Solution
We’ll use a simple shell script and either Calendar or a LaunchAgent to handle auto-updates:
Grab the direct image URL (same step as Windows above).
Create the shell script:
- Open TextEdit, go to Format → Make Plain Text, paste the code below, replace
YOUR_IMAGE_URLwith the copied address, then save it asupdate-wallpaper.sh:
#!/bin/bash IMAGE_PATH="$HOME/Documents/ClearSkyChart.gif" # Download the latest chart curl -o "$IMAGE_PATH" "YOUR_IMAGE_URL" # Set as desktop wallpaper (works for macOS 10.14+) osascript -e 'tell application "System Events" to set picture of every desktop to POSIX file "'"$IMAGE_PATH"'"'- Open Terminal, run
chmod +x ~/Documents/update-wallpaper.shto make the script executable.
- Open TextEdit, go to Format → Make Plain Text, paste the code below, replace
Schedule automatic runs:
- Option 1 (Super Easy): Use Calendar app
- Create a new event, set the repeat interval (e.g., every hour)
- Under "Alert", select Custom, then Open file, and choose your
update-wallpaper.shscript
- Option 2 (More Reliable): Use LaunchAgent
- Create a file named
com.yourname.clearskywallpaper.plistin~/Library/LaunchAgents/with the code below (replace paths with your username/script location):<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.yourname.clearskywallpaper</string> <key>ProgramArguments</key> <array> <string>/Users/YourUsername/Documents/update-wallpaper.sh</string> </array> <key>StartInterval</key> <integer>3600</integer> <!-- Runs every 3600 seconds (1 hour) --> <key>RunAtLoad</key> <true/> </dict> </plist> - Run
launchctl load ~/Library/LaunchAgents/com.yourname.clearskywallpaper.plistin Terminal to activate it.
- Create a file named
- Option 1 (Super Easy): Use Calendar app
Linux Solution (GNOME/KDE)
Most Linux desktops have built-in commands to set wallpapers—we’ll use a shell script and cron for scheduling:
For GNOME (Ubuntu, Fedora, etc.)
- Grab the direct image URL.
- Create the script:
- Open a text editor, paste the code below, save as
update-wallpaper.shin your Pictures folder:
#!/bin/bash IMAGE_PATH="$HOME/Pictures/ClearSkyChart.gif" wget -O "$IMAGE_PATH" "YOUR_IMAGE_URL" # Set as wallpaper with zoom mode gsettings set org.gnome.desktop.background picture-uri "file://$IMAGE_PATH" gsettings set org.gnome.desktop.background picture-options 'zoom' # Options: 'none', 'centered', 'stretched', 'zoom'- Make it executable:
chmod +x ~/Pictures/update-wallpaper.sh
- Open a text editor, paste the code below, save as
- Schedule with cron:
- Run
crontab -ein Terminal, add a line like:
This runs the script every hour (adjust the timing as needed—0 * * * * /home/YourUsername/Pictures/update-wallpaper.sh0 * * * *means top of every hour).
- Run
For KDE
Replace the GNOME gsettings lines in the script with this command to set the wallpaper for all KDE desktops:
qdbus org.kde.plasmashell /PlasmaShell org.kde.PlasmaShell.evaluateScript ' var allDesktops = desktops(); for (i=0;i<allDesktops.length;i++) { d = allDesktops[i]; d.wallpaperPlugin = "org.kde.image"; d.currentConfigGroup = Array("Wallpaper", "org.kde.image", "General"); d.writeConfig("Image", "file://'"$IMAGE_PATH"'"); } '
内容的提问来源于stack exchange,提问作者Tom Sachen

