Right now Apple Notes allows you to insert files. What I want is to create a link to the file, so that when I click on it it opens the file, but stores the file in Finder.
An application that already does this is Omnifocus. It allows you to either add the file to Omnifocus, or just add a link. I essentially am looking for a way to do the same thing in other applications, like Apple Notes, but have been unable to find an automatic way of doing this.
After tinkering around with this, I found a solution! Posting my solution in case anyone else is interested.
The first step is to copy the file’s path. This can be done by clicking on a file in Finder with two pictures. Then, hold down option. This will change “Copy [Filename]” to “Copy [Filename] as Pathname”. I had gotten this far before, but if you add this as a hyperlink in an application like Apple Notes or Bear, it will try to open it in the browser.
You have to add the characters “File:///” before the pathname. This tells the computer that it is a file, not a URL. So before the Pathname might look like “/Users/merickson/Dropbox/[Filename]”. Now, it should look like “File:////Users/merickson/Dropbox/[Filename]”. Clicking on that hyperlink will take you to the proper folder, and select the file.
Next steps: manually adding “File:///” to every hyperlink isn’t ideal. It would be nice if you could handle it the way Word does and just point the program at the right document. Perhaps there is a Automator workflow or something in TextExpander that can further automate the process. Let me know if you have thoughts on how to accomplish this!
One minor simplification… you can use the keyboard shortcut of option-command-C to directly copy the path. Saves the step of 2 finger click w option key! — jay
Here is one more take on this. Assuming you have Keyboard Maestro…
This automates the entire process
From within your note, type shift-command-K. You will then be prompted to select the file to link to. Click OK and then the properly formatted hyperlink will be inserted in your note.
Thank you so much! I do not yet have Keyboard Maestro and am just getting into these automation tools, but will probably get it soon. In the meantime, the keyboard shortcut is very helpful. Thanks!
The file:///... solution looks good! I have in my memory that this used to work for me. Now, when I create such a link (e.g., in AppleScript via tell application "System Events" to set myFileUrl to URL of theFile) my mac makes a beep sound and nothing happens (I remember the Finder or PathFinder would open) on click.
Is there a place to set what happens when a link with file:///... is clicked? Is this on the OS level, in Finder, in PathFinder?
Here is some AppleScript that will create links for all selected Folders/Files, including their name.
Handy to use in a Service, Keyboard Maestro etc.
Cheers
Iain
set theSeparator to " — "
set niceURL to {}
tell application "Finder"
set theSelection to the selection as alias list
repeat with i in theSelection
set {theName, theURL} to {displayed name of i, URL of i}
set the end of niceURL to theName & theSeparator & theURL & return & return
end repeat
end tell
set {saveTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {linefeed}}
set the clipboard to niceURL as text
set AppleScript's text item delimiters to saveTID
thanks @logic2design, this is a nice script, and I think basically what I am looking for. Also in my above short solution I use ... set myFileUrl to URL of theFile.
Now, something might be weird on my system:
When I paste the resulting link into the notes field of OmniFocus, it is recognised as a link (e.g. underlined, clickable). When I click it however, nothing happens.
When I paste it in iA Write (as an example), I get an error (“iA Writer does not have permissions to open…”, see attached screenshot)
I thought before that this is related to a potential conflict between Finder and PathFinder. The iA Writer error message suggests that this might be a permission problem…
I looked in System Preferences – Security & Privacy – Privacy
I added iA Writer to “Full Disk Access” and to “Files and Folders”… the same error persists…
Is there another place where I could change permissions to “open” files (not sure if “open” is the correct word… for the file:///... link to open Finder and select the linked file (not really necessary to open it)
If a generic macOS Copy as Markdown Link is all you need, there is also a Keyboard Maestro macro which covers many commonly used applications, and is readily extended:
Modified the script to create a Markdown link. Problem is that IA Writer still comes up with an error trying to open file or folder, but strangely it will open an App.
I suggest that the developer may need to make a change to the permissions as User changes don’t seem to have an effect, seen that a couple of times now with other Apps.
--Markdown Version
set niceURL to {}
tell application "Finder"
set theSelection to the selection as alias list
repeat with i in theSelection
set {theName, theURL} to {"[" & displayed name of i & "]", "(" & URL of i & ")"}
set the end of niceURL to theName & theURL & return & return
end repeat
end tell
set {saveTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {linefeed}}
set the clipboard to niceURL as text
set AppleScript's text item delimiters to saveTID
I have emailed with the developers of iA Writer. They say that it’s possible to link to anything that sits within the iA Writer – iCloud folder (and I have found no counter-example yet, they say it is an Apple sandboxing issue, but I do see your point @logic2design that the permissions are set by the user, aren’t they)
The weird thing is that the Attach File / create link menu item works in a Note field for Omni Focus, but not when I copy the file:///... link directly – on one computer, but it doesn’t work on a second computer, and I have no idea what the problem might be
@RobTrew the keyboard maestro script that copies markdown links from this impressive list of apps is really nice. Also @logic2design thanks for your support!