Jump to content

I'm looking for a way to switch Virtual Desktops (or workspaces) and launch programs to specific Virtual Desktops via python script. Preferably it would be for LXDE (environment it will be used on is a raspberry pi). But if that's not possible, Gnome or KDE will be fine too.

Link to comment
https://linustechtips.com/topic/1451812-switch-virtual-desktops-via-python/
Share on other sites

Link to post
Share on other sites

2 hours ago, TechPsychosis said:

I'm looking for a way to switch Virtual Desktops (or workspaces) and launch programs to specific Virtual Desktops via python script. Preferably it would be for LXDE (environment it will be used on is a raspberry pi). But if that's not possible, Gnome or KDE will be fine too.

hmm, I don't really use Linux distros that much, so my understanding might be a bit lacking sorry.

 

I mean, it should be possible, but don't know how hard it would be in gnome/kde etc (again I lack knowledge, hopefully someone with knowledge will step in with terminal commands)

 

So I think some distros have

wmctrl -s [screen]
 

So in python you could use something like

import os

os.system('wmctrl -s 1')

I could be very wrong on this one.  I only have my Ubuntu server, which has no gui elements.

 

Similar thing to opening a program in it.  You can use os.system and call it after switching the desktop.

 

Although it might be handier having it as a bash script...I dont know...not too familiar with Linux.

3735928559 - Beware of the dead beef

Link to post
Share on other sites

8 hours ago, wanderingfool2 said:

hmm, I don't really use Linux distros that much, so my understanding might be a bit lacking sorry.

 

I mean, it should be possible, but don't know how hard it would be in gnome/kde etc (again I lack knowledge, hopefully someone with knowledge will step in with terminal commands)

 

So I think some distros have

wmctrl -s [screen]
 

So in python you could use something like

import os

os.system('wmctrl -s 1')

I could be very wrong on this one.  I only have my Ubuntu server, which has no gui elements.

 

Similar thing to opening a program in it.  You can use os.system and call it after switching the desktop.

 

Although it might be handier having it as a bash script...I dont know...not too familiar with Linux.

Yeah you would think think there would be a python api I could import for kde, gnome or lxde that would give me an easy way to dictate what goes on Virtual Desktops but I've searched and nothing seems to allow direct control. It would have to be an api from the desktop environment and not the OS since the OS and the DE are completely separate unlike in Windows.

 

So far one workable solution would be to launch Devilspie2 from the script to launch programs to the virtual desktops and then use python keyboard to use keyboard shortcuts to specific virtual desktops. Not a very elegant solution though so I'm hoping someone has a cleaner way, preferably purely from python.

Link to post
Share on other sites

2 hours ago, TechPsychosis said:

Yeah you would think think there would be a python api I could import for kde, gnome or lxde that would give me an easy way to dictate what goes on Virtual Desktops but I've searched and nothing seems to allow direct control. It would have to be an api from the desktop environment and not the OS since the OS and the DE are completely separate unlike in Windows.

 

So far one workable solution would be to launch Devilspie2 from the script to launch programs to the virtual desktops and then use python keyboard to use keyboard shortcuts to specific virtual desktops. Not a very elegant solution though so I'm hoping someone has a cleaner way, preferably purely from python.

your desktop enviorment is entirely a different program and hence running in a different process. Two process(your python script and your desktop) do not communicate with one another unless one has exposes its internal data or api through some sturctures like socket, shared memory, unix pipe, or physical files on disks. In this case, wmctrl is a terminal cli tool that can interface with your desktop, it seems to do this through an api that the x11 exposes(no idea how it is expose, pretty sure it is via an unix socket which is common way to do things on unix system). You can see its source code and see excatly how it does it here

 

https://github.com/dancor/wmctrl/blob/master/main.c

 

In my opinion, you should just have python run a terminal command like someone already mentioned above. Why would you want to reinvent the wheel?

 

Edit:

https://en.wikipedia.org/wiki/X_Window_System#Client–server_separation

it is using unix socket. My guess is right. 

Sudo make me a sandwich 

Link to post
Share on other sites

41 minutes ago, wasab said:

In my opinion, you should just have python run a terminal command like someone already mentioned above. Why would you want to reinvent the wheel?

 

Its not reinventing the wheel I just prefer to do everything from python instead of calling an external program, the wmctrl website http://tripie.sweb.cz/utils/wmctrl/ even references devilspie which is one of the solutions I posted above. If there is no other way, I can use a devilspie and pass on a lua script but I want to make sure there isn't a python api that I'm missing which would make the whole process a lot cleaner, especially since there will be multiple calls to the Virtual Desktop. For instance kde has Pytivity that allows control of activities via python, I'm just trying to verify if there is anything like that for Virtual Desktops.

Link to post
Share on other sites

1 hour ago, TechPsychosis said:

 

Its not reinventing the wheel I just prefer to do everything from python instead of calling an external program, the wmctrl website http://tripie.sweb.cz/utils/wmctrl/ even references devilspie which is one of the solutions I posted above. If there is no other way, I can use a devilspie and pass on a lua script but I want to make sure there isn't a python api that I'm missing which would make the whole process a lot cleaner, especially since there will be multiple calls to the Virtual Desktop. For instance kde has Pytivity that allows control of activities via python, I'm just trying to verify if there is anything like that for Virtual Desktops.

there will only be such a python api if there is someone who felt the need to write such an api and be generous enough to share it, upload it somewhere, and write good enough documentions for you to learn how to use it. Also it would need to be popular enough so that the web search engine will actually feature it in the first couple page instead of page one thousand six hundred seventy. 

 

The api for interfacing with the windows system does exist, in c. 

https://github.com/freedesktop/xorg-libXmu

 

it is part of the libXmu. This is ultimatly what wmctrl uses to communicate with the x11.

you need to create bindings in python if you want to use it.

 

https://realpython.com/python-bindings-overview/

 

Like i said, a convenient python package is not likely to exists and if you are going through libXmu, it is akin to reinventing your own wmctrl pretty much. 

 

deivilspie is also an external program. 

https://linux.die.net/man/1/devilspie

And i am not sure what you meant by a lot cleaner. You ever use a terminal? it is literally a program that keeps forking off proccesses to run other programs. 

 

Sudo make me a sandwich 

Link to post
Share on other sites

20 minutes ago, wasab said:

there will only be such a python api if there is someone who felt the need to write such an api and be generous enough to share it, upload it somewhere, and write good enough documentions for you to learn how to use it. Also it would need to be popular enough so that the web search engine will actually feature it in the first couple page instead of page one thousand six hundred seventy. 

 

The api for interfacing with the windows system does exist, in c. 

https://github.com/freedesktop/xorg-libXmu

 

it is part of the libXmu. This is ultimatly what wmctrl uses to communicate with the x11.

you need to create bindings in python if you want to use it.

 

https://realpython.com/python-bindings-overview/

 

Like i said, a convenient python package is not likely to exists and if you are going through libXmu, it is akin to reinventing your own wmctrl pretty much. 

 

deivilspie is also an external program. 

https://linux.die.net/man/1/devilspie

And i am not sure what you meant by a lot cleaner. You ever use a terminal? it is literally a program that keeps forking off proccesses to run other programs. 

 

I am aware it exists in C, I wanted to know if there was one for python. I am also aware Devilspie2 is an external program but it gives a you more options on how to launch programs in Virtual Desktops in a simpler way (LUA scripts) so if I was to use an external program I would lean towards devilspie2 rather than wmctrl.

 

By a cleaner way i meant a lack of dependency on an external program instead of something already baked into the python script. That way in the future I won't have to worry if that external program has been deprecated or modified in such a way I have to rewrite portions of the script. Again, I just wanted to know if such an api exists for python, I know I can use external programs to achieve what I want from the script but I would prefer not to.

 

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×