How to change working directory when open a file with double clicking on ubuntu desktop?

Wanted to open a file (.pdb file: protein structure format, never mind) by a double-click.

The "open with" is manually directed to execute "pymol" (a program that open the .pdb format) with Ubuntu Tweak.

However, I found that the working directory is $HOME.

As other files on the directory I opened the file is crucial I want to access them.

I guess that the "pymol" is executed on $HOME while the absolute path of the "pdb file" (double-clicked file) is passed as an argument.

I guess this is somewhat general problem and ask a question:

How can I let the system change the "working directory" to the "directory where I clicked the file" and execute an program?


Although this is years later, here's what worked for me:

Exec=bash -c 'cd "%k" && ~/path/to/your/file'

%k gives you the path of the directory of the launcher you just launched.


None of the above solution worked on my debian. Also %d is deprecated. See Desktop Entry Specification.

So I'm using the following bash script to run pymol. First you need to create the following bash script file and save it in /usr/local/bin/run_pymol.sh. I saved the file in /usr/local because it seems the xdg-open does not understand what is the HOME directory or so on.

#!/usr/bin/env bash
# change working directory
cd `dirname $1`
# run pymol
/usr/bin/env pymol $@

Second, give the executable permission to that file. Open a terminal and type the following commands to give a permission and to confirm.

% chmod +x /usr/local/bin/run_pymol.sh
% run_pymol.sh

Finally, modify your pymol.desktop file as the following and then your pymol will start in the directory where the first given file is located.

#!/usr/bin/env xdg-open
[Desktop Entry]
Version=1.0
Name=PyMOL Molecular Graphics System
GenericName=Molecular Modeller
Comment=Model molecular structures and produce high-quality images of them
Type=Application
Exec=/usr/local/bin/run_pymol.sh %U
TryExec=pymol
Icon=pymol
MimeType=chemical/x-pdb
Categories=Education;Science;Chemistry;

Hope it helps you guys :-)