Open file by application process ID (PID)

I am using shell commands in an Applescript to open multiple instances of an application.


How can I force a particular instance to open a file AFTER the instance is open?


I.e., the following is NOT what I am after, as this opens the file at the same time the instance is created:


do shell script "open -na preview.app " & quoted form of POSIX path of "myfile.pdf"


----------


If I just use an open command, the same instance always opens the file, regardless of which instance is frontmost. I've also used applescript to control the frontmost application's menus (File > Open), but that is slow and clumsy.


The only way I can think of is to use the process IDs of the instances. I've got the list of PIDs with this:


set the_pids to (do shell script "ps ax | grep " & "Preview" & " | grep -v grep | awk '{print $1}'")


but how can I now use a PID to open a file?

Posted on Mar 10, 2012 4:46 PM

Reply
2 replies

Mar 10, 2012 7:38 PM in response to Joe7Pack

If you know the PID you can do this with applescript, I think:


tell application "System Events"

set proc to item 1 of (processes whose unix id is pid)

tell proc


opentheFile

end tell

end tell


But I need to point out that you are working against the way the OS is designed to work, and you are going to run into problems. The system expects gui apps to have one instance with multiple windows and documents: at the very least it's going to be confused over which instance owns the preference fils at any given moment, and there's no telling what unanticipated consequences will result from focing the system into a configuration none of the developers planned for.


There's no good reason to do this with a gui app (and if you think there is, you're mistaken). I suggest you rethink your approach.

Mar 10, 2012 7:47 PM in response to Joe7Pack

tw beat me to most of this, although I have a slightly shorter version. Given thePID (that you've previously determined), you can:


tell application "System Events"

tell application processidthePID to opentheFile

end tell


But you should be aware that most GUI apps aren't designed to work this way, at least under Mac OS X (other OSes (e.g. Windows) may do that, but not Mac OS X.


At the very least the sucess of this approach will be application-dependent... if the application understands and expects to be instantiated then you may be OK. You might have just picked a bad example, though - if you follow your example using Preview.app you will find you have multiple Preview.app processes running, but all the images appear in the same GUI instance, regardless of which PID you tell to open the image.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Open file by application process ID (PID)

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.