Link to external files

Hi,

Is there a way to create links to files outside the obsidian directory, particularly where the path contains spaces?

So far I can get such links to work using [readable name](file://path-here) but the link doesn’t render in preview and is only clickable in editor mode. Simply using file://path-here doesn’t render as a link or work in the editor.

This looks like I might be trying to achieve the same as the OP of this graveyarded bug and would be related to this post as well.

Any help is greatly appreciated. Loving Obsidian so far but this would be really useful for my job and I’m sure others would find it to be too.

Cheers
Ave54

EDIT: I have resolved my own issue. Discovered within the format help doc that you can escape spaces in links by enclosing it in <> (in much the same way as windows uses “” to enclose file paths with spaces).

Therefore it is possible to use a markdown link to link to external files like so: [pretty filename](<file:///path to file with spaces.extension>) which will create a clickable link in preview titled “pretty filename”.

Note that the link doesn’t work in editor mode (ctrl + click registers it as a link but opens a windows dialog “you’ll need a new app to open this app link”. I assume this is a bug.

11 Likes

I found the most reliable way to do links is to do a fully converted webbrowser style URI. In this, spaces become %20. There is no easy way to type such a link, but I found and adapted a Windows shell extension so I can send any file to the clipboard as a pastable Obsidian link.

I can post C# source code later. Could also post a binary program, but I don’t know right ways to do that.

1 Like

Thanks, replacing spaces with %20 solved the problem. Just need to find a way to simplify link conversion.

This was my solution, in Windows:

using System;
using System.Windows.Forms;
using System.IO;

namespace AbsoluteUriGenerator
{
  class Program
  {
    [STAThread]
    static int Main(string[] args)
    {
      if (args.Length != 1)
      {
        var executableName = typeof(Program).Assembly.GetName().Name + ".exe";
        Console.WriteLine("Usage: {0} pathToConvert", executableName);
        Console.WriteLine("Example: {0} \"C:\\foo\"", executableName);
        return 1;
      }

      var path = args[0];
      Console.WriteLine("Resolving file:// URI of path: {0}", path);
      var uri = new Uri(path).AbsoluteUri;
      var name = Path.GetFileName(path);

      Console.WriteLine("Setting clipboard to resolved file:// URI: {0}", uri);
      Clipboard.SetText("["+name+"](" + uri + ")");
      return 0;
    }
  }
}

Adapted from https://superuser.com/questions/297060/generate-file-uris-in-windows-explorer

If you compile this, you can copy that to your shell:sendto folder, then right click on any file and click send-to and click Copy_URI. Gives you any file on your computer.

2 Likes

I’m not able to click the link in Preview even after enclosing the absolute path within <>.

e.g. [Perspective Taking](<file://Users/JL/Documents/ACT Immersion/Perspective Taking.pdf>)

Using version v0.10.8

Is it supposed to open up the file with the system default app?

2 Likes

Hi,

There is a problem mentioned before, ther are spaces in your URI.
A new plugins “File path to URI” can help you.
Fules

1 Like

It works now, Thank you Fules!

On macOS you can create an AppleScript script, which saves URLs of selected files in Finder to the clipboard:

tell application "Finder"
    set _sel to (get selection as alias list)
    set _urls to {}
    repeat with _file in _sel
        set _url to URL of _file
        set end of _urls to _url
    end repeat
    set AppleScript's text item delimiters to return
    set the clipboard to (_urls as string)
end tell

Save this script as Copy URLs.applescript and put it into the ~/Library/Scripts/Applications/Finder/ folder. Now you can select a file in Finder and run the script via the Script menu, located in the OS menu bar (make sure the option “Show Script menu in menu bar” is checked in the General tab of the Preferences dialog of the Script Editor app). The URL of the selected file will then be copied to the clipboard and can be pasted into an Obsidian document. You may also assign a keyboard shortcut to the script.

8 Likes

It works beautifully! Thank you so much!

Is there a possibility to open .pdf on exact page?

on web browser works
file://path_to_file/File.pdf#page3
on windows
AcroRd32.exe /A page=3 “c:\path_to_file\File.pdf”

How to do that in obsidian? :slight_smile:

1 Like

Hi,

I have never tried, but the “Better PDF plugin” is about it.
Read it, how I remember he uses the “#page” format.
Füles

1 Like

i just realized that i can’t open [title](C:\some-file.pdf) in preview mode. it opens it in edit mode.
and there is no spaces in the link i am testing it with and i am using core obsidian to test. can someone please tell me what should i do or where to look?

Though this is an older topic, I ran into an issue with links to external files not functioning as expected between the Editor and Preview modes, despite following some of the instructions on this page.

Per the official help docs, you can also enclose links with spaces in angle brackets (<>). Using this convention looks to be properly picked up by the parser.

e.g.
[pretty name](<file:///drive:\path to file\file.ext>)

Using %20 did not function properly in Preview (but did work in the ‘link’ portion of the Editor mode). Hope this helps some other people trying to make heads or tails of this.

8 Likes

This is f**king awesome. Thank you!

Many of my notes refer to documents in Dropbox. So this is precisely what I need.

1 Like

I used a combination of the app service station and the apple script from above.

Save the following text as a file URLs.applescript and use Service Station to embed it into the finder right click action. The URL of any file will now be within the clipboard.

tell application "Finder"
     set _sel to (get selection as alias list)
     set _urls to {}
     repeat with _file in _sel
         set _url to URL of _file
         set end of _urls to _url
     end repeat
     set AppleScript's text item delimiters to return
     set the clipboard to (_urls as string)
 end tell

Hi good afternoon, I’d like to ask, how can I set Obsidian to link between notes using hyphen - instead of %20 for spaces. I want to create a blog with HUGO but the link created using Obsidian (obsidian://open?vault=Obsidian%20Database&file=blog%2Fcontent%2Fhobby%2FFOSS%20PC%2FImproving%20the%20shell%20experience ) creates the path using the %20 and therefore ERROR 404. As in this example:

Hi @saf-dmitry, thanks for this solution! It’s the only one working consistently for me! I am not a programmer, and have no clue if what I have done to your script is good practice or not, but wanted to share an addition:

on run {input, parameters}
	
	tell application "Finder"
		set _sel to (get selection as alias list)
		set _links to {}
		repeat with _file in _sel
			set _url to URL of _file
			set _name to name of _file
			set _link to "[" & _name & "](" & _url & ")"
			set end of _links to _link
		end repeat
		set AppleScript's text item delimiters to return
		set the clipboard to (_links as string)
	end tell
	
	return input
end run

It puts the following on the clipboard:

[filename.ext](file:///path_to_file/filename.ext)

3 Likes

BTW, I used automator to create a quick action with the script, and to make it available in Finder. Works like a charm.

Sorry for a naive question, but could you change the to-be-blogged note titles to get rid of the spaces or other characters you don’t like? (Will the URI update on note rename the way internal links do?)