Bash 打印当前所有应用程序的命令 运行

Bash Command to Print All Apps Currently Running

仅打印 运行 应用程序(即仅显示在 Dock 上的应用程序)的命令是什么?例如:

Chrome
Microsoft Word
Microsoft Outlook
Etc. 

但是不是

Microsoft Helper App
Other helper apps not shown on the dock

是否有要添加到 ps 命令的标记,或者是否有完全不同的命令来执行此操作?

以下是您可以尝试的方法。

ps -c -o comm -p $(pgrep -u $USER -d, -f /Applications) | grep -Ev 'Helper|handler'

这将显示您发布的过程。

内部 $(pgrep -u $USER -d, -f /Application) 将打印用户 $USER 拥有的进程的 PIDs,以逗号分隔。

外层的ps会打印-p ....

中进程id列表标识的进程

-o comm 告诉 ps 只打印进程名称。

-c 告诉 ps 排除进程的路径名。

或者

ps -u $USER -o comm | grep /Applications | grep -Ev 'Helper|handler'

这将显示进程的完整路径。

更新:原来有一个使用AppleScript的简单、可靠的解决方案:

作为单线:

osascript -e 'set text item delimiters to "\n"' -e 'tell application "System Events" to (name of every application process whose background only is false) as string' | sort

更具可读性的版本:

osascript -e 'set text item delimiters to "\n"' \
  -e 'tell application "System Events" to ¬
  (name of every application process whose background only is false) as string' | sort
  • set text item delimiters to "\n" 告诉 AppleScript 在将列表转换为字符串时用 \n(换行符)分隔列表项。

  • tell application "System Events" to ...命令的核心,name of every application process whose background only is falsereturns应用进程列表来自应用程序不是设计的在后台 运行。


原始的,脆弱的答案:

除非您比使用命令行实用程序更深入地挖掘单个 运行ning 应用程序以确定它们是否具有 UI,否则您需要求助于 heuristics,例如排除与文件名中某些单词的匹配项 (helper, ...) - 这永远不会完全可靠。

这是另一个尝试,以补充 :

pgrep -fl '.*/Applications/.*\.app/Contents/' | 
  sed -E 's:^[0-9]+ .*/([^/]+)\.app[[:>:]].*$::' | 
   grep -Evi 'helper|daemon|service|handler|settings' |
     sort -u