[英]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 ... 我正在使用Apple Script按名称查找进程PID,例如...
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". 通过此脚本,我可以知道所有名为“ KKK”的进程的PID。
But I have a question here. 但是我在这里有一个问题。
For example, there has three "KKK" process, "/FolderA/KKK", "FolderB/KKK", "FolderC/KKK" 例如,有三个“ KKK”过程,“ / 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". 我想终止“ / FolderA / KKK”的进程,因此我需要知道哪个PID属于“ / 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? 运行脚本后,我可以获得三个PID,在Apple Script中实际上可以做什么以区分我想要的PID? (Maybe to get the process path by PID?)
(也许要通过PID获取过程路径?)
Thank you 谢谢
Instead of the name
check for the path
of the application, this is an example checking for Messages.app
. 这不是检查应用程序
path
的name
而是检查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
; exit 0
to the end to eat an error if the app isn't running and have no need to check or trap otherwise: 如果应用未运行,则
; exit 0
到最后会报错,否则无需检查或捕获:
do shell script "kill -9 $(ps -x | awk '/[F]olderA\\/KKK/{print $1}'); exit 0"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.