Thursday, March 30, 2017

#1 2017-03-17 12:31:36 pm

emendelson
Member
Registered: 2008-10-31
Posts: 348

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

I have an AppleScript that launches an application this way

Applescript:

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.

Last edited by emendelson (2017-03-17 12:32:52 pm)


Filed under: pid, unix id

Offline

 

#2 2017-03-17 01:35:19 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 2885

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

Applescript:

set variableAppName to POSIX path of "SSD 500:Applications:iPlay '13:Pages.app:" # 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

Last edited by Yvan Koenig (2017-03-17 01:43:23 pm)

Offline

 

#3 2017-03-17 04:47:00 pm

robertfern
Member
Registered: 2011-11-29
Posts: 25

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

or this Single liner

Applescript:

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

Last edited by robertfern (2017-03-17 04:56:56 pm)

Offline

 

#4 2017-03-17 07:47:15 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 4967

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

robertfern wrote:

the next sequential PID

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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#5 2017-03-17 10:31:43 pm

emendelson
Member
Registered: 2008-10-31
Posts: 348

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

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?

Offline

 

#6 2017-03-18 05:17:14 am

DJ Bazzie Wazzie
Member
From: the Netherlands
Registered: 2004-10-20
Posts: 2594

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

emendelson wrote:

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.

Applescript:

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

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.037 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)