1

I'm using NSTask to launch an external app. But at this time, my app looses the focus. I don't want this. The other app just writes some infos in a file and my app reads and displays those infos. Is it possible, to start the NSTask app in the background?

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
Lupurus
  • 3,618
  • 2
  • 29
  • 59

3 Answers3

0

If you start the app using the open command (manpage), you could try using the -g option to avoid bringing the app into the foreground:

-g  Do not bring the application to the foreground.

So rather than making NSTask start the app directly; use open to, err, open it.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • Hm, in my case, this doesn't work, because the external app opens a second window and from this point, my app looses the focus again. – Lupurus Feb 13 '13 at 15:20
  • @Lupurus So by using `open -g` the initial external app window is in the background but some time later it opens a new window which takes the focus? – trojanfoe Feb 13 '13 at 15:24
  • Hm I don't know, where exactly the problem is. Maybe a better explanation: The external app is an app I created with Wineskin. It uses XQuartz to display the windows. The .exe I start in this way creates a minimized window which is also minimized at the dock. I tried to call open -g ... from the terminal, but it just opens a new terminal window, that the wineskin app and than the terminal looses the focus – Lupurus Feb 13 '13 at 15:32
  • 1
    @Lupurus Sounds like a losing battle; I don't think it's possible without more control over the external app. If you wrote it then perhaps chop the bits out that you need and put them into a library that can be linked with both apps? – trojanfoe Feb 13 '13 at 15:33
  • Ok... maybe I can find something in the Wineskin code. Thank you! – Lupurus Feb 13 '13 at 15:49
  • I got it. I found something like "bringToFront" in the Wineskin sourcecode. I commented this out and now it works. Thanks! – Lupurus Feb 13 '13 at 15:57
0

I think instead of using NSTask, you might want to use NSWorkspace's launchApplicationAtURL:options:configuration:error:. If you pass NSWorkspaceLaunchWithoutAddingToRecents | NSWorkspaceLaunchWithoutActivation | NSWorkspaceLaunchAndHide as the options: argument, it should be effectively invisible. (I obviously haven't tried it with your particular case, but that's how it's intended to work.)

Chuck
  • 234,037
  • 30
  • 302
  • 389
0

Why not just call "performSelector:withObject:afterDelay" after you start your NSTask? Using this you can, after an appropriate delay, call a selector which reactivates your application. You don't need anything fancy here, just let the other app come to the front and then reactivate your own app after that.

This will work. Just set your delay to whatever is needed.

[[NSRunningApplication currentApplication] performSelector:@selector(activateWithOptions:) withObject:[NSNumber numberWithUnsignedInteger:NSApplicationActivateIgnoringOtherApps] afterDelay:5.0];
regulus6633
  • 18,848
  • 5
  • 41
  • 49
  • Thanks. I had a similar solution before, but this isn't very useful: If I switch to another running app (maybe Safari, Mail etc.), this code would also activate my app... – Lupurus Feb 14 '13 at 19:47