dsiegler19
dsiegler19

Reputation: 499

Bash Command to Print All Apps Currently Running

What would be a command to print just the running apps (i.e. just the apps that show up on the dock). For example:

Chrome
Microsoft Word
Microsoft Outlook
Etc. 

But not

Microsoft Helper App
Other helper apps not shown on the dock

Is there a tag to add to the ps command or is there an entirely different command to do this?

Upvotes: 2

Views: 1124

Answers (2)

mklement0
mklement0

Reputation: 440679

Update: Turns out there's a simple, robust solution using AppleScript:

As a one-liner:

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

More readable version:

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" tells AppleScript to separate list items with \n (a newline) when converting a list to a string.

  • The heart of the tell application "System Events" to ... command, name of every application process whose background only is false returns a list of application processes from applications not designed to run in the background.


Original, fragile answer:

Unless you dig deeper than is possible with command-line utilities into individual running applications to determine whether they have a UI, you need to resort to heuristics, such as excluding matches with certain words in the filename (helper, ...) - which will never be fully robust.

Here's another stab at it, to complement alvits' helpful answer:

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

Upvotes: 5

alvits
alvits

Reputation: 6768

Here's what you can try.

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

This will display the processes as you have posted.

The inner $(pgrep -u $USER -d, -f /Application) will print the PIDs of the processes owned by user $USER delimited by comma.

The outer ps will print the processes identified by process id list in -p ....

-o comm tells ps to only print the process names.

-c tells ps to exclude pathnames of the processes.

Or

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

This will display the full path to the processes.

Upvotes: 1

Related Questions