1

Can I start a desktop launcher from command line? E.g. I have a desktop launcher for slack in my ~/Desktop directory:

vadim@my-pc ~/Desktop $ cat slack.desktop 
[Desktop Entry]
Name=Slack
Comment=Slack Desktop
GenericName=Slack Client for Linux
Exec=/usr/bin/slack --proxy-server="10.1.50.8:8080"
Icon=/usr/share/pixmaps/slack.png
Type=Application
StartupNotify=true
Categories=GNOME;GTK;Network;InstantMessaging;
MimeType=x-scheme-handler/slack;
Name[en_US]=slack

In the Exec parameter I can specify various options to start application with, for instance proxy server. I find it convenient to specify proxy exactly in the parameters of desktop launcher.

Now I'd like to be able to start slack from command line using all start options from the desktop launcher. I know I can create a shell script file and specify in it all start options I want and run this file both from desktop launcher and from command line, but is there a way to just run slack.desktop file from command line?

  • I don't understand. It is much simpler, faster and easier to use a script, a function, or an alias for this. Why would you want to go to the trouble of using a desktop file? – terdon Jul 20 '18 at 10:22
  • @terdon I find it convenient to change proxy and other settings through gui window by right-clicking on launcher icon on my desktop. I mostly start slack by clicking desktop launcher, but also want to be able to start it from command line in some cases. – Vadim Zverev Jul 20 '18 at 10:26
  • See askubuntu.com/questions/5172/… as well... – Gert van den Berg Jul 20 '18 at 11:07
  • @GertvandenBerg Hm, it seems to be exactly my question. I tried gtk-launch slack.desktop from one of the answers and it works! I guess my question should be marked as duplicated. – Vadim Zverev Jul 20 '18 at 11:17
  • It was on a different site (And Mint questions would be off-topic on AskUbuntu), so I'm not sure if it counts as a duplicate... – Gert van den Berg Jul 20 '18 at 11:18
1

You can use a tool like xdg-open (Broken currently), kde-open, gnome-open, gtk-launch or the equivalent for your desktop-environment. (xdg-open seems to be supposed to be the universal one...)

(It seems like just about everything other than gtk-launch (more similar tools might exist for other toolkits) suffers from the same bug as xdg-open - it opens the .desktop file in an editor instead of running it)

Packages: (Exact name will differ betweem distros) (this is based on CentOS 7)

  • gtk3 for gtk-launch
  • xdg-utils for xdg-open
  • kde-runtime for kde-open
  • libgnome for gnome-open
  • Both xdg-open and gnome-open open the desktop file for editing instead of executing. Presumably because of the bug you linked to. Shame, this would indeed have been a better way. – terdon Jul 20 '18 at 12:05
  • Ah, ok.. Edited to add gtk-launch as well – Gert van den Berg Jul 20 '18 at 15:48
  • That gives me gtk-launch: no such application /home/terdon/Desktop/foo.desktop (and yes, foo.desktop exists and can be launched by double clicking). I'm using Arch and Cinnamon. Have you managed to get any of these to work on your system? – terdon Jul 20 '18 at 16:07
  • @terdon I'm currently a bit far from an up to date *nix system using a GUI. gtk-launch was added based on the OP's comment that it worked for him. gtk-launch seems to be provided in the gtk3 package – Gert van den Berg Jul 20 '18 at 16:22
  • Ah, nice, +1 then. I have gtk-launch, it just doesn't like .desktop files on my system for some reason. – terdon Jul 20 '18 at 17:31
0

If you really want this, I suggest you write a little function that extracts the executable name from the .desktop file and runs it. Add these lines to your shell's initialization file (e.g. ~/.bashrc):

runDesktop () {
  eval "$(awk -F= '$1=="Exec"{$1=""; print}' "$1")"
}

Then, you can run your .desktop file with runDesktop ~/Desktop/slack.desktop. Of course, the usual caveats concerning eval apply.

You could try making it a bit more complicated sophisticated:

runDesktop () {
  comm=( $(awk -F= '$1=="Exec"{$1=""; print}' "$1") )
  "${comm[0]}" "${comm[@]:1}" &
  disown
}
  • It works, thanks. But when I close a terminal slack is being killed. I tried to unbind slack from terminal by changing a command to nohup "${comm[0]}" "${comm[@]:1}" & and I see that now slack starts kind of asynchronously, but still slack is being killed after closing a terminal. – Vadim Zverev Jul 20 '18 at 11:00
  • @VadimZverev use disown. See updated answer. – terdon Jul 20 '18 at 12:09
  • Yes, disown did the trick. – Vadim Zverev Jul 20 '18 at 12:21
0

The dex application is probably the simplest way to do this.

sudo apt install dex and then dex ~/Desktop/some-application.desktop or since many/most system applications are in /usr you could use dex /usr/share/applications/some-application.desktop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.