I made a program that is meant to get image urls from unsplash.com, turns a random one into a jpeg file and then updates my mac wallpaper to be the chosen image.
Here's the code:
# from appscript import app, mactypes
import os
import subprocess
import requests
from bs4 import BeautifulSoup
import random
image_urls = []
queries = ["/mac", "/macbook", ""]
SCRIPT = """/usr/bin/osascript<<END
tell application "System Events" to set picture of (reference to current desktop) to "%s"
END"""
def get_image_urls(website_url, photo_url_start):
response = requests.get(website_url).content
soup = BeautifulSoup(response, "html.parser")
images = soup.find_all("img")
for image in images:
src = image["src"]
if photo_url_start in src:
if not src in image_urls:
image_urls.append(src)
for query in queries:
get_image_urls(website_url=f"https://unsplash.com/wallpapers/desktop{query}",
photo_url_start="https://images.unsplash.com/photo")
img_data = requests.get(random.choice(image_urls)).content
with open('wallpaper_background.jpeg', 'wb') as handler:
handler.write(img_data)
abspath = os.path.abspath("wallpaper_background.jpeg")
subprocess.Popen(SCRIPT % abspath, shell=True)
# app('Finder').desktop_picture.set(mactypes.File("wallpaper_background.jpeg"))
The jpeg file is created and shown to be the current wallpaper in system settings, however, it won't actually show up for me. I commented out the previous attempt I made. If anyone knows how I can actually make my wallpaper change I would really appreciate it.