You are not logged in.
Pages: 1
I am trying to write a python script that will stop gnome-screensaver, run mplayer, then start gnome-screensaver again. The idea is that this script is called "mplayer" and comes earlier in my PATH than /usr/bin/mplayer so I just type the mplayer command as usual. Anyway, here is my first attempt at a python script....
#!/usr/bin/python
import dbus
import os
import sys
bus = dbus.Bus(dbus.Bus.TYPE_SESSION)
devobj = bus.get_object('org.gnome.ScreenSaver', '/org/gnome/ScreenSaver')
dev = dbus.Interface(devobj, "org.gnome.ScreenSaver")
cookie = dev.Inhibit('MPlayer', 'Watching video')
os.system("/usr/bin/mplayer TODO_insert_parameters_from_command_line")
dev.UnInhibit(cookie)
You might have spotted my problem in the region of the "TODO_insert_parameters_from_command_line". I know the parameters are stored in sys.argv but how do I join them together into one string?
Offline
if you want the arguments ala python parsing, you want optparse.
If you want to just pass them through, you want argv (which returns a list), which you can join to become a string. the first arg is the name of the file itself, so slice that off.
example
#!/usr/bin/python
import sys
print ' '.join(sys.argv[1:])
"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍
Offline
Thanks. I had just found join but didn't know the "sys.argv[1:]" syntax. I have now got
command = '/usr/bin/mplayer ' + ' '.join(sys.argv[1:])
os.system(command)
Edit: Crap that does not work...
mplayer -alang jp -slang en video/Ergo\ Proxy/01.mkv
then the escaping "\" in the directory name disappears.
However, I have found the "replace" command which should allow me to fix this.... I'll post back if I fail!
Last edited by Allan (2008-01-24 05:30:44)
Offline
Well, I finally got this to work!
file = len(sys.argv) - 1
command = '/usr/bin/mplayer ' + ' '.join(sys.argv[1:file]) + ' ' + sys.argv[file].replace(" ", "\ ")
os.system(command)
No more pesky gnome-screensaver in the middle of my anime.
Offline
you don't need to find the length manually, you can use negative indexing in slicing.
import sys
import os
import os.path
command = '/usr/bin/mplayer %s "%s"' % (' '.join(sys.argv[1:-1], os.path.normpath(sys.argv[-1:]))
os.system(command)
by quoting the path, you shouldn't have problems with spaces, and using python's sprintf for string building is a better way to go than string addition.
"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍
Offline
you don't need to find the length manually, you can use negative indexing in slicing.
Wow, python is cool! I am going to have to learn it properly now...
Anyway, just fixing one of the lines in case anyone else is following this. Well, it think it is fixed because it works for me!
command = '/usr/bin/mplayer %s "%s"' % (' '.join(sys.argv[1:-1]), os.path.normpath(sys.argv[-1]))
The slice "[-1:]" cause a crash for me.
Last edited by Allan (2008-01-24 07:12:22)
Offline
ahh. [-1:] returns a list, of one element (but a python list of strings, not a single string). so you would have had to join that list too.. which is not really needed.
Nice extrapolation to using just the -1 array index. Looks good.
"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍
Offline
Offline
PS Ergo Proxy is good
So is elfen lied ^^
My coding blog (or an attempt at it)
Archer start page (or an attempt at it)
Offline
No more pesky gnome-screensaver in the middle of my anime.
Erm, doesn't mplayer have an option for this? I thought there was some --disable-screensaver type flag. I know my screen doesn't blank via DPMS when mplayer is running
Offline
Allan wrote:No more pesky gnome-screensaver in the middle of my anime.
Erm, doesn't mplayer have an option for this? I thought there was some --disable-screensaver type flag. I know my screen doesn't blank via DPMS when mplayer is running
From man page:
-stop-xscreensaver (X11 only)
Turns off xscreensaver at startup and turns it on again on exit.
But I find reinventing the wheel for something like this especially helpful .. especially when trying to learn a new language.
Offline
That only stops xscreensaver not gnome-screensaver. There used to be a patch applied to stop gnome-screensaver but it was removed and I can no longer get it to work...
Turn out that the dbus stuff there doens't work for me anyway...
About 1/2 way through Ergo Proxy and it is good. Same with Elfen Lied. I think I will open an anime thread...
Offline
That only stops xscreensaver not gnome-screensaver. There used to be a patch applied to stop gnome-screensaver but it was removed and I can no longer get it to work...
Turn out that the dbus stuff there doens't work for me anyway...
About 1/2 way through Ergo Proxy and it is good. Same with Elfen Lied. I think I will open an anime thread...
The old-standby for this is to write something that moves the mouse to 0,0 and then moves it to like 0,5 then back every minute or so.
Offline
The old-standby for this is to write something that moves the mouse to 0,0 and then moves it to like 0,5 then back every minute or so.
Or change the dbus stuff to os.system("killall gnome-screensaver") & os.system("gnome-screensaver"). Simple but thuggish
Offline
phrakture wrote:The old-standby for this is to write something that moves the mouse to 0,0 and then moves it to like 0,5 then back every minute or so.
Or change the dbus stuff to os.system("killall gnome-screensaver") & os.system("gnome-screensaver"). Simple but thuggish
https://bugs.launchpad.net/ubuntu/+sour … +bug/53947
You might be able to set and unset the gconf key value specified there.
"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍
Offline
Pages: 1