17

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?

7 Answers7

21

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.

TuxForLife
  • 1,305
  • 2
  • 18
  • 30
  • 1
    the %k did not work (nor %d) but I did need the bash -c part – Marc Van Daele Jul 14 '16 at 19:01
  • 1
    Glad I can help indirectly. You need the bash -c part to define ~ correctly. You don't need bash -c if you use the direct /home/marc path. – TuxForLife Jul 16 '16 at 04:13
  • 1
    bash -c done the job. helped me a lot thanks :) – Muthu Jul 28 '16 at 07:16
  • 3
    The %k resolves to the the full path, including the desktop file, so cd "%k" won't work. But you can use cd "$(dirname %k)". – ivant Sep 27 '16 at 22:43
  • I am confused, why do you go in a directory using "%k" and then the command just after && is executing something relative to ~ ? – vdegenne Feb 03 '20 at 12:01
  • @ivant Actually, I needed to use cd "$(dirname "%k")", or dirname would output some strange . . with my path containing spaces. – gog Jul 26 '23 at 12:30
7

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 :-)

Λlisue
  • 79
  • 9
    I was able to put Path=/my/path and that worked just fine. Path - If entry is of type Application, the working directory to run the program in. – Ripster Oct 04 '15 at 16:48
5

I don't understand why solution up there so upvoted, it do absolute different thing. It changes working directory to those where .desktop file is, while question was about to change working directory to those with opened file(s).

My solution:

Exec=/bin/bash -c 'cd $(dirname $0) && micro $0'

Bear in mind that setting env variables for .desktop files is different, like so

Exec=env CPATH=../include /bin/bash -c 'cd $(dirname $0) && micro $0'
Narical
  • 93
2

I'm not kidding, its this simple...
Add one new line:

Name={name}
Comment={comment}
Path={WORKING DIRECTORY}
Exec={application/script/executable/file}
Icon={path to icon}
Terminal={false}
Type={Application}
Categories={Category1;Category2;Category3;}
NoDisplay={false}
Hidden={false}
MimeType={MimeType if it has one;}
StartupNotify={true/False}
Name[en_US]={Name in whatever language you want}
Keywords={keyword1;keyword2;}

That should be the general format of .desktop files.

Now the line you want to add is:

Path=<Working Directory>

Every application you open with a .desktop file runs in a subshell, and the working directory by default is $HOME. However the Path= tells the subshell to work in a specific directory.

So after adding Path= to your .desktop shortcut, it will now act like that directory is the default working directory for whatever you're launching, without actually needing to change the default working directory.

zx485
  • 2,426
1

Similar but not identical approach to @tuxForLife:

passing current dir as first parameter:

#!/usr/bin/env xdg-open
[Desktop Entry]
Version=1.0
Type=Application
Terminal=true
Exec=/usr/bin/bash /home/frank/bin/checkExtensionless.sh "%k"
Name=checkExtensionless
Icon=system-upgrade

checkExtensionless.sh

I encountered that the pathcomes in single quotes if the filename has spaces '/somewhere/my stuff/' so I had to remove those, then cd into it. (builtin is just being overzealous to bypass any fancy cd wrappers in your .bashrc)

#!/usr/bin/env bash

prunedQuotes="$(echo $1 | sed -e "s/'//g")" workdir="$(dirname "$prunedQuotes")" builtin cd "$workdir" <your actual script>

Frank N
  • 1,340
0

You can go to /usr/share/applications and find the proper file.desktop that is called whenever you open a specific file type. Then edit it and in the exec line add cd PATH && to the start of the line.

Mostafa Shahverdy
  • 5,082
  • 2
  • 21
  • 34
0

OK -

Exec=cd PATH &&  <application-name> %F

did not work for me as PATH is not defined to point to the path of %F. The only variables available in any applications desktop file are:

  • %f a single filename.
  • %F multiple filenames.
  • %u a single URL.
  • %U multiple URLs.
  • %d a single directory. Used in conjunction with %f to locate a file.
  • %D multiple directories. Used in conjunction with %F to locate files.
  • %n a single filename without a path.
  • %N multiple filenames without paths.
  • %k a URI or local filename of the location of the desktop file.
  • %v the name of the Device entry.

There it is: %d is the single directory you're looking for, as it doesn't make sense to change to multiple directories. So go to /usr/share/applications and edit the file application-name.desktop (in your case pymol.desktop); you have to have superuser privileges to do that. Then locate the line that starts with "Exec=". It probably reads

Exec=<application-name> %F

(In your case Exec=pymol %F). Change this line to

Exec=cd %d && <application-name> %F

Save that desktop file, and next time you click on a .pdb file your working directory should be the one this .pdb file is in, so pymol can see all of your files there.

Of course, you could also start pymol from the command line after having first navigated to the directory where your files are, but I guess you wanted a GUI solution, so you have to do a little editing :-)