Tuesday, April 30, 2024
0
rated 0 times [  0] [ 0]  / answers: 1 / hits: 441  / 2 Years ago, mon, april 25, 2022, 7:31:52

I often find that I need to figure out the command to execute a certain application, such as an Alarm Clock application or the System Monitor. However, many of the actual CLI commands to run these are not directly evident. What I usually do is just go to the Ubuntu Software Center, then search for the application's "common name" and hence find its package/command name. I would like to know if there is an easier or more efficient way of doing this.



I was wondering if there is a command similar to xprop, where clicking on an open (graphical) application would output its command in the terminal. In other words, I am interested in a graphical way of finding an application's command.



For example, if I run such a command and click on System Monitor, the terminal would output gnome-system-monitor, etc. Thanks.



PS. If a solution is not currently available, I would be interested in writing my own script with this functionality. I only know basic bash, however. I realize that should probably go under a separate question, but I'm just throwing it out there.


More From » command-line

 Answers
6

I wrote this (rather ugly) script to find the command to execute a running application by clicking on it. It uses xprop to get the "clicking interface" and to get information about the selected window:



#!/bin/bash

xprop > ./tmp

chk_desk=`grep -c "_NET_WM_DESKTOP_FILE(STRING)" ./tmp` #Checks whether application has a corresponding .desktop file. The '-c' counts the number of matches.

if [ $chk_desk -ne 0 ] ; then
desk_line=`grep "_NET_WM_DESKTOP_FILE(STRING)" ./tmp` #Extract line containing .desktop path
desk_path=${desk_line/"_NET_WM_DESKTOP_FILE(STRING) = "/} #Extract only the .desktop path
desk_path=${desk_path//"/} #Removes quotes
exe_line=`grep "Exec=" $desk_path | head -n1`
exe=${exe_line/"Exec="/}
else #If .desktop doesn't exist, it uses the PID to find the executable
pid_line=`grep "_NET_WM_PID(CARDINAL)" ./tmp`
pid=${pid_line/"_NET_WM_PID(CARDINAL) = "/}
exe_path=`readlink -f /proc/$pid/exe`
exe=${exe_path//usr/bin//}
fi

echo $exe

rm ./tmp

exit 0


In case you are wondering, I could not have used only the PID for matching since /proc/$pid/exe gives you the path of the "real" executable, or of the underlying service running the program. So if you used it on say 'geogebra', the script would output java instead. Or in the case of Firefox you would get the path /usr/lib/firefox/firefox, which I suppose is where the actual firefox executable exists, which means the /usr/bin/firefox points to it.



While firefox is not a big problem (you could use the "real" executable), for the java based applications the .desktop file is needed to get the command to run the actual application and not a blank java session.



This script, of course, will only work if you are using X as your display server (since it uses xprop). I'm sure there must be a way to make the script cleaner, though my main problem was having to create many variables to hold the grep line outputs and the multiple substitutions.


[#32311] Tuesday, April 26, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
asketbridiculou

Total Points: 168
Total Questions: 116
Total Answers: 115

Location: Antigua and Barbuda
Member since Sat, Jan 28, 2023
1 Year ago
asketbridiculou questions
Thu, Dec 22, 22, 08:36, 1 Year ago
Thu, Jan 27, 22, 14:28, 2 Years ago
Tue, Nov 29, 22, 21:50, 1 Year ago
Sun, Jun 20, 21, 04:42, 3 Years ago
Sun, Aug 15, 21, 08:42, 3 Years ago
;