Tuesday, November 19, 2019

#1 2019-01-25 12:18:44 am

Neophyte
Member
From:: Canberra, ACT, Australia
Registered: 2017-08-20
Posts: 52
Website

How to get pid of current script process

I have a script which is started with:

do shell script "osascript" & script_file

I need the script to know the pid of its own osascript process.  That is because it needs to pass its own pid on to another process later.  There can be a variable number of processes called osascript running at the same time so, I need a way to be specific.

I had thought of this:

Applescript:

tell application "System Events"
   set my_pid to unix id of my process
end tell
set result to this_pid

But, that failed with

error "Can’t get process." number -1728 from «class prcs»

Is there another way for a running process to get its own pid (when the name is ambiguous) ?  Would using ps to find the most recent instance of osascript be a way ?

Thanks.


Public servants are people too

Offline

 

#2 2019-01-25 01:13:49 am

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

Re: How to get pid of current script process

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set theID to current application's NSRunningApplication's currentApplication()'s processIdentifier()


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

Offline

 

#3 2019-01-25 03:16:10 am

Neophyte
Member
From:: Canberra, ACT, Australia
Registered: 2017-08-20
Posts: 52
Website

Re: How to get pid of current script process

Shane Stanley wrote:

set theID to current application's NSRunningApplication's currentApplication()'s processIdentifier()

Shane, once again, you've solved my problem.  Many thanks.

Garry


Public servants are people too

Offline

 

#4 2019-01-25 05:43:32 pm

Neophyte
Member
From:: Canberra, ACT, Australia
Registered: 2017-08-20
Posts: 52
Website

Re: How to get pid of current script process

I spoke too soon.   I get back "-1" instead of the process id.   I put the ASOC into a try/on error block but, no error was triggered.  Is the "-1" result an error ?  If so, is there a way to get back the cause ?  The process is named osascript.  Is that a problem ?

Thanks.


Public servants are people too

Offline

 

#5 2019-01-25 05:49:01 pm

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

Re: How to get pid of current script process

From the documentation for processIdentifier:

Not all applications have a pid. Applications without a pid return a value of -1


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

Offline

 

#6 2019-01-25 10:52:03 pm

Neophyte
Member
From:: Canberra, ACT, Australia
Registered: 2017-08-20
Posts: 52
Website

Re: How to get pid of current script process

In this case I've confirmed there is a pid.  The pid relates to the osascript process not the script it runs.  Perhaps that's the issue.

I'll find another way.  Easiest (but cumbersome) would be to get the pid when calling the script, saving that in a temporary file and reading that file in the called script.  That is:

Applescript:

set script_pid to do shell script "osascript" & script_file & " & echo $!"

Then, save script_pid in a file and add code to script_file to read it.  Another way would be for script_file to load the calling script and get the value of a global script_pid.  Also, can still get the unique pid by looking for the most recent osascript process which in my case will always be the one needed.

I'll do some testing.

Many thanks.


Public servants are people too

Offline

 

#7 2019-01-26 05:08:21 am

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

Re: How to get pid of current script process

This will get you the pid of any app launched:

Applescript:

use framework "Foundation"
use framework "AppKit" -- for NSWorkspace
use scripting additions
on watchForAppLaunches()
   set theNSWorkspace to current application's NSWorkspace's sharedWorkspace()
   set theNSNotificationCenter to theNSWorkspace's notificationCenter()
   theNSNotificationCenter's addObserver:me selector:"appWasActivated:" |name|:(current application's NSWorkspaceDidActivateApplicationNotification) object:(missing value)
end watchForAppLaunches
on appWasActivated:theNSNotification
   set userInfo to theNSNotification's userInfo()
   set theApp to userInfo's valueForKey:(current application's NSWorkspaceApplicationKey)
   set theAppName to (theApp's localizedName()) as text
   set theID to theApp's processIdentifier() as integer
   display dialog (theAppName & " was launched with pid " & theID)
   stopWatchingForAppLaunches()
end appWasActivated:
on stopWatchingForAppLaunches()
   set theNSWorkspace to current application's NSWorkspace's sharedWorkspace()
   set theNSNotificationCenter to theNSWorkspace's notificationCenter()
   theNSNotificationCenter's removeObserver:me
end stopWatchingForAppLaunches
watchForAppLaunches()


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

Offline

 

#8 2019-01-28 09:44:35 pm

Neophyte
Member
From:: Canberra, ACT, Australia
Registered: 2017-08-20
Posts: 52
Website

Re: How to get pid of current script process

Shane, many thanks again.  I've afraid ASOC is beyond me at present.  So, I've gone with just doing a pgrep -n osascript to get the most recent instance of my osascript calls.  It's a kludge but it works for my purpose.

Cheers.


Public servants are people too

Offline

 

#9 2019-10-14 03:31:02 am

Uwe
Member
Registered: 2018-03-29
Posts: 20

Re: How to get pid of current script process

Hello,

I wonder, whether the:

Applescript:

set theID to current application's NSRunningApplication's currentApplication()'s processIdentifier()

could be modified to find the PID of a running process (it is a hardware driver that needs to be restarted) within a Sandbox environment, i.e. without using System Events unix syntax ?

Does anybody know? (maybe I should open a new post for this)

Thank you, Uwe


Filed under: applescript, pid, sandbox

Offline

 

#10 2019-10-14 08:58:27 am

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

Re: How to get pid of current script process

I created a multi threaded applescript program in which I also needed the PID of each osascript thread.
Here is what I did

Applescript:


set pid to do shell script "osascript '" & (POSIX path of (path to me)) as text) & "/TestClient.scpt' &>/dev/null & echo $!"

The last bit "& echo $!" will return the process ID when called

Last edited by robertfern (2019-10-14 09:16:20 am)

Offline

 

#11 2019-10-14 11:16:15 am

Uwe
Member
Registered: 2018-03-29
Posts: 20

Re: How to get pid of current script process

Thank you very much Robert. Yes that works with a script file, but I cannot get it do work with a driver.app? If you have any other idea, I would be grateful.

Offline

 

#12 2019-10-14 11:30:02 am

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

Re: How to get pid of current script process

Is the driver.app written in AppleScript (i.e. a stay open application)?

  also

is there only one osascript running?

if so then easy.

Applescript:


try
   set pid to (word 1 of (do shell script "ps -ecwwo pid,comm | grep osascript")) as integer
on error
   display alert "No osascript running!"
end try

Last edited by robertfern (2019-10-14 02:23:29 pm)

Offline

 

#13 2019-10-14 06:26:53 pm

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

Re: How to get pid of current script process

Assuming you know the process's bundle ID, you can use this:

Applescript:

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set bundleId to "com.apple.Safari" -- whatever
set thePid to ((current application's NSRunningApplication's runningApplicationsWithBundleIdentifier:bundleId)'s firstObject())'s processIdentifier()


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

Offline

 

#14 2019-10-15 03:32:15 pm

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

Re: How to get pid of current script process

Here is another way to get the Process ID by comparing the times the process started.

Applescript:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

on run
   local cc, sc, i, pid, mdate, udate
   set mdate to (current date)
   copy mdate to udate
   set sc to paragraphs of (do shell script "ps -ecwwo pid,lstart,comm | grep osascript")
   repeat with i in sc
       set pid to (word 1 of i) as integer
       set cc to words 3 thru -2 of i
       set umonth to (offset of (item 1 of cc) in "XXJanFebMarAprMayJunJulAugSepOctNovDec") div 3
       tell udate to set {its month, its day, its year, its hours, its minutes, its seconds} to {umonth, item 2 of cc, item 6 of cc, item 3 of cc, item 4 of cc, item 5 of cc}
       if mdate = udate then exit repeat -- this is the correct pid
       set pid to 0 -- resets if date-times don't match
   end repeat
   -- pid now has the process id of this osacript
end run

if there is any delay from when the script first gets the date/time after it is launched
then change the last if statement to something like

Applescript:


if mdate - udate < 2 then exit repeat -- you can change the number from 2 to whatever your delay might be

Last edited by robertfern (2019-10-16 09:58:59 am)

Offline

 

#15 2019-10-16 09:36:39 am

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

Re: How to get pid of current script process

Another way is to use passed arguments to find the correct osascript if more than one osascript is running.

The below code is your script run from a "do shell script" command

Call the script ThreadTest.scpt

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

on run args
   local cc, osalist, i, pid, aid
   if class of args is list then -- arguments passed come in as a list
       set aid to item 1 of args -- 1st item is the random number passed as argument
   end if
   set osalist to paragraphs of (do shell script "ps -ecwwo pid,comm,args | grep osascript")
   set atid to text item delimiters -- save current text item delimiter
   set text item delimiters to " "
   repeat with i in osalist
       set pid to (word 1 of i) as integer
       if text item 3 of i is "osascript" then
           set cc to (text items 3 thru -1 of i) -- arguments passed to script
           if (item -1 of cc) is aid then exit repeat -- this one has the correct pid
       end if
       set pid to 0 -- resets if not osascript or correct script name
   end repeat
   set text item delimiters to atid -- resets text item delimiter back to what it was
   -- pid now has the process id of this osacript
   delay 25
end run

and here is the calling code that starts the osascript

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

local cpath, cid

set atid to text item delimiters
set text item delimiters to "/"
get (text items 1 thru -2 of POSIX path of (path to me))
set cpath to ((text items 1 thru -2 of POSIX path of (path to me)) & "ThreadTest.scpt") as text
set cid to random number from 11111 to 99999
do shell script "osascript '" & cpath & "' " & (cid as text) & " &>/dev/null & echo $!"
set text item delimiters to atid -- resets text item delimiter back to what it was

by passing the "cid" variable which contains a random number,
we are able to find ourself with the unix "ps" command and the arguments that were passed to it.

Last edited by robertfern (2019-10-16 09:45:38 am)

Offline

 

#16 2019-10-25 09:30:37 am

Uwe
Member
Registered: 2018-03-29
Posts: 20

Re: How to get pid of current script process

Thank you to all, I had to park that development for a week ago, as Catalina is creating challenges for one of my Applescript projects.  But I will come back to it and then report, how it went.

Thank you already! Uwe

P.S. Now off to post another cry for help (In Catalina Xcode does not show me a Sandbox turn off/on capability and my code does not show the right behaviour colours - something got wrong upon transfer)

Last edited by Uwe (2019-10-25 09:32:37 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)