Get PID (unix id) of application when launching it?

I have an AppleScript that launches an application this way

do shell script "open - a " & quoted form of variableAppName & space & quoted form of variableParameter 

Is it possible to get the PID (unix id) of the application when launching it, so that I can write the PID into a plist for other applications to read and test whether the application is running?

I cannot use the bundle identifier of the application to test this, because I can have multiple instances of the application running, and I need to test whether a specific instance is running or not.

Thanks for any help with this.

set variableAppName to POSIX path of "SSD 500:Applications:iPlay '" # Edit to fit your needs
set theProcessName to "Pages" # Edit to fit your needs

do shell script "open -a " & quoted form of variableAppName

tell application "System Events"
	set iitsPID to unix id of every process whose name is theProcessName
	--> {4349} # but it was {4230} before I quit it and run this script
end tell

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) vendredi 17 mars 2017 19:35:15

or this Single liner

set myPID to do shell script "open -a " & quoted form of variableAppName & space & quoted form of variableParameter & " & echo $!"

set myPID to myPID + 1

For some reason it is returning a PID one less than the actual PID.
Probably because the pid is the PID for the “open” command and the actual program gets the next sequential PID.
So you have to add 1

That’s something you have no practical way of knowing. It might be n + 1, but it might not.

Thank you for these replies. The script that I’m working on runs the SheepShaver emulator (which runs OS 8-9), and when I use these methods, the PID that is returned is the same one reported by Activity Monitor - I don’t get the mismatch. Maybe different applications produce different results?

The open command line util uses an AppleEvent («event GURLGURL») to open and the returned pid is the pid of open. What happens between starting the open command and the launch of your applications we have no control of. The system could be starting up an agent for instance and the pid you want is n + 2.

When you let the script launch the application you can be sure the pid is correct.

set pathToApplicationExecutable to "/Applications/"
do shell script quoted form of pathToApplicationExecutable & " > /dev/null 2> /dev/null & 
echo $!"