Copy Encoded URL(s) of File(s) Selected in Finder or Path Finder

Copy encoded URLs of selected files.kmmacros (20.1 KB)

For example, for pasting clickable links into TaskPaper 3

(function () {
    'use strict';

    var strFrontApp = Application("System Events")
        .applicationProcesses.whose({
            frontmost: true
        })
        .at(0)
        .name(),
        blnFinder = ["Finder", "Path Finder"].indexOf(strFrontApp) !== -1;

    if (blnFinder) {
        var app = Application(strFrontApp),
            lstSeln = app
            .selection(),
            lstURL = (lstSeln && lstSeln.length) ? lstSeln
            .map(function (x) {
                return x.url();
            }) : [];

        if (lstURL.length) {
            var sa = (app.includeStandardAdditions = true, app),
                strUrls = lstURL.join('\n');

            sa.setTheClipboardTo(strUrls);
            return strUrls;
        }
    }

})();
4 Likes

Rob,

Thanks for sharing this script. It is very userful.

I'd like to modify it to return a rich text hyperlink using the file name and URL.
I know how to create this from our previous work.

My question is, where/how to mod your code?
I'm thinking in this block:

.map(function (x) {
   return x.url();

Instead of return x.url(), I could use my function and
return createRTFHyperlink(x.name(), x.url());

See anything wrong with that approach? Suggest otherwise?

Looks fine.

Thanks, Rob. Just what I was looking for.

1 Like

I am trying to use this but getting mixed results. I am using this to copy file links into OmniFocus, Mainly pdf files. Sometimes it works and the file will open in the Preview app and other times it just opens Finder and highlights the file? Can someone help as I would like the links to open with the default apps.

I've noticed that too — in Mojave, file:/// URLs often just "reveal" the target file in the Finder instead of opening it. Not a big deal since you can then just ⌘O to open it, but still. Anyway, I'm pretty sure it's not a problem with the macro. Probably something to do with the new security features.

1 Like

For the past couple years, I had been happily using this script on a regular basis, until I recently realized that some applications (such as Obsidian) don't always handle file:/// links properly, unless they are enclosed in <angle brackets>.

After a bit of tinkering, I figured out how to modify the script accordingly:

(function () {
    'use strict';

    var strFrontApp = Application("System Events")
        .applicationProcesses.whose({
            frontmost: true
        })
        .at(0)
        .name(),
        blnFinder = ["Finder", "Path Finder"].indexOf(strFrontApp) !== -1;

    if (blnFinder) {
        var app = Application(strFrontApp),
            lstSeln = app
            .selection(),
            lstURL = (lstSeln && lstSeln.length) ? lstSeln
            .map(function (x) {
                return x.url();
            }) : [];

        if (lstURL.length) {

            var URLsInAngleBrackets = "<" + lstURL.join(">\n<") + ">";

            var sa = (app.includeStandardAdditions = true, app)

            sa.setTheClipboardTo(URLsInAngleBrackets);
            return URLsInAngleBrackets;
        }
    }

})();

So maybe this modified version of the script is useful to someone else, who, like me, is too stubborn to just add the angle brackets via Keyboard Maestro, and instead insists on modifying the actual script, even though their JavaScript skills are somewhat rudimentary. :grinning:

1 Like

Good work : -)

Bear in mind, of course, that one user's 'proper' will always be another user's 'improper' :slight_smile:

Some will need the file just 'found' in Finder, and will not expect or want it to be involuntarily or unexpectedly 'opened' by a default application.

Good to have the option.