2016-03-04 29 views
2

什麼是僅打印正在運行的應用程序(即僅顯示在碼頭上的應用程序)的命令。例如:Bash命令打印當前正在運行的所有應用程序

Chrome 
Microsoft Word 
Microsoft Outlook 
Etc. 

Microsoft Helper App 
Other helper apps not shown on the dock 

是否有標籤添加到ps命令或者是有一個完全不同的命令來做到這一點?

+0

雖然它不是一個單一的命令,你可以在*〜/庫/首選項/ com.apple.dock解析從plist文件數據。 plist中*。當然,你需要先將它從二進制轉換爲文本格式。 – TheDarkKnight

+0

@TheDarkKnight:不幸的是,這不起作用,因爲只有Dock中顯示的應用程序_persistently_在該文件中才能顯示 - 它不反映當前正在運行的應用程序。 – mklement0

+0

我可以結合2. – dsiegler19

回答

4

更新:原來有使用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 false返回從應用設計爲在後臺運行的應用程序的列表。


原始,脆弱的答案:

除非你深入挖掘比使用命令行實用程序爲單獨運行的應用程序,以確定他們是否有一個UI,你需要求助於啓發式,例如排除文件名中某些詞的匹配(helper,...) - 這將永遠不會完全健壯。

下面是它的另一種嘗試,以補充alvits' helpful answer

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

將「設置」添加到列表使它工作得很好。 – dsiegler19

+1

完美地工作。 Ty(但不幸的是,這只是我正在做的一小部分)。 – dsiegler19

1

以下是您可以嘗試的方法。

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

這將顯示您發佈的流程。

內部$(pgrep -u $USER -d, -f /Application)將打印由逗號分隔的由用戶$USER擁有的進程的PID

外部ps將打印-p ...中進程標識列表標識的進程。

-o comm告訴ps只打印進程名稱。

-c告訴ps排除進程的路徑名。

或者

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

這將顯示完整路徑的過程。

+0

這是正確的道路,但我仍然得到像bash,java和Microsoft Database Daemon這樣的輸出,所有這些都是助手應用程序或進程。 – dsiegler19

+0

我不明白爲什麼'bash'會在正則表達式是'/ Applications'時出現。 – alvits

+0

@ dsiegler19 - 你從'pgrep'中刪除了'-f/Applications'嗎?或者,也許你用'-f .'替換了'-f/Applications'? – alvits

相關問題