BLOGROLLS

WSLPaper

By: wormius On: Sat 13 July 2024
In: misc
Tags:

I am using WSL currently (eventually will be switching to Linux once Win10 is officially unsupported).

I like having the ability to set a wallpaper background, but I thought it'd be fun to create a script to rotate wallpapers instead of having to manually change it anytime I wanted to update. Since I'm using Windows, I figured I may as well use the default script (Powershell). I debated on somethign like Python or even doing it from within WSL (though upon reflection that wouldn't work - see below)

My first thought was to use a script of some sort to rename my files to the wallpaper name I'm using in WSL. I'd just use a generic name like 'wslpaper.png' for the filename.

As I was looking at the terminal in WSL, I realized that the config is a json file, and thought maybe it would be better to just modify what the json file is pointing to (which is essentially what the UI does when you select the file via a file explorer).

I pondered which was a better approach. The first thing I thought was that the json approach is more minimal. It's only a text file edit.

Renaming a file would take more data writes to the drive. It's not a huge deal, but I did ponder a little and asked my more experienced friend which approach he'd use.

He had some sound advice.

1) The wear and tear on a drive is going to be minimal since this isn't a giant corporate server setup. I'm only doing it once a day and compared to the writing the system does in general for things like caching and backing up and snapshots, etc... let alone any of my other saving and deleting files, this is pretty minimal, so the wear and tear isn't a problem.

2) The json approach is clearly more expandable. It could be modified for other use cases if needed. If I was for some reason planning to do something much more than renaming a file/pointing to a file, then that might be a fine approach but as it is, the image renaming shouldn't be an issue.

I concurred. Mostly because I'm lazy and thought the rename would be simpler. Of course, I may still try the other way for funsies and to learn about parsing json and manipulating it.But for now, a simple shell script was all I needed. What IS that shell script?

$inpaper = Get-Random("D:\My Documents\My Pictures\Wallpaper\wslpaper-folder" | Get-ChildItem)

$wslpaper = "D:\My Documents\My Pictures\Wallpaper\wslpaper-folder\wslpaper.png"
Copy-Item $inpaper $wslpaper

I did attempt to run the script while I was in WSL to see if it would autoupdate while it was running, but it didn't. Because it's loaded in WSL it was locked and unable to be modified. Not a problem as that was what I expected, but would have been cool to do it while I had it open. Since I it wouldn't update while I had WSL open, using a bash script from WSL to modify wouldn't have worked. If I gave a shit I'm sure I could devise a workaround, but it's not that important, since I'm not doing it in WSL itself anwyays.

So my next step is to learn how windows scheduling works and run the script automatically once or twice a day.

If you'd like to mess with it, here's the files

Download the .ps1 script here

Here's a .zip of the 153 .png files

The only thing I'd add is that I didn't test all the images in terms of whether or not they look good in a terminal, I just downloaded a whole bunch that I thought looked nice. They may or may not work, but I know there's definitely a few good ones already (especially if you tweak the opacity of the terminal background).

UPDATE: In the process of using Windows' "Task Scheduler", it turns out that the script needs to be invoked not just by putting the wslpaper.ps1 script into the field of the applicationg to be executed, but also needs to be passed into the "powershell" command with the -File option.

Further, since this was a Powershell 7.0 script (at least it seems that way based on it erroring out using "powershell") it turns out to use the PowerShell Core (e.g. 7.0+ version, that is cross platform), the command to execute is "pwsh" not "powershell".

But NOW it is working and schedules apparently. Will find out in an hour when it's invoked again in theory.


If you found the article helpful, please share or cite the article, and spread the word: