From 6db6561bd68c67547e6a35e9058d58881742d412 Mon Sep 17 00:00:00 2001 From: Late Night Defender Date: Tue, 4 Feb 2025 01:51:58 +0700 Subject: [PATCH] Add files --- README.md | 41 ++++++++++++++++++++++++++++++-- change-wallpaper.py | 51 ++++++++++++++++++++++++++++++++++++++++ change-wallpaper.service | 9 +++++++ change-wallpaper.timer | 8 +++++++ 4 files changed, 107 insertions(+), 2 deletions(-) create mode 100755 change-wallpaper.py create mode 100644 change-wallpaper.service create mode 100644 change-wallpaper.timer diff --git a/README.md b/README.md index e617078..fb8c6c6 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,40 @@ -# random-wallpaper-gnome +# Random Wallpaper for GNOME -Randomly pick a wallpaper for your GNOME desktop, without any shell extensions. \ No newline at end of file +A Python script to pick a random photo from the specified directory, set the wallpaper (for both light and dark) and the accent color. + +## Why does this exist + +We'd like to change our wallpaper every 15 minutes and automatically set a pre-defined accent color for each photo. There are many GNOME Shell extensions out there that can do something similar, but we simply want to achieve the same effect **without using any extensions**, especially when upgrading to a pre-release GNOME version that many extensions don't support yet. + +So instead of a GNOME Shell extension, we created a Python script that does everything via `gsettings` and hook it up to a systemd timer instead. + +## How to use + +1. Create a directory with desired photos and note its full path +2. In that directory, rename your photos according to this table + +*Suppose the filename is example.jpg* + +|Accent Color |Suffix |Expected filename | +|--------------|-------|-------------------| +|Blue |blue |example-blue.jpg | +|Teal |teal |example-teal.jpg | +|Green |gree |example-gree.jpg | +|Yellow |yell |example-yell.jpg | +|Red |redd |example-redd.jpg | +|Pink |pink |example-pink.jpg | +|Purple |purp |example-purp.jpg | +|Slate |- |*any file without valid suffix above*| + +*This means if there is no valid color suffix at the end of file name, we will set the accent color to Slate.* + +3. In `change-wallpaper.py`, replace the `directory_path` with the path from step 1 +4. Put `change-wallpaper.py` to `~/.local/bin` and make sure it is in your `$PATH` +5. In `change-wallpaper.service` replace `youruser` in `ExecStart` line with your actual username. +6. **Optionally**, modify `change-wallpaper.timer` as needed. The default is set to *every 15 minutes*. +7. Put `change-wallpaper.service` and `change-wallpaper.timer` in `~/.config/systemd/user/` and run these commands to get started + +``` +$ systemctl --user daemon-reload +$ systemctl --user enable --now change-wallpaper +``` \ No newline at end of file diff --git a/change-wallpaper.py b/change-wallpaper.py new file mode 100755 index 0000000..dc2a285 --- /dev/null +++ b/change-wallpaper.py @@ -0,0 +1,51 @@ +#!/usr/bin/python + +import os +import random + +# Specify the directory +directory_path = '/path/to/your/wallpapers' + +def pick_random_file(directory): + # List all files in the specified directory + files = [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))] + + # Check if the directory is empty + if not files: + return None + + # Pick a random file from the list + random_file = random.choice(files) + + # Return the full path of the randomly picked file + return os.path.join(directory, random_file) + +# Pick a random file +random_file_path = pick_random_file(directory_path) + +if random_file_path[-8:-4] == "blue": + accent_color = "blue" +elif random_file_path[-8:-4] == "teal": + accent_color = "teal" +elif random_file_path[-8:-4] == "gree": + accent_color = "green" +elif random_file_path[-8:-4] == "yell": + accent_color = "yellow" +elif random_file_path[-8:-4] == "oran": + accent_color = "orange" +elif random_file_path[-8:-4] == "redd": + accent_color = "red" +elif random_file_path[-8:-4] == "pink": + accent_color = "pink" +elif random_file_path[-8:-4] == "purp": + accent_color = "purple" +else: + accent_color = "slate" + +# print(f'DEBUG: Using background picture {random_file_path}') +# print(f'DEBUG: Using accent color {accent_color}') + +os.system(f'gsettings set org.gnome.desktop.background picture-uri "file://{random_file_path}"') +os.system(f'gsettings set org.gnome.desktop.background picture-uri-dark "file://{random_file_path}"') +os.system(f'gsettings set org.gnome.desktop.interface accent-color {accent_color}') + diff --git a/change-wallpaper.service b/change-wallpaper.service new file mode 100644 index 0000000..871b070 --- /dev/null +++ b/change-wallpaper.service @@ -0,0 +1,9 @@ +[Unit] +Description=Change wallpaper at specified interval + +[Service] +Type=oneshot +ExecStart=/home/youruser/.local/bin/change-wallpaper.py + +[Install] +WantedBy=default.target diff --git a/change-wallpaper.timer b/change-wallpaper.timer new file mode 100644 index 0000000..883aa30 --- /dev/null +++ b/change-wallpaper.timer @@ -0,0 +1,8 @@ +[Unit] +Description=Change wallpaper every 15 minutes + +[Timer] +OnCalendar=*:0/15 + +[Install] +WantedBy=default.target