Friday, May 3, 2024
13
rated 0 times [  13] [ 0]  / answers: 1 / hits: 12455  / 3 Years ago, thu, august 19, 2021, 9:49:22

I want to list only running application like: Firefox, gedit, Nautilus, etc. using the command line.



Note: I don't want to list all running process, only applications which are running (say manually launched GUIs).


More From » command-line

 Answers
1

A combination of wmctrl and xprop offers many possibilities.



Example 1:



running_gui_apps() {

# loop through all open windows (ids)
for win_id in $( wmctrl -l | cut -d' ' -f1 ); do

# test if window is a normal window
if $( xprop -id $win_id _NET_WM_WINDOW_TYPE | grep -q _NET_WM_WINDOW_TYPE_NORMAL ) ; then

echo "$( xprop -id $win_id WM_CLASS | cut -d" " -f4- )"", window id: $win_id"

fi

done
}


The output could look in this case similar like this:



"Firefox", window id: 0x032000a9
"Gnome-terminal", window id: 0x03a0000c
"Thunar", window id: 0x03600004
"Geany", window id: 0x03c00003
"Thunar", window id: 0x0360223e
"Mousepad", window id: 0x02c00003
"Mousepad", window id: 0x02c00248
"Xfce4-terminal", window id: 0x03e00004


Example 2:



running_gui_apps() {
applications=()

# loop through all open windows (ids)
for win_id in $( wmctrl -l | cut -d' ' -f1 ); do

# test if window is a normal window
if $( xprop -id $win_id _NET_WM_WINDOW_TYPE | grep -q _NET_WM_WINDOW_TYPE_NORMAL ) ; then

# filter application name and remove double-quote at beginning and end
appname=$( xprop -id $win_id WM_CLASS | cut -d" " -f4 )
appname=${appname#?}
appname=${appname%?}

# add to result list
applications+=( "$appname" )

fi

done

# sort result list and remove duplicates
readarray -t applications < <(printf '%s0' "${applications[@]}" | sort -z | xargs -0n1 | uniq)

printf -- '%s
' "${applications[@]}"
}


Output example:



Firefox
Geany
Gnome-terminal
Mousepad
Thunar
Xfce4-terminal


You can add the function to your ~/.bashrc or run it from an script file.


[#24628] Friday, August 20, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
irtuallyefu

Total Points: 429
Total Questions: 97
Total Answers: 119

Location: Hong Kong
Member since Tue, Oct 19, 2021
3 Years ago
irtuallyefu questions
;