How to "pass" an image to a bash script

Good day,
So I have polybar installed on i3 and using one of adi1090x’s themes (colorblocks) with pywal (a colour scheme app) enabled. In my i3 config I have a feh command that sets a random wallpaper from a folder of pictures which is working fine.

What I want to achieve is if I set a random wallpaper be it with command line or through the i3 config will it be possible to execute the pywal script in adi1090x’s config and point it to the current wallpaper automatically so that the colours and theme matches the wallpaper?

I’ll gladly clarify if there’s any confusion from above. Thanks!

When setting the desktop wallpaper, just save the filename of the image to an environment variable, so that you can refer to it whenever you want.

2 Likes

Hi thanks for the quick reply!

I’m guessing this involves bash scripting which I have no idea whatsoever about (this is actually an incentive to start learning) so how would I be able to do this please?

Thanks!

Yes, this involves some Bash scripting, it’s not trivial.

I’m not exactly sure how --randomize works with feh, does it output the filename? If it does, you can use process substitution so save the image into a variable.

But if not, you’ll have to figure out how to select the image yourself.

If you have an array with paths to image files, which you can create like this (assuming all images have a jpg extension):

image_list=()
while read f; do 
    image_list+=("$f")
done < <(ls -1 -d "/path/to/directory/"*.jpg 2> /dev/null )

a reliable way to pick a random element is

random_image=${image_list["$[RANDOM % ${#image_list[@]}]"]}

And then you can use "$random_image" instead of path in both the feh command and pywal.

This is just one potential way to do it.

Hi again

I decided to stick with one wallpaper and changed the polybar config
I have saved this script somewhere though and will start learning bash scripting soon!

Thanks again

1 Like