views:

35

answers:

1

How do I bring another app's window to front on Mac in C/C++? I have the app's pid. I found solutions for AppleScript, but I'm not familiar with AppleScript. (and also not with Objective C) Thanks!

edit: I cannot use Carbon in this program, because it must be a 64 bit binary. edit2: I'm also interested in Carbon and Cocoa solutions. I could put this into an exernal 32 bit app to use Carbon.

+1  A: 

You can do it like this:

#include <stdlib.h>

system("osascript -e \"tell application \\\"Address Book\\\" to activate\"");

This will also launch the app (Address Book in this example) if it's not already running.

Paul R
Thanks! The problem is it doesn't seem to bring it to the front. It launches it if it wasn't running, but it's still behind my window. (well, Photoshop's window, I'm writing a plugin. I don't think Photoshop's window is topmost)
Cornelius Scarabeus
@Cornelius: that sounds like a quirk of Photoshop then - I tested this from Terminal and it behaves as expected - you don't have some kind of modal window or dialog showing do you ?
Paul R
It seems to work with the Address book. I don't know how this other app is displaying its window, it might be a modal dialog.
Cornelius Scarabeus
So, I had the bad luck of having to deal with an app that was unresponsive to scripting. And launching it with execve() opened it behind the Photoshop window. The solution was to call an AppleScript (the same way as above) - tell application "Finder" to open application...Anyway, thanks for the help! It's the right answer, and it got me on the right track to solving my somewhat special case.
Cornelius Scarabeus