Monday, July 16, 2018

#1 2014-12-19 12:54:05 pm

bmose
Member
From:: Massachusetts
Registered: 2006-01-03
Posts: 280

Get the unix process id of the current application

This may be old news, but my search for a solution came up empty.

I sometimes would like to get the unix process id of the current application (typically a stay-open application) from within its own code.  The following returns the current application's process id:

Applescript:

set currAppAlias to path to me
tell application "System Events"
   tell processes to set {applicationAliases, processIDs} to {its application file, its unix id}
   repeat with i from 1 to applicationAliases's length
       if applicationAliases's item i = currAppAlias then
           set currAppProcessId to processIDs's item i
           exit repeat
       end if
   end repeat
end tell
currAppProcessId --> unix process id of the current application

Last edited by bmose (2014-12-19 03:35:11 pm)

Offline

 

#2 2014-12-19 01:18:38 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11499
Website

Re: Get the unix process id of the current application

Hi,

the easiest way is to assign an unique bundle identifier to the stay-open app and use this

Applescript:


tell application "System Events"
   set currAppProcessId to unix id of 1st process whose bundle identifier is "com.mycompany.myapp"
end tell


regards

Stefan

Offline

 

#3 2014-12-19 01:53:54 pm

bmose
Member
From:: Massachusetts
Registered: 2006-01-03
Posts: 280

Re: Get the unix process id of the current application

Thanks.  I'm used to assigning a unique bundle identifier to Cocoa and Cocoa applescript apps but have not done that with vanilla Applescript apps.  Would you recommend modifying the CFBundleIdentifier property in the Info.plist file directly, or is there an alternative way?

Last edited by bmose (2014-12-19 01:54:57 pm)

Offline

 

#4 2014-12-19 02:10:32 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11499
Website

Re: Get the unix process id of the current application

you can change the value directly in AppleScript Editor opening the Bundle Contents drawer


regards

Stefan

Offline

 

#5 2014-12-19 02:42:42 pm

bmose
Member
From:: Massachusetts
Registered: 2006-01-03
Posts: 280

Re: Get the unix process id of the current application

Great.  Thanks.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)