4
#!/usr/bin/env python
import os
bashCommand = "gsettings set org.gnome.desktop.background picture-uri file:///home/malon/autowallpaperchanger/" + pic_name
print bashCommand
os.system(bashCommand)

Print result:

gsettings set org.gnome.desktop.background picture-uri file:///home/malon/autowallpaperchanger/wallpaper-1252048.jpg

Copying and pasting the print result into a terminal makes the change successfully, so the command is correct, but os.system isn't processing the request correctly for some reason. In the full script (posted below), I use os.system for a different reason immediately before (wget) and that works fine.

Full script here.

5
  • Do you need to use os.system instead of subprocess.popen2 ? Aug 27, 2012 at 19:36
  • @aking1012 If I knew, I wouldn't be asking :P Also, I already am using os.system
    – malon
    Aug 27, 2012 at 19:37
  • You didn't understand what I asked. Do you have some reason for using os.system instead? or is popen okay? Aug 27, 2012 at 19:37
  • @aking1012 Oh, I understand your phrasing now. I'm not very familiar with popen, but i don't think there would be a reason why I couldn't. Upon reading about popen/subprocess, it seems overly complicated. I don't need to pass any variables or use any return values. Just to execute one static line of code. is subprocess the only way to accomplish this?
    – malon
    Aug 27, 2012 at 19:40
  • the same question on stackoverflow
    – jfs
    Sep 1, 2012 at 6:43

2 Answers 2

5

Drop all the os.system and subprocess stuff. Just use the Gio/gsettings API, like this:

from gi.repository import Gio

settings = Gio.Settings.new("org.gnome.desktop.background")
settings.set_string("picture-uri", "file:///path/to/image.png")
8
  • nice. i wasn't thinking about other ways to do it. i just went with os.whatever has been deprecated for a while in favor of subprocess. i get a brain-lock thing when i see things implemented one way and just try to get that way working. Aug 27, 2012 at 21:15
  • @Timo This is excellent, advice I haven't seen before. Here is my updated (but still not working) script: pastebin.com/19QtFLZq
    – malon
    Aug 27, 2012 at 21:44
  • Do a check that your file actually exists: print "Does my new image exists?", os.path.exists("/home/tyler/autowallpaperchanger/" + pic_name)
    – Timo
    Aug 27, 2012 at 21:48
  • @Timo This is what prints: Does my new image exists? True. Also, I'm not sure how to format these responses nicely... but here again is my most updated version of the script (including your image checker): pastebin.com/8EDDBEJ9
    – malon
    Aug 27, 2012 at 21:51
  • The script works perfectly fine here, my background gets changed to a random image. I just changed your image path with os.getcwd() for convenience.
    – Timo
    Aug 27, 2012 at 22:03
3

This works:

import shlex, subprocess, os, sys

def change_wp(fname, folder_stem='Pictures'):
    home = os.environ['HOME']
    uri = os.path.join(home, folder_stem)
    uri = os.path.join(uri, fname)
    set_wp_command = "gsettings set org.gnome.desktop.background picture-uri file://" + uri
    args = shlex.split(set_wp_command)
    try:
        my_proc = subprocess.Popen(args)
        my_proc.wait()
    except:
        sys.exit(1)

if __name__ == '__main__':
    change_wp('mywallpaper.png')
    print("Wallpaper changed...")

It anticipates mywallpaper.png existing in ~/Pictures.

2
  • bashCommand = "wget " + page_source[address_start:address_end] os.system(bashCommand) bashCommand = "gsettings set org.gnome.desktop.background picture-uri file:///home/tyler/autowallpaperchanger/" + pic_name print bashCommand args = shlex.split(bashCommand) my_proc = subprocess.Popen(args) my_proc.wait() I attempted to modify your code to no avail. I presume that is because there is some magic happening in os.path.join and os.environ ?
    – malon
    Aug 27, 2012 at 20:06
  • I gave you the working chunk for changing the wallpaper. All you have to do is hand it the right directory as folder_stem and the right file name as fname. Aug 27, 2012 at 20:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .