Update 2019-06-19
Sorry, my original post is locked for updating, for whatever reason. So, a new post instead.
Well, here again a new update of my script:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
# SETTINGS
# Here you can manipulate the output of the script to your needs, or to the needs of the app you are going to paste the output in.
# Mode
# If we should get…
# (1) the file reference URL
# (2) the file URL
# (3) the file URL + the file reference URL
set URLMode to 1
-- set theMode to 2
-- set URLMode to 3
# File/folder icons
# The icon displayed before the file/folder name (including any space character in-between the icon and the name).
# Set them to an empty string if you don't want to have icons.
-- set {fileIcon, folderIcon} to {"📄 ", "📁 "} # Some standard icons
set {fileIcon, folderIcon} to {"", ""} # No icons
# The characters that should enclose the file name
# You can eliminate one or both by using an empty string.
# Having a leading and a trailing separator allows you to put the ref URL into some kind of brackets (e.g. for Markdown).
set {fileNameLeading, fileNameTrailing} to {"[", "]"} # File name wrapped in brackets (works with Markdown)
-- set {fileNameLeading, fileNameTrailing} to {" ", " — "} # Only a spaced em dash after the file name, no leading character
# The characters that should enclose the file reference URL
# You can eliminate one or both by using an empty string.
# Having a leading and a trailing separator allows you to put the ref URL into some kind of brackets (e.g. for Markdown).
set {refURLLeading, refURLTrailing} to {"(", ")"} # Ref URL wrapped in parenthesis (works with Markdown)
-- set {refURLLeading, refURLTrailing} to {" 〈", "〉 "} # Ref URL wrapped in Angle Brackets
-- set {refURLLeading, refURLTrailing} to {" — ", ""} # Only a spaced em dash before the ref URL, no final character
# The characters that should enclose the file URL
# You can eliminate one or both by using an empty string.
# Having a leading and a trailing separator allows you to put the ref URL into some kind of brackets (e.g. for Markdown).
set {fileURLLeading, fileURLTrailing} to {"(", ")"} # File URL wrapped in parenthesis (works with Markdown)
-- set {fileURLLeading, refURLTrailing} to {" 〈", "〉 "} # File URL wrapped in Angle Brackets
-- set {fileURLLeading, refURLTrailing} to {" — ", ""} # Only a spaced em dash before the file URL, no final character
# The filename/URL separator
# If using both URLs the resulting URLs string will likely not fit in the same line as the file name, so maybe probably nicer to separate with a line break right away.
-- set nameURLSeparator to linefeed
set nameURLSeparator to ""
# SCRIPT
set niceURL to {}
tell application "Finder" to set theSelection to the selection as alias list
# Get properties and construct a nice file ref URL
repeat with i in theSelection
set theIcon to fileIcon
tell application "Finder"
set {fileName, filePath} to {displayed name of i, POSIX path of i}
if kind of i is "Folder" then set theIcon to folderIcon
end tell
set fileURL to (current application's class "NSURL"'s fileURLWithPath:filePath)
set refURL to fileURL's fileReferenceURL()
set {fileURL, refURL} to {fileURL's absoluteString(), refURL's absoluteString()}
if URLMode is 3 then
# Getting both the file URL and the reference URL
set the end of niceURL to fileNameLeading & theIcon & fileName & fileNameTrailing & nameURLSeparator & fileURLLeading & fileURL & fileURLTrailing & refURLLeading & refURL & refURLTrailing
else if the URLMode is 2 then
# Getting only the file URL
set the end of niceURL to fileNameLeading & theIcon & fileName & fileNameTrailing & nameURLSeparator & fileURLLeading & fileURL & fileURLTrailing
else
# Getting only the file reference URL
set the end of niceURL to fileNameLeading & theIcon & fileName & fileNameTrailing & nameURLSeparator & refURLLeading & refURL & refURLTrailing
end if
end repeat
# Put it to the clipboard
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
What's new:
- Better explanations in the Settings part of the script
- Added setting for only using the file URL (see the Note below)
- For compatibility with Markdown targets (like Bear) now you can fully customize the parenthesis for the file name, the file ref URL and the file URL.
- The default settings now are Markdown-ish:
[<file name>](<file reference URL>)
- The default settings now are Markdown-ish:
With the example text from above and the new default settings, it looks like this in Bear:
…and in Things (which doesn’t have a Markdown interpreter):
I think, this a reasonable default setting: it works perfectly in Markdown-aware apps, and it’s not too ugly in plain-text apps. If you only ever paste into plain-text apps (Things, TaskPaper) then of course you should change the settings in the script to something visually more attractive.
Note:
In many apps the file reference URLs work as expected (you click the link and the destination file/folder will be shown in the Finder). However I noticed that in some apps they don’t: you get a “does not have permission to open” error.
This concerns for example iA Writer, Agenda. I guess, these apps are trying to open the linked file (instead of showing it in the Finder) and then hit some sandbox restrictions.
Interestingly, when using the file URL (not the file ref URL) these apps work correctly, i.e., they reveal the destination file in the Finder. So, if this problem hits you, you have to set URLMode
to 2
or 3
.