1
Fork 0
dotfiles/.bauke/scripts/desktop-wallpaper.ts

68 lines
1.8 KiB
TypeScript
Raw Normal View History

import { Command } from "./dependencies.ts";
2022-12-13 14:03:00 +00:00
const imagePath = new URL("../data/wallpaper.jpg", import.meta.url).pathname;
async function main(): Promise<void> {
const { options } = await new Command()
.name("desktop-wallpaper")
.description("Desktop wallpaper changer for XFCE")
.option("--unsplash", "Download a random Unsplash wallpaper")
.option("--height <height:number>", "The height of the image", {
default: 1080,
depends: ["unsplash"],
})
2023-01-28 15:04:17 +00:00
.option(
"--save-current <file:file>",
"Save the current wallpaper to a different file",
)
.option("--set <file:file>", "Set a file as the wallpaper")
2022-12-13 14:03:00 +00:00
.option("--width <width:number>", "The width of the image", {
default: 1920,
depends: ["unsplash"],
})
.parse(Deno.args);
2023-01-28 15:04:17 +00:00
if (options.saveCurrent) {
await Deno.copyFile(imagePath, options.saveCurrent);
}
if (options.set) {
await setWallpaper(options.set);
}
2022-12-13 14:03:00 +00:00
if (options.unsplash) {
await downloadImage(
`https://source.unsplash.com/random/${options.width}x${options.height}`,
);
console.log("Saved Unsplash image to");
console.log("-", imagePath);
await setWallpaper();
}
}
async function downloadImage(url: string): Promise<void> {
await new Deno.Command("curl", {
args: ["-fsLS", url, "-o", imagePath],
}).output();
2022-12-13 14:03:00 +00:00
}
async function setWallpaper(file: string = imagePath): Promise<void> {
2022-12-13 14:03:00 +00:00
const monitors = ["monitorHDMI-0", "monitorHDMI-1"];
for (const monitor of monitors) {
await new Deno.Command("xfconf-query", {
args: [
2022-12-13 14:03:00 +00:00
"-c",
"xfce4-desktop",
"-p",
`/backdrop/screen0/${monitor}/workspace0/last-image`,
"-s",
file,
2022-12-13 14:03:00 +00:00
],
}).output();
2022-12-13 14:03:00 +00:00
}
}
if (import.meta.main) {
void main();
}