1

I am running Ubuntu 18.04 with two monitors.

I use the Atom IDE with the atom-python-run package to run the command

 python3 {filename} 

at the push of a button. This opens a new terminal window. However the window does not have focus and is buried under all other windows, forcing me to alt-tab to it.

How do I force the newly opened terminal to have focus?

I tried using dconf-editor to set the focus-new-windows setting in org.gnome.desktop.wm.preferences to strict, but it did not solve the issue.

1

In general, you need to install an extension to eliminate the behavior in gnome-shell, where gnome displays a "Window is ready" notification rather than giving the new window focus. The extension "Noannoyance" appears to be the best maintained, but alternatively "Steal My Focus" or "Focus my window" probably still work fine.

0

Updating the Atom package autocomplete-clang instantly fixed the problem. I have no words.

0

I don't have an access to Gnome extensions somehow, so I wrote a shell script which checks every tenth of second for a new window and gives it focus. It works well for me, no visible performance impact, unlike Cinnamon after some minutes of use.

Prerequisite: wmctrl

Startup script: sh -c "sleep 1; focus &"

File /usr/local/bin/focus:

#!/bin/bash

echoTest()
{
    local test="$1"
    
    eval 'if [ '"$test"' ]; then echo true; else echo false; fi'
}

isMatch()
{
  local str="$1"
  local regex="$2"
  
  local numMatches=`echo "$str" | grep -E "$regex" | wc -l`
  
  echoTest "$numMatches -ge 1"
}


countOccurences()
{
    local a="$1"
    local b="$2"
    
    local all=`
        echo "$a"
        echo "$b"
    `
    echo "$all" | sort | uniq -c
}

getLinesWithOccurences()
{
    local a="$1"
    local b="$2"
    local occurences=$3
    
    local counted=`
        countOccurences "$a" "$b"
    `
    echo "$counted" | grep "^\s*$occurences" | sed "s|^\s*$occurences\s*||"
}

getUniqueLines()
{
    getLinesWithOccurences "$1" "$2" 1
}

getCommonLines()
{
    getLinesWithOccurences "$1" "$2" 2
}

getWindowIds()
{
    wmctrl -l | sed -r "s|^([^ ]*).*$|\1|"
}


old=`getWindowIds`

while [ true ]
do
    sleep 0.1
    
    new=`getWindowIds`
    unique=`
        getUniqueLines "$old" "$new"
    `
    added=`
        getCommonLines "$unique" "$new"
    `
    
    toActivate=`
        echo "$added" |
        head -n 1
    `
    
    if [ "$toActivate" != "" ]
    then
        name=`
            wmctrl -l | grep $toActivate | sed -r "s|^([^ ]*\s*){3}||"
        `
        if [ \
            `isMatch "$name" Panel` == false -a \
            `isMatch "$name" Desktop` == false -a \
            `isMatch "$name" "System Monitor"` == false \
        ]
        then
            echo $name
            wmctrl -a $toActivate -i
        fi
    fi
    
    old="$new"
done

Note: I did not write functions echoTest and isMatch specially for this script, but they are a part of my framework and I use them often. The script on my computer just includes this framework source code, but for this purpose, I have extracted only the functions of it which this script uses, so that you can more easily verify its safety. The exclusion of these three window names from focusing fixes unwanted behavior when these windows were incorrectly given focus after closing a window instead of returning focus to the previously focused window.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.