Mac OS - Can I generate a clickable link to local file using relative path like “file:///~”

23,313

Solution 1

I actually found a reasonably quick solution to this.

There's a free OSX app called Lincastor https://onflapp.wordpress.com/lincastor/ which is just awesome! It allows to trigger a certain shell command, applescript or app whenever a custom URL handler is used.

So I'm going to:

  1. have a script on user A generate a ling starting with googledrive:// instead of file://
  2. install Lincastor on every user's mac and set it to open googledrive:// urls and point to the user's home folder plus the path in the link

Solution 2

Applescript has the "path to" command. It can automatically generate the path to many common folders, the home folder being one of them.

So here's how I would do your task. Save the following applescript code as an application. Then just email that application everyone. When they double-click it it will 1) if you link to a folder then a Finder window will open to that folder or 2) if you link to a file then the file will automatically open using the default application. NOTE: if you don't want files to open then you can change "open" to "reveal" in the code so only a Finder window will open to the item.

IMPORTANT: applescript uses colon (:) delimited paths instead of "/", so make sure to use colons in your path as shown in my code.

set itemPath to (path to home folder as text) & "Google Drive:path:to:some:file:or:folder"
tell application "Finder"
    set theItem to item itemPath
    if (class of theItem) is folder then activate
    open theItem
end tell
Share:
23,313
Iacopo Boccalari
Author by

Iacopo Boccalari

Updated on January 18, 2020

Comments

  • Iacopo Boccalari
    Iacopo Boccalari over 4 years

    In my company, we use Google Drive and often have to tell each other the path to a particular file. Paths are long as we use a lot of nested folders, so it's pretty boring and time consuming to browse through all that.

    Now, in my previous company, we used a central afp server, so the path was common to everyone, therefore I had created an applescript script that could get the absolute afp path to the file, people pasted that path in a mail or chat and the end user could click on that path which automatically became a link and the finder would open, selecting that particular file or folder that the link led to.

    The reason I can't use the exact same script is that Google Drive folder is in the user home folder. So if user's name is Foo the path will be file:///Users/Foo/Google Drive, while for user Bar the path will be file:///Users/Bar/Google Drive

    Clearly the path generated from user Foo won't work for user Bar

    Since, at least in the terminal, the path to file:///Users/username equals to ~, I made my script generate links like file:///~/Google Drive/pathToFolder, but they do not work :( Clicking on such a link will open the finder, but won't select the right file or folder

    This is driving me crazy because it looks like it should work but it simply doesn't... is there any other syntax I should try? Any suggestion?

  • Iacopo Boccalari
    Iacopo Boccalari over 9 years
    I don't understand I how would use that to open a different link at any time.
  • regulus6633
    regulus6633 over 9 years
    This will work for your path for anyone who runs it. It will open the proper item on the person's computer. If you want a different link just change the path and send out a new app.
  • Iacopo Boccalari
    Iacopo Boccalari over 9 years
    I think there's a little misunderstanding here :) we're exchanging tens of links a day, the app has to generate a different link every time. Anyway I found a solution as shown in my reply, thanks for helping
  • regulus6633
    regulus6633 over 9 years
    Glad you found a solution. Good luck.
  • Calion
    Calion almost 3 years
    Current URL for Lincastor: onflapp.github.io/blog/pages/LinCastor.html