eosbtw
1
For example, how can I extract just leafpad
out of
/usr/share/applications/leafpad.desktop
I would like to account for not just /usr/share/applications/
but also for ~/.local/share/applications
.
The best I could come up with is to use a combination:
echo ${selected##*/} | cut -d '.' -f1
(where selected
is in the context of the code below)
#!/usr/bin/bash
desktop_file() {
fd . -H -e .desktop /usr/share/applications "$HOME/.local/share/applications"
}
selected="$(desktop_file | sort | fzf --reverse)"
[ -z "$selected" ] && exit
cd || return
echo ${selected##*/} | cut -d '.' -f1 | tr '[:upper:]' '[:lower:]' | xargs -I{} setsid -f bash -c "{}"
Source: bits taken from here
(The tr
bit is because of Alacritty.)
basename /usr/share/applications/leafpad.desktop .desktop
dalto
3
#!/usr/bin/env bash
MYPATH=/usr/share/applications/leafpad.desktop
NAME=$(basename ${MYPATH} .desktop)
echo "$NAME"
1 Like
This won’t remove the .desktop
suffix if I’m right
dalto
5
You are right, I didn’t read it carefully enough. Fixed.
manuel
6
Maybe first we need to know exactly what the OP wants to do…
1 Like
eosbtw
7
I’ve given the entire script. It’s an application launcher with .desktop files as the source.
manuel
8
Do you want to extract the app name from the name of the desktop file, or from the contents of the desktop file?
eosbtw
9
Just from the name of the .desktop file, not from the contents.
manuel
10
OK, then basename is your command.
1 Like
dalto
11
Be aware that desktop files can also look like this:
okularApplication_powerpoint_calligra.desktop
org.strawberrymusicplayer.strawberry.desktop
eosbtw
12
Yes! I have /usr/share/applications/org.pwmt.zathura.desktop
and that’s a problem.
So back to the drawing board
!
$ basename /usr/share/applications/org.pwmt.zathura.desktop .desktop
org.pwmt.zathura
Is this output not what you expect?
dalto
14
If you just want the name, I agree with @manual that extracting it out of the desktop file is a better solution.
manuel
16
Something like this:
cat /usr/share/applications/xed.desktop | grep ^Exec= | head -n1 | cut -d'=' -f2 | awk '{print $1}'
1 Like
manuel
17
But even the above may not be optimal if there are many Exec= lines, or quotes used…
eosbtw
18
Yes, and there are the options like xed --new window and field codes (%F, %U, etc).
Overall too many possibilities 
manuel
19
The above already handles some problems, try it and see. 
eosbtw
20
And I just saw there is
/usr/share/applications/org.gnome.FileRoller.desktop:TryExec=file-roller