bashshortcutfreedesktop.org

Find location of desktop shortcut which invoked bash script


I have a desktop shortcut e.g /home/user/Desktop/myfolder/link.desktop which invokes a bash script located somewhere else, e.g. /tmp/myscript.sh

Within my script, how can I find the path of the shortcut which invoked my script? Is it possible at all?

What I actually want to achieve is that there is a subfolder where the shortcut link is, e.g. /home/user/Desktop/myfolder/subfolder. And in my script I would like to be able to access the subfolder.

I have tried readlink -f but that will always return /home/user no matter where the shortcut icon lies.

I cannot set the work path as the shortcut link is generated and dynamically placed in different locations.


Solution

  • Well at least you can do something like this in your script:

    find / -iname '*desktop' -exec fgrep -l $0 \{\} \; 2>/dev/null
    

    That will travell your filesystem and searches every .desktop file for your script in it... But note, this can be misleading, as Someone can put comments in a .desktop... so you might create a searchstring first like ^Exec=/PATH/TO/$0 and use egrep instead of fgrep.

    Or you can do a copy function which edits the .desktop files when it copies it to it's location and adds it's new location as a parameter to the Exec line, e.g.:

    mycp() {
        sed "s/^Exec=.*/& $2/" $1 > $2 
    }
    

    Or (and I'd go with it) use the %k param in your Exec line, according to the spec.