I want to run a file with the specified application, and I want the running program to immediately become the front-most window.
I know I can do it like this:
[[NSWorkspace sharedWorkspace] openFile:fileName withApplication:appName];
Then, if I can get the PID of this running application, I can do this to make this application the very latest:
NSRunningApplication* app = [NSRunningApplication runningApplicationWithProcessIdentifier: PID]; [app activateWithOptions: NSApplicationActivateAllWindows];
The question I have is this: what is the easiest, fastest and most reliable way to get this PID application right after launch, so I can make sure that this application is at the very beginning?
It is not as simple as it might seem at first glance. For example, I need a process name to get the PID using Carbon calls or through the application dictionary, accessible through NSRunningApplication . However, in the general case, I do not always know what the process name is, and in some cases the process name is an empty string.
In addition, I may have other instances of the same application that are already running, and I want to always get the PID of the specific instance of the application that I just launched.
Can anyone suggest the ultimate, 100 percent reliable way to get the current PID of the application?
Or, alternatively, is there a way to run this file with the specified application so that the application always opens as the closest application?