How to Change The Desktop Wallpaper on Linux from Within a Shell/Bash Script

Shell script changing desktop wallpaper

#!/bin/bash
wallpaperdir='$HOME/wallpaper'

files=($wallpaperdir/*)
randompic=`printf "%s\n" "${files[RANDOM % ${#files[@]}]}"`

gconftool-2 -t str --set /desktop/gnome/background/picture_filename "$randompic"

Save this script and edit your with the command "crontab -e" (it launches an editor where you put this line at the end of the file):

*/1     *     *     *     *         /bin/bash /path/to/script.sh

edit: I assumed you're using gnome. If not you need to edit the last line, because my example uses the Gnome Conftool. ;)

To change the background in XFCE, you should change the line with gconftool-2 to:

echo -e “# xfce backdrop list\n$randompic”>$HOME/.config/xfce4/desktop/backdrops.list    
killall -USR1 xfdesktop

Is there a universal way to set a wallpaper on Linux?

This is the best solution i found, but I bet it could be improved. I scan $XDG_CURRENT_DESKTOP first and then $GDM_SESSION (apparently the xdg doesn't always existas an environment variable). Then I act based on what environment I found and set the wallpaper.

public static void main(String[] args) {
String wpPath = "/path/to/file";
String os = System.getProperty("os.name");
switch (os) {
case "Windows 10":
System.out.println("WINODWS DETECTED");
break;
case "Linux":
String de = identifyDE();
if (de == null) {
System.out.println("Couldn't identify your Desktop Environment"); // log Severe
break;
}

switch (de) {
case "xfce":
executeProcess("xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitorVGA-1/workspace0/last-image -s \"" + wpPath + "\"");
break;
case "gnome":
executeProcess("gsettings set org.gnome.desktop.background draw-background false && gsettings set org.gnome.desktop.background picture-uri \"file://" + wpPath + "\" && gsettings set org.gnome.desktop.background draw-background true");
break;
case "kde":
executeProcess("qdbus org.kde.plasmashell /PlasmaShell org.kde.PlasmaShell.evaluateScript 'var allDesktops = desktops();print (allDesktops);for (i=0;i<allDesktops.length;i++) {d = allDesktops[i];d.wallpaperPlugin = \"org.kde.image\";d.currentConfigGroup = Array(\"Wallpaper\", \"org.kde.image\", \"General\");d.writeConfig(\"Image\", \"" + wpPath + "\")}'");
break;
case "unity":
executeProcess("gsettings set org.gnome.desktop.background picture-uri \"file://" + wpPath + "\"");
break;
case "cinnamon":
executeProcess("gsettings set org.cinnamon.desktop.background picture-uri \"file://" + wpPath + "\"");
break;
default:
System.out.println("Can't recognize DE: " + de);
}

break;
default:
System.out.println("Can't recognize OS: " + os);
}
}

public static String identifyDE() {
String de;
de = System.getenv("XDG_CURRENT_DESKTOP").toLowerCase();

if (de.contains("xfce")) {
return "xfce";
} else if (de.contains("kde")) {
return "kde";
} else if (de.contains("unity")) {
return "unity";
} else if (de.contains("gnome")) {
return "gnome";
} else if (de.contains("cinnamon")) {
return "cinnamon";
} else if (de.contains("mate")) {
return "mate";
} else if (de.contains("deepin")) {
return "deepin";
} else if (de.contains("budgie")) {
return "budgie";
} else if (de.contains("lxqt")) {
return "lxqt";
} else {
System.out.println("Not identifiable with: echo $XDG_CURRENT_DESKTOP: " + de);
}

de = System.getenv("GDM_SESSION").toLowerCase();

if (de.contains("xfce")) {
return "xfce";
} else if (de.contains("kde")) {
return "kde";
} else if (de.contains("unity")) {
return "unity";
} else if (de.contains("gnome")) {
return "gnome";
} else if (de.contains("cinnamon")) {
return "cinnamon";
} else if (de.contains("mate")) {
return "mate";
} else if (de.contains("deepin")) {
return "deepin";
} else if (de.contains("budgie")) {
return "budgie";
} else if (de.contains("lxqt")) {
return "lxqt";
} else {
System.out.println("Not identifiable with: echo $GDM_SESSION: " + de);
}

return null;
}

public static String executeProcess(String s) {
ProcessBuilder pb = new ProcessBuilder("bash", "-c", s);
pb.redirectErrorStream(true);
Process p = null;
try {
p = pb.start();
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

StringBuilder res = new StringBuilder();
String line;

try {
while ((line = reader.readLine()) != null) {
res.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}

return res.toString();
}

This piece of code does need further testing as for now it has been tested only on manjaro XFCE and manjaro KDE, I'm not even sure about some of the names (deepin's XDG_CURRENT_DESKTOP contains "deepin" or "dde"?). However I think it's a good solution (or a backbone for one) so I'm posting this anyway.

Sources:

  • XDG_CURRENT_DESKTOP and GDM_SESSION
  • KDE
  • GNOME
  • Unity
  • Mint

SLES - Gnome - Change desktop background using a script

You should really specify which SLES version you're using.
Try with gconftool-2 in a terminal instead of gsettings.

Like this:
gconftool-2 -t str --set /desktop/gnome/background/primary_color #fff8e7



Related Topics



Leave a reply



Submit