Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 13, 2020 10:45 pm GMT

How I open and resize all my applications with just a keyboard shortcut

Every morning I have to open, resize and and reposition all applications I need to work, so I thought: What if I only had to press a keyboard shortcut that could do that for me...

To make this happen I would need to write a script that will be executed by that shortcut, this script would have to do three tasks: Open applications, wait for it to start and resize and reposition it.

Before continuing, this was developed and tested on Pop!_OS 20.04 LTS, a Ubuntu-based Linux distro. The source code is here.

This script was written in Python 3.8, I called it desktop-bootstrap.py, to open the applications I used the subprocess module. And to resize and reposition I used the command line tool wmctrl. The script accepts arguments, so you can open one application at a time, but it also support a configuration file to open a set of applications, I'll cover in this article the latter approach.

In each line of the configuration file you write the application you want to open, the command to open it, size an position, like this:

<command> <Application name> <X Axios> <Y Axios> <Window width> <Window height>

An example of configuration file would be:

insomnia insomnia 960 0 960 1080google-chrome google 0 0 960 1080 https://dev.to/ricardo93borges https://github.com/ricardo93borges

Note that most browsers accepts as arguments the websites to open when it starts, each website open in a different tab, so we'll take advantage of that. You can name the configuration file as config-sample.txt.

Now we need a shell script that will be executed when we use the keyboard shortcut, this shell script will call the python script passing as argument the location of the configuration file. I want to open a terminal too, so I also included the command to do so in the shell script:

# desktop-bootstrap.sh#!/bin/shgnome-terminal --geometry 170x25+1920+890 --working-directory=/home/usr/bin/python3.8 /path/to/desktop-bootstrap.py --file /path/to/config-sample.txt

Here is the python script with comments to explain some pieces of the code:

# desktop-bootstrap.pyimport subprocessimport timeimport sys"""I limited the retries, so the script don't run forever waiting for a application to start """max_retries = 20"""This method get an application ID looking for its name in a list obtained by a wmctrl command, that ID is used in the resize command"""def get_id_by_name(applications_running, name):    for app in applications_running:        if app.lower().find(name) > -1:            return app.split(' ')[0]"""This method receives a list of applications and for each one check if is running. If is running resize and reposition it, if not check again in the next iteration"""def resize(applications):    retry = 0    while len(applications) > 0:# Get a list of running applications        applications_running = subprocess.check_output(["wmctrl", "-lp"]).decode("utf-8").split("\n")        for index, app in enumerate(applications):            application_id = get_id_by_name(applications_running, app['name'])            if application_id:# Resize and reposition                dimensions = "-e " f"0,{app['x']},{app['y']},{app['width']},{app['height']}"                command = ["wmctrl", "-i", "-r", application_id, dimensions]                subprocess.run(command)                applications.pop(index)        retry += 1        if retry > max_retries:            break        time.sleep(5)"""This method open the application and send it to resize() method to resize and reposition it accordingly to the arguments passed"""def handle_args():    command = [sys.argv[1]]"""If the application is a browser it may have more arguments (the websites to open when it starts)"""    for i in range(len(sys.argv) - 7):        command.append(sys.argv[7 + i])    output = subprocess.Popen(command)    applications = [{        'name': sys.argv[2],        'pid': output.pid,        'x': sys.argv[3],        'y': sys.argv[4],        'width': sys.argv[5],        'height': sys.argv[6]        }]    resize(applications)"""This method handle the configuration file, open the applications and send it to resize() method to resize and reposition it."""def handle_file(file):    applications = []    with open(file) as f:        for index, line in enumerate(f):            props = line.split(' ')            command = [props[0]]"""If the application is a browser it may have more arguments (the websites to open when it starts)"""            for i in range(len(props) - 6):                command.append(props[6+i])            output = subprocess.Popen(command)            applications.append({                'name': props[1],                'pid': output.pid,                'x': props[2],                'y': props[3],                'width': props[4],                'height': props[5]            })    resize(applications)"""Main method, check the arguments and call the appropriated method to process the applications."""def run():    if len(sys.argv) > 2:        if sys.argv[1] == "--file":            handle_file(sys.argv[2])        else:            handle_args()    else:        print("\nInvalid number of arguments")        print("\nUse the following arguments: ")        print("\n<command> <Application name> <X Axios> <Y Axios> <Window width> <Window height>")        print("\nOr: ")        print("\n--file <path/to/configuration/file>")    sys.exit(0)run()

Finally, we'll create the keyboard shortcut, this may differ from one Linux distribution to another, in Pop_OS! 20.04 you open settings menu go to keyboard shortcuts, then scroll to down to the bottom of the list of shortcuts and click on the + button. In the window that launched you set a name for the shortcut, in the command input you type the path to that shell script (desktop-bootstrap.sh), for example: /home/ricardo/workspace/desktop-bootstrap/desktop-bootstrap.sh. In shortcut you choose the combinations of keys that will execute the shell script.

And that's it, you can find more details on this repository.


Original Link: https://dev.to/ricardo93borges/how-i-open-and-resize-all-my-applications-with-just-a-keyboard-shortcut-3469

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To