10

When a GUI process is launched from the OS X terminal, the window shows up in the background, and you have to use command-tab to give it focus.

Is there a way to make the terminal automatically give such GUIs focus after they are launched? For example (assuming gitk is installed):

% gitk

should launch the GUI and then switch to it.

Note: For several reasons, using open as this answer suggests is not a general solution.

Update: To better explain why the open method isn't satisfactory, here's a sample bash session (with witty commentary).

% cd /my_repo
% gitk

Waiting for the GUI to appear ... any day now ... oh wait -- it's already open. I just didn't notice because it opened a window BEHIND my terminal. I wonder how long I was going to sit here waiting....

% open gitk
The file /my_repo/gitk does not exist.

Ah, of course.

% which gitk
/usr/bin/gitk
% open /usr/bin/gitk

What the ... it opened a new terminal window to run gitk, and it did so in my home directory, not /my_repo, so gitk complains that the current directory isn't actually a repository...

Community
  • 1
  • 1
Stuart Berg
  • 17,026
  • 12
  • 67
  • 99
  • It might help if you explained more about why you need to invoke a windowed application synchronously from the command line (instead of opening it asynchronously with `open`). – Chris Page Jun 29 '12 at 12:28
  • Not all applications can be opened via `open` (gitk, for example). Additionally, the ones that can be opened that way will be opened via a separate terminal, which may be inconvenient. – Stuart Berg Jun 29 '12 at 17:00
  • 'Not all applications can be opened via open' Why not? I can't think of a reason why it would be true--normal/windowed applications by definition should be something you can open from Finder or the Dock. 'will be opened via a separate terminal' I don't understand what you mean by 'separate terminal'. Do you mean that, even if you have a terminal open, you can't use `open ...` from that terminal? If so, why not? – Chris Page Jun 29 '12 at 20:35
  • Sorry for being vague. I just updated the question. Hopefully it explains why `open` doesn't quite cut it... – Stuart Berg Jun 29 '12 at 22:06
  • gitk is doing something strange, acting partly like a GUI app and partly like a command line tool (paying attention to its current working directory). I think ideally it should take responsibility for bringing itself to the front. – JWWalker Jun 29 '12 at 22:39
  • Thanks, both of you, I get it. It is odd. It should probably activate itself depending on the command-line parameters. If you've given it enough information to perform a complete operation, it should stay in the background, but if you open it for interaction, it should activate. (In general, application should not activate themselves without the user requesting it.) – Chris Page Jul 02 '12 at 21:58

3 Answers3

4

Do you need to invoke it synchronously? If not, you could start it asynchronously with & and then activate it with osascript -e 'tell application "gitk" to activate'.

Chris Page
  • 18,263
  • 4
  • 39
  • 47
  • This worked until macOS Big Sur came along and seems to have ruined it. If I still run the osascript command separately myself from a terminal, it works. But as part of a function where I am invoking the actual tool using `&` and then doing the activate after a delay of `0.5`, it doesn't work anymore. Am I missing something? – shikhanshu Mar 01 '21 at 21:49
2

If you are dealing with gitk specifically you can edit the gitk file to achieve this, I posted an answer on the apple stack exchange: https://apple.stackexchange.com/a/74917/35956

You can find the gitk file using the following command from the terminal

which gitk

In my gitk file I found a line that looks like the following near the top (line 3)

exec wish "$0" -- "$@"

I changed it to this

exec wish "$0" -- "$@" & exec osascript -e "tell application \"Wish\" to activate"

When I execute gitk from the command line the gitk window comes to the foreground, another side effect of this is that it executes asynchronously

Community
  • 1
  • 1
1

You can wrap up @chris page's answer in a bash function and drop it in your .bashrc

function gitk() {
  command gitk "$@"&
  command osascript -e "delay .5" -e "tell application \"wish\" to activate"
}

There should be a way to get rid of the delay by looping and looking for 'wish' with a timeout.

NOTE: 'Wish' is the window title that shows up on my Mac for gitk.

Robert Wahler
  • 12,350
  • 3
  • 19
  • 11