Download space wide screen images with Python

Photo by NASA on Unsplash

Download space wide screen images with Python

And add it as your MacOS background

Theo van der Sluijs's photo
Theo van der Sluijs
·Apr 24, 2023·

2 min read

Play this article

So I would like a new wallpaper every day from outer space.

I love Python so I was thinking about writing a python code to automate this.

So the assignment is:

Create a python script that daily downloads a ultra widescreen image from the galaxy and place it as macos background

To create a Python script that downloads a daily ultra widescreen image from the galaxy and sets it as the macOS background, you can follow these steps:

  1. Install the necessary libraries

  2. Download the image from a source of your choice

  3. Save the image locally

  4. Set the image as the macOS background

Here's a Python script that demonstrates this process using the NASA Astronomy Picture of the Day (APOD) API:

import os
import requests
import json
from datetime import datetime
from appscript import app, mactypes

# Get the APOD API Key from

def download_image(url, filename):
    response = requests.get(url)
    with open(filename, 'wb') as file:

def set_macos_background(filename):

def main():
    date ='%Y-%m-%d')
    api_url = f'{date}&api_key={API_KEY}'

    response = requests.get(api_url)
    apod_data = json.loads(response.text)

    image_url = apod_data['hdurl']
    file_ext = os.path.splitext(image_url)[1]
    filename = os.path.expanduser(f'~/Pictures/apod_{date}{file_ext}')

    download_image(image_url, filename)

if __name__ == '__main__':

To run this script, you'll need to install the appscript library, which you can install using:

pip install appscript

Replace 'YOUR_API_KEY' with your NASA API key, which you can obtain from Please note that this script is designed for macOS, and the NASA APOD API may not always provide an ultra-widescreen image.

To run the script daily, you can set up a cron job on your Mac:

  1. Open Terminal

  2. Type env EDITOR=nano crontab -e and press Enter

  3. Add the following line to the file:

0 0 * * * /path/to/python3 /path/to/
  1. Replace /path/to/python3 with the path to your Python 3 interpreter (use which python3 to find it) and /path/to/ with the path to your script file

  2. Save the file and exit (Ctrl+X, Y, Enter)

This will set up a daily cron job to run the script at midnight.

Did you find this article valuable?

Support Theo van der Sluijs by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
Share this