Copy encoded url(s) of file(s) selected in Finder or Path Finder

taskpaper
jxa
pathfinder

#1

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;
        }
    }

})();

#2

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?


#3

Looks fine.


#4

Thanks, Rob. Just what I was looking for.


#5

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.


#6

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.