简体   繁体   中英

Using apple script to find out and compare process path by PID

I am using Apple Script to find the process PID by name, like ...

set appName to "KKK"

tell application "System Events"
     set processPID to (unix id of processes whose name is appName)
end tell

With this script, I can know about PIDs of all processes which name is "KKK".

But I have a question here.

For example, there has three "KKK" process, "/FolderA/KKK", "FolderB/KKK", "FolderC/KKK"

I want to kill the process of "/FolderA/KKK", so I need to know which PID is belong to "/FolderA/KKK".

After run my script, I can get three PIDs, what can I actually do in Apple Script to distinguish which PID is what I want? (Maybe to get the process path by PID?)

Thank you

Instead of the name check for the path of the application, this is an example checking for Messages.app .

set appPath to "/Applications/Messages.app"

tell application "System Events"
    set processPID to (unix id of 1st process whose POSIX path of application file is appPath)
end tell

It also can be done using the following:

do shell script "kill -9 $(ps -x | awk '/[F]olderA\\/KKK/{print $1}')"

You can also add ; exit 0 ; exit 0 to the end to eat an error if the app isn't running and have no need to check or trap otherwise:

do shell script "kill -9 $(ps -x | awk '/[F]olderA\\/KKK/{print $1}'); exit 0"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM