Skip to content

Instantly share code, notes, and snippets.

@Ayehavgunne
Created October 12, 2024 06:57
Show Gist options
  • Save Ayehavgunne/0bd209bd217ad65d89aa8a1e239c5abc to your computer and use it in GitHub Desktop.
Save Ayehavgunne/0bd209bd217ad65d89aa8a1e239c5abc to your computer and use it in GitHub Desktop.
Hyprpaper random wallpaper script with portrait support
#!/usr/bin/env python3.12
import random
from dataclasses import dataclass
from subprocess import PIPE, Popen
WALLPAPERS_PATH = "~/nas/Wallpapers"
@dataclass
class Response:
return_code: int
std_out: str
std_err: str
def send_command(cmd: str) -> Response:
shell = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
std_out, std_err = shell.communicate()
return Response(
shell.returncode, std_out.decode().strip(), std_err.decode().strip()
)
def main() -> None:
landscape_wallpapers = send_command(
f"ls -d {WALLPAPERS_PATH}/*{{.jpg,.png}}"
).std_out.split("\n")
portrait_wallpapers = send_command(
f"ls {WALLPAPERS_PATH}/Portrait/*"
).std_out.split("\n")
monitor_list = send_command("hyprctl monitors").std_out.split("Monitor")
monitor_list = [info.strip() for info in monitor_list if info]
monitor_info = {}
for monitor in monitor_list:
name, body = monitor.split("\n", 1)
body = f"current resolution: {body}"
body = {
line_parts[0].strip().replace(" ", "_"): line_parts[1].strip()
for line in body.split("\n")
if (line_parts := line.split(":"))
}
monitor_info[name.split(" ")[0]] = body
send_command("hyprctl hyprpaper unload all")
for name, monitor in monitor_info.items():
current_resolution = [
int(side) for side in monitor["current_resolution"].split("@")[0].split("x")
]
if current_resolution[0] > current_resolution[1]:
wallpaper = random.choice(landscape_wallpapers)
else:
wallpaper = random.choice(portrait_wallpapers)
send_command(f"hyprctl hyprpaper preload {wallpaper}")
send_command(f'hyprctl hyprpaper wallpaper "{name},{wallpaper}"')
send_command('dunstify "Wallpaper changed!"')
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment