简体   繁体   English

如何在OS X中获取当前前台应用程序的名称或PID?

[英]How to get current foreground application's name or PID in OS X?

I need to to get current foreground application's name or PID in OS X? 我需要在OS X中获取当前前台应用程序的名称或PID吗? How to get it using terminal? 如何使用终端获取它?

You can find this information from the terminal using the 'lsappinfo' command. 您可以使用'lsappinfo'命令从终端找到此信息。 The man page has a lot of detail about the huge amount of information that this tool can return. 手册页有很多关于此工具可以返回的大量信息的详细信息。

In order to get the frontmost application, you can call lsappinfo with the front argument. 为了获得最前面的应用程序,您可以使用front参数调用lsappinfo。

$ lsappinfo front
ASN:0x0-0x10010:

This returns the unique application specifier (ASN) that launch services uses to identify processes. 这将返回启动服务用于标识进程的唯一应用程序说明符(ASN)。 You can feed that back in to lsappinfo to get more details about that process. 您可以将其反馈到lsappinfo以获取有关该过程的更多详细信息。

$ lsappinfo info `lsappinfo front`
"Terminal" ASN:0x0-0x10010: (in front) 
    bundleID="com.apple.Terminal"
    bundle path="/Applications/Utilities/Terminal.app"
    executable path="/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal"
    pid = 652 type="Foreground" flavor=3 Version="326" fileType="APPL" creator="????" Arch=x86_64 
    parentASN="loginwindow" ASN:0x0-0x1001: 
    launch time =  2014/06/25 15:13:00 ( 8 days, 39 minutes, 25.0882 seconds ago )
    checkin time = 2014/06/25 15:13:00 ( 8 days, 39 minutes, 24.6907 seconds ago )
    launch to checkin time: 0.397498 seconds

The info flag takes a -only flag to only return certain fields. info标志只使用-only标志来返回某些字段。 Using that, you can query for the PID, the app name, the path to the app bundle, etc. 使用它,您可以查询PID,应用程序名称,应用程序包的路径等。

$ lsappinfo info -only pid `lsappinfo front` 
"pid"=652

$ lsappinfo info -only name `lsappinfo front` 
"LSDisplayName"="Terminal"

$ lsappinfo info -only bundlepath `lsappinfo front` 
"LSBundlePath"="/Applications/Utilities/Terminal.app"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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