Choose style:

Author Topic: Start an application on a specific workspace (with Python 3)  (Read 83 times)

0 Members and 1 Guest are viewing this topic.

Offline Fritz74

  • Jr. Member
  • **
  • Posts: 98
  • Karma: 10
  • m, 45, Austria
    • View Profile
  • Peppermint version(s): 10
Start an application on a specific workspace (with Python 3)
« on: September 11, 2019, 04:17:11 pm »
I want my terminal to start on workspace 3. This can be achieved with the app wmctrl - which is complicated.
There is an easier way with Python. I am using Python 3.

First install pip, the Python package manager:
Code: [Select]
sudo apt install python3-pip

Then use pip to install pyautogui. This is a library that lets Python perform mouseclicks, keystrokes etc.
Code: [Select]
sudo pip install pyautogui

I want Python to click on the workspace switcher in my Xfce panel. To get the coordinates, I make a screenshot and open Gimp. If I move the cursor over the image, Gimp shows me the coordinates. On my desktop, a click on 1569/1056 will open workspace 3.

The script that clicks on workspace 3 gets the name clickonworkspace3.py:
Code: [Select]
#!/usr/bin/env python3
import pyautogui
pyautogui.click(1569,1056)

This can be used in shell scripts, whenever I want them to click on workspace 3.
The script gets the name sakuraonworkspace3.sh:
Code: [Select]
python3 clickonworkspace3.py
sakura

Make them executable with sudo chmod +x clickonworkspace3.py and sudo chmod +x sakuraonworkspace3.sh

Running the script sakuraonworkspace3.sh now opens the terminal Sakura on workspace 3.

Finally, put a starter that opens the script sakuraonworkspace3.sh into the Xfce panel.


« Last Edit: September 22, 2019, 11:10:51 am by Fritz74 »

Offline PCNetSpec

  • Administrator
  • Hero
  • *****
  • Posts: 25497
  • Karma: 2807
  • "-rw-rw-rw-" .. The Number Of The Beast
    • View Profile
    • PCNetSpec
  • Peppermint version(s): Peppermint 8R, 9, and 9R
Re: Start an application on a specific workspace (with Python 3)
« Reply #1 on: September 11, 2019, 04:42:01 pm »
Nice, thanks for that .. the pyautogui module is new to me, gonna need to look into this :)
https://pyautogui.readthedocs.io/en/latest/
« Last Edit: September 11, 2019, 04:44:11 pm by PCNetSpec »
WARNING: You are logged into reality as 'root' .. logging in as 'insane' is the only safe option.

Team Peppermint
PCNetSpec

Offline Fritz74

  • Jr. Member
  • **
  • Posts: 98
  • Karma: 10
  • m, 45, Austria
    • View Profile
  • Peppermint version(s): 10
Re: Start an application on a specific workspace (with Python 3)
« Reply #2 on: September 13, 2019, 08:59:05 am »
It is magic. ;) Some people use it for tasks that require doing the same thing on a desktop over and over again (for example uploading a lot of stuff).
« Last Edit: September 13, 2019, 09:38:36 am by Fritz74 »