Create org-mode link from Markdown Link

I have been using this wonderful Macro by @ComplexPoint - Copy markdown link

I wanted to make a slightly different version to create Org-mode links which use the format [[link][title]]

I have changed for the Browser URL from

Right (
    `[${tab.name()}](${tab.url()})`
      );

into

Right(
    `[[${Tab.url()}][${tab.name()}]]`
);

And the other only line that looks like it is creating a markdown link

  Right(`[${winTitle}](${maybeDocURL})`)

I have changed like this

 Right(`[[${maybeDocURL}][${winTitle}]]`)

The one for the URL works and creates the link I needed, but for the app links, nada, it still returns a markdown link.

I discovered I am totally underqualified to make the changes and wonder if anyone had any idea how to go about creating links in the format [[link][title]].

/R

(edited as I submitted before I was finished writing)

Haven't taken a close look yet, but JS names are case-sensitive,
so tab.name() vs Tab.name() looks like one thing to fix.

(I think you need lower-case there)

OK, the code you have been trying to edit looks like it may come from one of the 35+ app-specific sub-macros in the bundle.

What you need is an additional macro:

  • based on a copy of the main Copy As Markdown link macro within that bundle,
  • but with a different name and key-stroke
  • and with an edit to the JXA action labelled Copy to clipboard both as MD link and as RTF labelled hyperlink

There's a function in the code of that action called mdLinkParts which:

  • takes an MD link coming in from any of the 30+ app-specific sub-macros
  • and returns a pair of values (labelString, urlString)

We could:

  1. build an org-mode link from that separated pair of values, and
  2. place it in the clipboard as plain text

You can experiment yourself, of course, but otherwise, I will aim to write an Org-mode addition to the bundle over the weekend.

In the meanwhile, here is a first sketch of a macro to add to the existing MD Link tools group.

Copy as OrgMode link.kmmacros.zip (13.6 KB)

(It isn't stand-alone, and relies on the other submacros to interact with browsers and other apps)

It includes a JS action called Copy to clipboard as OrgMode link, with the following source code:

Expand disclosure triangle to view JS Source
(() => {
    "use strict";

    // Rob Trew @2021
    // Ver 0.1

    // Orgmode [[link][label]]
    // derived from KM mdLink variable
    // and placed in clipboard.

    ObjC.import("AppKit");

    // main :: IO ()
    const main = () => {
        const
            kme = Application("Keyboard Maestro Engine"),
            kv = mdLinkParts(
                kme.getvariable("mdLink")
            ),
            orgLink = `[[${kv[1]}][${kv[0]}]]`;

        return (
            kme.setvariable("orgLink", {
                to: orgLink
            }),
            copyTypedString(true)(
                "public.utf8-plain-text"
            )(
                orgLink
            )
        );
    };


    // ---------------------- LINKS ----------------------

    // copyTypedString :: Bool -> String -> String -> IO ()
    const copyTypedString = blnClear =>
        // public.html, public.rtf, public.utf8-plain-text
        pbType => s => {
            const pb = $.NSPasteboard.generalPasteboard;

            return (
                blnClear && pb.clearContents,
                pb.setStringForType(
                    $(s),
                    $(pbType)
                ),
                s
            );
        };


    // mdLinkParts :: String -> (String, String)
    const mdLinkParts = s => {
        const ab = s.trim().split("](");

        return 2 !== ab.length ? (
            Tuple(s)("")
        ) : Tuple(ab[0].slice(1))(
            ab[1].slice(0, -1)
        );
    };


    // --------------------- GENERIC ---------------------

    // Tuple (,) :: a -> b -> (a, b)
    const Tuple = a =>
        b => ({
            type: "Tuple",
            "0": a,
            "1": b,
            length: 2
        });

    return main();
})();

Update:

I've also added that draft macro to the main macro group at:

Copy as Markdown Link - Macro Library - Keyboard Maestro Discourse

Thank you! Works a charm. I had totally missed the second script...

1 Like