SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2013-07-29 12:29:23

Tasulian
New Member
Registered: 2013-07-29
Posts: 2

Dual Monitor Nitrogen Wallpaper Script

I have a dual monitor set up, and Nitrogen has the cool feature of being able to set separate wallpapers on each screen. However, I have been trying to set it up so that I can change wallpapers randomly as well. It's proven a bit complicated, but I've come up with a workaround. I found a script that will select a random wallpaper from a folder and change it. However, it does not support dual monitors.

 
#! /bin/bash
#using the path WALLPAPERS will select a new random wallpaper
WALLPAPERS="/filepath/to/wallpapers"
ALIST=( `ls -w1 $WALLPAPERS` )
RANGE=${#ALIST[@]}
let "number = $RANDOM"
let LASTNUM="`cat $WALLPAPERS/.last` + $number"
let "number = $LASTNUM % $RANGE"
echo $number > $WALLPAPERS/.last
nitrogen --set-scaled --save $WALLPAPERS/${ALIST[$number]}
exit

Since Nitrogen does not appear to have a command line way of specifying individual monitors, I had to improvise a bit. I modified the script to change the values stored in bg-saved.cfg before issuing the --restore command to nitrogen.

#! /bin/bash
#using the path WALLPAPERS will select a new random wallpaper
WALLPAPERS="/filepath/to/wallpapers"
ALIST=( `ls -w1 $WALLPAPERS` )
RANGE=${#ALIST[@]}
let "number = $RANDOM"
let LASTNUM="`cat $WALLPAPERS/.last` + $number"
let "number = $LASTNUM % $RANGE"
echo $number > $WALLPAPERS/.last 
sed -i '7 c\file='$WALLPAPERS/${ALIST[$number]}'' /home/user/.config/nitrogen/bg-saved.cfg
let "number = $RANDOM"
let LASTNUM="`cat $WALLPAPERS/.last` + $number"
let "number = $LASTNUM % $RANGE"
echo $number > $WALLPAPERS/.last 
sed -i '12 c\file='$WALLPAPERS/${ALIST[$number]}'' /home/user/.config/nitrogen/bg-saved.cfg
nitrogen --restore
exit

I'm definitely a newbie when it comes to scripting, so if there's an easier way to do this, I'd love to hear about it (For example, what's the most efficient way to code this without so many repeating lines?) As it is, I now have a cron job that uses this script to change my wallpapers on both monitors!

Offline

Be excellent to each other!

#2 2013-07-29 12:51:01

pidsley
Window Mangler
Registered: 2012-05-23
Posts: 1,752

Re: Dual Monitor Nitrogen Wallpaper Script

Well done!

Here is a script that uses a slightly simpler method of selecting a random wallpaper: http://crunchbang.org/forums/viewtopic. … 27#p212827 Of course you would need to modify this to set dual wallpapers, but it might be a simpler way to select each wallpaper.

The solution to the "repeating lines" problem in a bash script is usually to put anything that needs to run more than once in a function, and then call the function. Google (or ixquick) for "using bash functions" for more information. You could put all the code that selects a random wallpaper in a function, and then just call the function twice. Anything that needs to be different (like your sed calls) would be done after the function call.

Here is a script that uses a simple function: http://crunchbang.org/forums/viewtopic. … 38#p215338

It's also sometimes helpful to leave space (empty lines) between sections of code in a script (to make it easier to read), and to add comments so that other people can see what you're doing in the script (or so that you can remember what you were doing when you try to modify the script in a few months after you forget how it works :-)

Last edited by pidsley (2013-07-29 13:48:20)

Offline

#3 2014-08-24 16:20:09

tariqk
New Member
From: Kuala Lumpur
Registered: 2014-08-24
Posts: 2

Re: Dual Monitor Nitrogen Wallpaper Script

(joined the forums so that I could update this topic; hopefully someone finds this update useful).

In any case, here's what I got after several improvements:

#!/bin/bash

# Select random wallpaper for each screen, by fiddling with the configuration file for nitrogen.
# TODO: Modify to use getops, allowing you to specify config file locations, etc.
# TODO: Modify to use an RC file, so that rather than looking at hardcoded shit in this script you go somewhere else. 

CONFIG="$HOME/.config/nitrogen/bg-saved.cfg" # Replace this with whatever the nitrogen config file is.
WALLPAPERDIR="$HOME/wallpapers/"

function setwallpaper {
    # Modify the nitrogen configuration file (default location set in $CONFIG).
    # $1: Line to modify
    # TODO: Modify it so that rather than use a line to modify this, we just use the screen name.
    # Other potential features: allow you to use different modes rather than centre-and-zoom.
    WALLPAPER=`find $WALLPAPERDIR -type f | grep -E "jpeg|jpg|png" | shuf -n1`
    echo "$(date -u) -- Setting line $1 to $WALLPAPER."
    sed -i "$1 c\file=$WALLPAPER" $CONFIG
    }

setwallpaper 7   # screen 1, defined in line #7
setwallpaper 12  # screen 2, defined in line #12

# Refresh Nitrogen with new wallpaper.
nitrogen --restore

Tested and ran this on the console, appeared to work. Success!

Therefore, I put this in cron as a vanilla cron entry, since I had placed it in my $HOME/bin directory, naïvely thinking that it would the same way as it did on the console. Nope. Turns out that the environment variables aren't the same as one's console. So fixed that by putting in the full path.

But then I noticed that it was throwing this error:

Gtk-WARNING **: cannot open display:

…which I then discovered were caused because cron couldn't see a DISPLAY variable. So I put in a DISPLAY variable. This is what the final cron entry looks like:

* * * * * DISPLAY=:0 path/to/dir/cycle-wallpapers >> /path/to/dir/errors.log 2>&1

This time, it worked. Hope someone finds this useful. I'll tinker around with this and see if I can make it a little more flexible, per the comments in my script.

Offline

#4 2014-08-24 18:13:45

hrnn187
Member
Registered: 2014-07-29
Posts: 20

Re: Dual Monitor Nitrogen Wallpaper Script

I posted recently the script I'm using to change wallpapers. it written in python, though.

http://crunchbang.org/forums/viewtopic.php?id=36375

I have no idea about dual monitors but probably by tinkering the DISPLAY=:0 property you can select which wallpaper to change.

Offline

#5 2015-01-07 08:03:02

OrganizedFellow
#! Member
Registered: 2014-09-02
Posts: 59

Re: Dual Monitor Nitrogen Wallpaper Script

Hi everyone. I would like some help writing a bash script (I think that's what I need?)

  • I use Tapet to generate wallpapers on my Nexus4, they are saved to a folder.

  • I use another app called FolderSync to sync that folder to another folder on my Dropbox. I run Dropbox on my Crunchbang. Path=~/Dropbox/wallpaperstapet/ When I select 'Choose Wallpaper', the wallpaper application (is it called Nitrogen?) is set to select from the Tapet wallpaper folder on my Dropbox.

  • I'm sure it's possible, but can I write a bash script to select the newest added wallpaper?

#!/bin/sh

FOLDER=/home/jaimito/Dropbox/wallpaperstapet/
SELECT=ls -t | head -n 1



nitrogen --save --set-scaled /home/jaimito/Dropbox/wallpaperstapet/${SELECT}

exit

That's as far as I got.
smile
Of course it doesn't do anything, lol. First bash script I'm writing.


On my device, Tapet rotates every hour, so it displays a new wallpaper every hour. So each hour, a new wallpaper gets synced to the Dropbox folder.

I think I need a cronjob to rotate my wallpaper on my desktop every hour from the latest new wallpaper the FolderSync sends to my Dropbox wallpapers folder, right?

I'm just asking for some help writing my first bash script which I think will be fun.
I'm not asking someone to write it out for me, oh no! I'd like to learn.

Offline

#6 2015-01-07 08:22:38

machinebacon
#! unstable
From: China
Registered: 2009-07-02
Posts: 6,826
Website

Re: Dual Monitor Nitrogen Wallpaper Script

Something like

#!/bin/bash

WALLS=/home/$USER/wallpapers
# show newest (last modified) file in $WALLS
NEWPIC=`ls $WALLS -tp | grep -v /$ | head -1`

nitrogen --save --set-scaled $WALLS/$NEWPIC

Untested, but should work.


Sweaty lads picking up the soap | I love the new "Ignore user" button

Offline

#7 2015-03-08 03:33:15

pingu
#! Junkie
Registered: 2014-12-05
Posts: 367

Re: Dual Monitor Nitrogen Wallpaper Script

tariqk wrote:

(joined the forums so that I could update this topic; hopefully someone finds this update useful).

In any case, here's what I got after several improvements:

#!/bin/bash

# Select random wallpaper for each screen, by fiddling with the configuration file for nitrogen.
# TODO: Modify to use getops, allowing you to specify config file locations, etc.
# TODO: Modify to use an RC file, so that rather than looking at hardcoded shit in this script you go somewhere else. 

CONFIG="$HOME/.config/nitrogen/bg-saved.cfg" # Replace this with whatever the nitrogen config file is.
WALLPAPERDIR="$HOME/wallpapers/"

function setwallpaper {
    # Modify the nitrogen configuration file (default location set in $CONFIG).
    # $1: Line to modify
    # TODO: Modify it so that rather than use a line to modify this, we just use the screen name.
    # Other potential features: allow you to use different modes rather than centre-and-zoom.
    WALLPAPER=`find $WALLPAPERDIR -type f | grep -E "jpeg|jpg|png" | shuf -n1`
    echo "$(date -u) -- Setting line $1 to $WALLPAPER."
    sed -i "$1 c\file=$WALLPAPER" $CONFIG
    }

setwallpaper 7   # screen 1, defined in line #7
setwallpaper 12  # screen 2, defined in line #12

# Refresh Nitrogen with new wallpaper.
nitrogen --restore

Tested and ran this on the console, appeared to work. Success!

Therefore, I put this in cron as a vanilla cron entry, since I had placed it in my $HOME/bin directory, naïvely thinking that it would the same way as it did on the console. Nope. Turns out that the environment variables aren't the same as one's console. So fixed that by putting in the full path.

But then I noticed that it was throwing this error:

Gtk-WARNING **: cannot open display:

…which I then discovered were caused because cron couldn't see a DISPLAY variable. So I put in a DISPLAY variable. This is what the final cron entry looks like:

* * * * * DISPLAY=:0 path/to/dir/cycle-wallpapers >> /path/to/dir/errors.log 2>&1

This time, it worked. Hope someone finds this useful. I'll tinker around with this and see if I can make it a little more flexible, per the comments in my script.

I'm using this scrip but using while and sleep  to make it change every 5min instead of using cron for it (I din't want to have to start crond just for that). Does anybody know if this method is  "bad" in any way?

Last edited by pingu (2015-03-08 03:41:33)


"We don't merge kernel code just because user space was written by a retarded monkey on crack."
Linus f****g Trovalds

Offline

#8 2015-09-14 20:59:44

LaneLester
Member
From: North Georgia, NA
Registered: 2010-03-22
Posts: 31
Website

Re: Dual Monitor Nitrogen Wallpaper Script

I'm glad I was able to google this thread! The two-monitor wallpaper problem in Extix (Debian with LXQT) was driving me crazy. Nitrogen wouldn't work at all until I went into LXQT's Session Settings, unchecked Desktop, and clicked Stop.

I'm using tariqk's script with success, and because I'm running conky, all I had to do to get periodic changes is put this in conkyrc:
${execi 60 /opt/WallpaperChange2.sh}

I did find I had to remove the echo line in the script to keep it from printing in the conky window.

Lane

Last edited by LaneLester (2015-09-14 21:25:15)

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.
Server: acrobat

Debian Logo