MacOS script to fetch NASA's Astronomy Picture of the Day and set it as a wallpaper. It can be used to automagically fetch a new wallpaper each day 🪄✨
This script can be easly repurposed for other operating systems. For POSIX systems you just need to replace the AppleScript (osascript
) part with the os specific equivalent. For Windows you can try with Windows Subsystem for Linux. If you actually make it, please fork this and post a pull request 🥺
sleep 10 && curl $(echo "https://apod.nasa.gov/apod/$(curl https://apod.nasa.gov/apod/astropix.html | grep -m 1 "png\|jpg\|jpeg" | awk -F '"' '{print $2}')") > /var/tmp/new-wallpaper && osascript -e 'tell application "Finder" to set desktop picture to POSIX file "/var/tmp/new-wallpaper"'
This script:
- Waits 10 seconds. Important, if it's used at booting Mac. You don't want to do it instantly, before (more important) macOS services are fully working.
- Goes into https://apod.nasa.gov/apod/astropix.html website and downloads the HTML code of the main site.
- Scanns the code for the first accurance of the PNG/JPG/JPEG files and glues together URL for that image.
- Downloads the image into
/var/tmp
folder (system's temporary folder). - Uses AppleScript to set up the background image. This step requires permissions to control computer. AppleScript is an Apple scripting language to control UI Applications by commands, and can be easily abused. Always verify AppleScript (
osascript
) you are executing. Fortunately this part of the script almost reads as English so you can verify it with little to no coding abilities 🤓
Just use it with crontab or launch deamon 😎
- Open Automator, create a new Application
- Drag and drop "Run Shell Script"
- Remove everything from textbox and paste the script
- Save it as NASA-Wallpapers.app
- (Optional) Copy NASA-Wallpapers.app to Applications folder
- Go to System Settings -> General -> Login Items
- Add NASA-Wallpapers.app as Login Item
- Download pre-made App and unzip it
- (Optional) Copy it to Applications folder
- (Optional) Open it inside the Automator.app and make sure the script inside is correct!
- Go to System Settings -> General -> Login Items
- Add it as Login Item
Sometimes NASA uploads non-image media, such as youtube videos. In this cases the script obviously won't work properly. Just wait for another day 😆
Example of Youtube video media from May 31, 2023 |