Sunday, May 5, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 583  / 2 Years ago, fri, october 21, 2022, 11:35:57

I was wondering if it is possible to create a keyboard shortcut to run application if it is not yet running in Ubuntu?



This is a valid example for something like a browser which is normally heavily used, and you wouldn't really want to run another instance of it if there is one running already (rather just activate the window).


More From » 10.04

 Answers
7

  1. Save the following script to your home folder as launch_chrome.py:



    #!/usr/bin/env python

    import subprocess
    import wnck
    import gtk
    import time


    screen = wnck.screen_get_default()
    while gtk.events_pending():
    gtk.main_iteration()

    window_list = screen.get_windows_stacked()

    last_active = None
    for window in window_list:
    if window.get_application().get_name() == "Google Chrome":
    last_active = window

    if last_active:

    timestamp = int(time.time())
    last_active.activate(timestamp)

    else:

    subprocess.Popen("google-chrome")

  2. Make the script executable:



    chmod +x ~/launch_chrome.py

  3. Use the command /home/<YOURUSERNAME>/launch_chrome.py when adding a shortcut.




When this shortcut is activated, it will either:




  1. Launch a new Chrome window if there aren't any already running or,

  2. bring the most recently used Chrome window into focus.


[#44517] Sunday, October 23, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ameatoes

Total Points: 321
Total Questions: 106
Total Answers: 112

Location: Belarus
Member since Sat, Jul 18, 2020
4 Years ago
ameatoes questions
Tue, Aug 16, 22, 22:50, 2 Years ago
Fri, May 14, 21, 03:36, 3 Years ago
Sat, Oct 8, 22, 01:00, 2 Years ago
Fri, Feb 17, 23, 14:44, 1 Year ago
;