Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm making a script that combines taking input from the command line with the launch of other programs. So the script will go like this:

...
launch program
Read input from the terminal
...

The problem is that when I launch the program (even if I launch it in the background), the terminal looses focus and the launched program becomes active.

How do I launch a program keeping the focus on the terminal with a default Ubuntu command?

Thank you

share|improve this question
    
Also you could try looking at this question. –  iBelieve Jan 8 '13 at 0:11
    
Interesting question, I don't think there is any way to guarantee that you won't lose focus. –  elias Jan 8 '13 at 18:59
add comment

1 Answer 1

If you know the title of the terminal, you can switch back to it after opening the program using the wmctrl command (it doesn't even look like the new program was in the foreground at all if the window opens instantly and you don't need the sleep command):

wmctrl -a <WIN-TITLE>

<WIN-TITLE> doesn't have to be the full title, for example, if the title was TESTING, TEST would work.

Depending on how long it takes to open the program, you might need to use the sleep command to delay the switch back to the terminal. You can use it like this: sleep <SECONDS>s.

To set the title of the terminal from within a script, use:

NAME="NEW_TITLE"; echo -en "\033]0;$NAME\a"

See this answer for more information.

For example, here is a little script to open a program in the background:

NAME="Opening program..."; echo -en "\033]0;$NAME\a"
$1&
sleep 0.75s
wmctrl -a "Opening program"
share|improve this answer
    
I already saw that option but I was searching for a default ubuntu command, so there was no need for installing a new program –  Vndtta Jan 8 '13 at 0:47
    
Sorry, it was already installed on my system so I didn't realize it wasn't installed by default. –  iBelieve Jan 8 '13 at 1:53
    
It's OK, if no one gives a better answer in a week I'll mark this one as good. Thanks for the answer by the way –  Vndtta Jan 8 '13 at 8:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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