-1

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.

Richard Kirk
  • 281
  • 1
  • 12
  • xdotool can put the focus on a given window (but that's going off-topic) – Thomas Dickey Aug 27 '23 at 21:17
  • No, this is good. I saw references to xdotool. It is not installed on my Mac, and it is not on the Linux machines at work, so that struck me as a step too far. I also taught myself AppleScript over the weekend. This could also do the job, but it was not simple. – Richard Kirk Aug 28 '23 at 07:53

1 Answers1

0

No, I can't.

xterm -iconic is probably the best solution. You have to click on the icon, and that passes the focus. If the window is open, your first click on it is probably consumed getting the focus, so being able to click on a feature in the open window is misleading too.

It is hard to prove a negative. There could be a great solution out there. But, having looked, it seems unlikely. There certainly is no xterm option to do what I want.

This makes sense. The window manager should be in charge of what is on top. There are system options for setting whether focus follows the mouse, or whether the window with focus is on top. If I transfer focus to the new window, I will have to take account of all these settings, because I may not have permission to do so. It is best not to try. Other applications obey the OS House rules.

It still feels wrong. I open an 'xterm' from a terminal, and it opens a window that may lie in front of the window I was using. It is easy to assume it has focus. If you iconise the new window, rather than leave it open, then you have to click on it to see it.

PS: I have found the xterm -iconic solution does not work on my current Mac (Intel Powerbook, Ventura 13.4). It ionises the window to a blank icon, but it does not get focus when you open it.

Richard Kirk
  • 281
  • 1
  • 12