简体   繁体   English

使用Apple脚本通过PID查找并比较过程路径

[英]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 . 这不是检查应用程序pathname而是检查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.

相关问题 找出进程是否由macOS上的系统(由pid)创建的方法? - Ways to find out if the process is created by system (by pid) on macOS? 在osx内核扩展中通过pid找出进程名称 - Find out the process name by pid in osx kernel extension 如何在Mac OS上的C / C ++ / Objective-C中找出SystemUIServer进程的PID? - How can I find out the PID of the SystemUIServer process in C/C++/Objective-C on Mac OS? MacOSX:确定进程(给定PID)是否以32位或64位Intel模式运行 - MacOSX: find out if a process (given a PID) is running in 32bit or in 64bit Intel mode 如何使用Apple脚本在Outlook中查找会议是否是定期会议 - How to find a meeting is a recurrence meeting or not in outlook using Apple script 如何使用 PID 杀死 Mac 进程 - QuickLookUIService - How to Kill a Mac process using PID - QuickLookUIService 如何在Mac OSX C ++中查找任何进程的PID - How to find the PID of any process in Mac OSX C++ 区分进程的不同实例或窗口并找到它们的 PID 而不是它们的所有者 PID 以在 macOS 中发送输入事件? - distinguish between different instances or windows of a process and find their PID instead of their owner PID to send input event in macOS? 如何找出进程正在使用的端口号 - How to find out which Port number a process is using 使用Apple Script单击按钮 - Clicking button using Apple Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM