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:

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

Hi,

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


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


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?

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

Great. Thanks.