Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic background changer using Python 2.7.3 not working, though it should

I'm very new to Ubuntu/Python/Bash/Gnome in general, so I still feel like there's a chance I'm doing something wrong, but it's been 3 days now without success...

Here's what the script is supposed to do:
* [✓] Download 1 random image from wallbase.cc
* [✓] Save it to the same directory that the script is running from
* [x] Set it as the wallpaper

There are two attempts made to set the wallpaper two using different commands and NEITHER work when in the script. There is a print statement (2nd line from the bottom) that spits out the correct terminal command because I can C&P the print result and it works fine, it just doesn't work when it's executed in the script.

#!/usr/bin/env python
import urllib2
import os
from gi.repository import Gio

response = urllib2.urlopen("http://wallbase.cc/random/12/eqeq/1366x768/0.000/100/32")
page_source = response.read()
thlink_pos = page_source.find("ico-X")
address_start = (page_source.find("href=\"", thlink_pos) + 6)
address_end = page_source.find("\"", address_start + 1)

response = urllib2.urlopen(page_source[address_start:address_end])
page_source = response.read()

bigwall_pos = page_source.find("bigwall")
address_start = (page_source.find("src=\"", bigwall_pos) + 5)
address_end = page_source.find("\"", address_start + 1)

address = page_source[address_start:address_end]

slash_pos = address.rfind("/") + 1

pic_name = address[slash_pos:]

bashCommand = "wget " + page_source[address_start:address_end]
os.system(bashCommand)

print "Does my new image exists?", os.path.exists(os.getcwd() + "/" + pic_name)

#attempt 1
settings = Gio.Settings.new("org.gnome.desktop.background")
settings.set_string("picture-uri", "file://" + os.getcwd() + "/" + pic_name)
settings.apply()

#attempt 2
bashCommand = "gsettings set org.gnome.desktop.background picture-uri file://" + os.getcwd() + "/" + pic_name
print bashCommand
os.system(bashCommand)
settings.apply()
like image 416
MALON Avatar asked Aug 21 '26 04:08

MALON


1 Answers

You've successfully changed your settings, but they're still left unapplied, try next:

settings.apply()

after setting "picture-uri" string.

like image 165
Rostyslav Dzinko Avatar answered Aug 23 '26 23:08

Rostyslav Dzinko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!