focusxterm

Can I run 'xterm -e MyApp', and pass focus to the new xterm window?


If I run xterm -e MyApp in a terminal session, a new xterm window pops up running MyApp. I have to click on the new window to get focus. When I close MyApp, the window closes, but focus does not return to the original window. I am working on a Mac, in a Terminal window.

I want the new window to take the focus from the old window, and hand it back when it closed.

The 'xterm' command generates the new window, and passes the -e argument to the shell in that window. The local 'xterm' task completes. I can keep it going with nohup, but that does not pass the focus. This is logical. To get the behaviour I expect, I have to write something that transfers the focus from the old terminal to the new one and back again.

My search turned up very little. I suspect I am asking a dumb question. I therefore ask...

Is there a neat-ish way to pass focus to and fro on Mac, Linux, and maybe Windows? I welcome a categoric answer "No, it's complicated, and different in every case". Then I could give up with honour, knowing I tried my best.

A follow-on question might be "If no other app passes focus like this, why does it feel it should?". Probably because the new xterm window appears in front of the old terminal window. I tried xterm -iconic hoping that clicking on the icon might get keyboard focus, but it does not'

Background:

This is is a debug tool. I run it on several machines. My emphasis on simple, and foolproof. I can click on the new window, even if it bugs me. If I forget, it fails in an obvious way. I am sure the ingenious folk on this site can provide cunning solutions, but I don't want you to waste your time for anything more than a simple shell command, if there is one.

My current machine is an Intel Mac, and I type in a Terminal window. I have the following alias in my .zshrc...

alias AnsiView="xterm -e myAnsiView"

If I type the command AnsiView image.png it opens an 'view' window running my application, which displays the image using ANSI codes.

I know a new window starts without focus. I use the ANSI \e[?1004h escape to detect when it gets focus for the first time. The app running in a new window start with the a 'click here for focus' prompt, and goes to the usual command prompt when it gets focus. It does not seem to react when it loses focus. I expect the event is not handled until it gets focus back again.


Solution

  • It is not possible using the current XQuartz... https://github.com/XQuartz/XQuartz/issues/374

    I can get it to work once after starting XQuartz. My new xterm window pops up with the keyboard focus. But only once.

    I want my app to run on an unmodified Mac so that people can use my tool in the field, so replacing XQuartz with X11 is not a useful option.

    Another complication I found (a year later!) was that the Mac terminal opens any new window behind the terminal window if you have Secure Keyboard Entry enabled. Go to the terminal menu and disable that and applications can open in front of the terminal, and some of them have focus.

    My workaround was to run my app under xterm with a -f prompt which causes the app to start with a prompt saying 'click here for focus'.

    I can get focus if I restart XQuartz before calling the xterm script with...

    open -a XQuartz && xterm -e MyApp

    This works for me, and will do until the focus issue is fixed. But what if something else is using XQuartz?