Make link open target file, not file at link location

Sorry for the tags. I don't know whih one applies. Please remove not pertaining ones, then delete this line. Or comment, and I'll do it myself.

Background

I want to put a link to a file located in /home/myname/some/path/file.tex on my desktop. This TeX file calls other TeX files inside it. Now, when I open the link, which is located on the desktop, my TeX editor, it says I opened the file /home/myname/Desktop/file.tex. I can't compile this file, because the other TeX files the main TeX file relies upon are not located where the program expects them.

Main question

How can I make a link file open the target file in its actual location instead of on the desktop?

EDIT

The solution advertised here does not work as intended. With

#!/bin/bash
exec /home/myname/some/path/file.tex "$@"

I get

/tmp/geany_run_script_JVX1HZ.sh: 7: /tmp/geany_run_script_JVX1HZ.sh: ./open-file.sh: Permission denied


—----------------
(program exited with code: 126)
Press return to continue

Where open-file.sh is the wrapper script located on my desktop.

EDIT 2

Although, if I open a terminal and do

chmod a+x ./open-file.sh

and change exec to texstudio, which incidentally is the program I use to open .tex files, then I can open a terminal and do

bash open-file.sh

it will launch TeXStudio with the desired file loaded in the desired target location. I still can't just double-click open-file.sh on my desktop, since that will open my text editor, letting me edit the bash script. I want to double-click the open-file.sh script file and have TeXStudio open up my file.tex file.

### EDIT 3

From this question, I deduct that I have done everything right and the file should be clickable and be executed upon clicking. Unfortunately, it doesn't. I think my permissions are correct, afaik

enter image description here

and same here

-rwxr-xr-x me me 94 Apr 27 12:00 open-file

Solution 1:

This is due to the semantics of symbolic links. While you can't change the way symbolic links are handled, you can use a Link-type desktop file instead.

Make a text file in ~/Desktop called e.g. file.tex.desktop with these contents:

[Desktop Entry]
Name=file.tex
Type=Link
URL=file:///home/myname/some/path/file.tex

This will open the path

/home/myname/some/path/file.tex

with the default text editor in the working directory

/home/myname/some/path/

regardless of the location of the desktop file.

You will need to adjust the Name and URL values to be suitable, but everything else can stay the same. Here's an actual working example:

[Desktop Entry]
Name=Link to utf8test.tex
Type=Link
URL=file:///usr/share/texlive/texmf-dist/tex/latex/base/utf8test.tex

Note that the URL must be a proper file:// URL. Some file managers will assist you in generating these files if you'd rather not do it by hand.

Related:

  • Is it possible to create a 'shortcut link' instead of 'symbolic link' to a folder?

  • How do I create an shortcut to a folder(not a symlink) in Ubuntu 18.04 like shortcuts in windows

  • https://unix.stackexchange.com/questions/310618/is-it-possible-to-create-a-soft-link-on-my-desktop-which-opens-with-the-target-p

Solution 2:

I don't know how to make a link itself work like this, but there's a relatively easy way to get it done.

The problem appears to be that program invoked to process your data is not running with its working directory set to the location of your data file.

Instead of linking the file itself, write a launcher script and pass your file location to it as a parameter. Then, link to the script from your desktop entry.

At least in KDE, this is easier to do by adding an entry to your Application Launcher with all the parameters set the way you want them and then dragging the entry from the menu to the desktop. It has a bunch of placeholder variables that let you modify the command and even lets you specify the working directory to use.

This could be simple or fancy. Start with simple:

#!/bin/bash
## Usage: launcheroo working-directory data-file
## defending this script is left as an exercise for the reader
## ... or you can ask in the comments ;)
cd "$1"
myprog "$2" ## or your more fancy exec that will figure out
            ## what program to run by itself

invoked as:

launcheroo path-to-data-files data-file

This handles the most basic usage, and illustrates the approach, but doesn't handle any errors like bad or missing parameters

To get a little fancier, you could extract the path from the specified data file path using dirname or bash parameter editing instead of passing it as a separate parameter.

A quick and dirty way to get this done - especially for testing how it works - would be to just edit the desktop icon to run

cd where-the-files-are ; exec ...

Since this is Linux, there are probably many other ways to do this as well.

If you add the file as an icon on the desktop, you can directly edit its .desktop file and get it to do all sorts of interesting things. I haven't done much with those.

Just saw this answer which is essentially the same as mine with a slightly different spin to it.