desktop application with gnome-terminal: relative paths to script and icon
Asked Answered
P

3

9

I am using gnome-terminal to create a clickable desktop application. The application should be downloaded in a zip folder, with a sub-directory bin containing my myapp.desktop. I would like to have a different directory somedir in my zip file containing main application script and the icon for the application.

However, gnome-terminal does not seem to work with relative paths. Here my myapp.desktop:

[Desktop Entry]
Exec=gnome-terminal -e "bash -c -i 'exec $SHELL --init-file ../somedir/myscript.sh'"
Icon=../somedir/myicon.gif
Terminal=true
Type=Application

My question is: how can I pass the directory from which the application icon was clicked to gnome-desktop? Or how can I organize my directory such that I do not have to have myicon.gif and myscript.sh in the same directory as myapp.desktop?

I am aware of the --working-directory argument to gnome-terminal and tried to pass $PWD, without success.

EDIT:

Related question: Desktop Launcher for Python Script Starts Program in Wrong Path (Linux)

A path can be specified in the .desktop file, but again, it does not seem to deal with relative paths. If I add Path=., it is not found.

Poitiers answered 10/12, 2015 at 15:47 Comment(6)
This isn't a gnome-terminal question. This is a .desktop launcher question. The question is what information about the .desktop file being launched does the launcher provide to the spawned process and what does it make available as variables/etc. for use in the .desktop file. I'm going to guess that the answer is not much that is of use to you for this.Turk
For the icon, I agree, it is a .desktop launcher issue. But event if I execute the gnome-terminal command above without launcher, it only finds my --init-file myscript.sh if it is either in the same directory as my myapp.desktop or if I give an absolute path.Poitiers
Calling a shell with an init file e.g. bash --init-file ../somedir/myscript.sh works well, but does not seem to work correctly with the paths in gnome-terminalPoitiers
Paths are relative to the working directory of whatever uses them. When you say myscript.sh that will look for myscript.sh in the current working directory of whatever process tries to use that path. I have no idea what working directory the launcher is going to use when it executes your gnome-terminal process. It might use the location of the .desktop file. It might use your home directory. It might use /. Or it might use anything else for that matter.Turk
Yes, I think the path is relative to my home directory, which is not what I want... In OSX, it worked for me to call cd -- "$(dirname "$0")" in my clickable bash script, but I haven't gotten it to work with the launcher in ubuntu .yetPoitiers
Your approach seems backwards. There are very good reasons paths are standardized and there are good reasons why all popular distros have package management systems. Package your tool according to the standard for your target audience(s) instead.Velarde
C
5

Based on this document and other answers on ask ubuntu (like this or this), I think that you cannot use relative paths directly in the desktop application.

You can however keep your files in the desired folders and include in the package a configuration script that copy the executable and the icon in the default paths, as specified in the documentation ($PATH for the executable and /usr/share/pixmaps for the icon).

Also, here there is a nice workaround. And here a solution like the one I proposed.

Everyone seems to agree that it is not possible to use relative paths but ...

... searching a little bit more it seems that there is the possibility to use the home directory as the starting point for the relative path by leave out the forward slash on the path. Based on this page (link) this:

  Exec="${HOME}/bin/scripts/UNIX/Prototype.bash"
  Path=${HOME}/bin/scripts/UNIX

doesn't work

But this:

  Exec="Prototype.bash"
  Path=bin/scripts/UNIX

should.

Capriola answered 14/12, 2015 at 21:0 Comment(1)
There are probably situations where the environment where the launcher runs is not in your home directory, but as a quick and dirty hack, this holds some promise.Velarde
T
2

This method does not work because $PWD inside .desktop file, point to home directory.So you should create an installation script to generate "myapp.desktop" and copy both of "myapp.desktop" and "icon.png" to specific directory. copy this script to root of your app and run it!

install.sh

#!/bin/bash

home_local=$HOME/.local/share
icon_dir=$home_local/icons/hicolor/48x48/apps

myapp_desktop=$home_local/applications/myapp.desktop
myapp_dir=$PWD
myapp=$myapp_dir/bin/myapp.sh
myapp_icons=$myapp_dir/icons/myapp.png

echo "[Desktop Entry]" > $myapp_desktop
echo "Name=MyApp" >> $myapp_desktop
echo "Exec=bash -c 'cd $myapp_dir;bash $myapp;exec bash'" >> $myapp_desktop
echo "Icon=myapp.png" >> $myapp_desktop
echo "Terminal=true" >> $myapp_desktop
echo "Type=Application" >> $myapp_desktop

cp $myapp_icon $icons_dir 
Taal answered 19/12, 2015 at 20:41 Comment(1)
both yours and @terence hill's solution look very promising, cannot test it over the holidays though because I don;t have an ubuntu machine (with X)Poitiers
W
1

Make that Exec like look like this:

Exec=gnome-terminal -e "bash -c -i 'set > /tmp/env_vars.log'"

Then check on the log file the available variables (e.g. where it is running from), and adjust your relative path accordingly. If there isn't anything usefull, maybe use dirname on some existing variable to derive the full path, etc.

You can also run like:

Exec=gnome-terminal -e "bash -c -i 'pwd' > /tmp/curdir.log"

And use the path found in curdir.log as the one to be relative to.

Weinert answered 16/12, 2015 at 0:48 Comment(3)
Where do I find the information from where the script was launched in the logfile? I searched for 'launcher' and 'gnome-terminal', but could not find anything.Poitiers
The log only contains the precise output that your command generates. The set command simply prints your environment, which however under Bash includes the value of PWD. You could include date, pwd, and other commands in the log to make it more useful. However, this is not robust against multiple simultaneous instances being started in rapid succession.Velarde
This does not work, $PWD at this moment is my home directory and not the directory in which I started the launcher...Poitiers

© 2022 - 2024 — McMap. All rights reserved.