Copy Markdown link to current item (a script and a GUI suggestion)

To get a very useful x-scrivener-item link with an id reference to the current text/note, we can use the Scrivener menu path:

Edit > Copy Special > Copy Document as External Link

but the menu item is only enabled when:

  1. The binder panel is visible at left, and
  2. the item for which a link is needed is selected in the binder.

A suggestion (and, in the interim) a script.

Suggestion:

  • Perhaps enable copying of an x-scrivener-item link with id to whichever text/note currently has editing focus ?
  • perhaps also enable a variant which copies a full Markdown link, including a label taken from the text’s binder name ?

For example, where the binder item has the name Abstract.

[Abstract](x-scrivener-item:///Users/houthakker/Desktop/sample.scriv?id=30B2E82B-39E4-421C-B528-A1943A472A21)

Script:

In the meanwhile, here is a script (I have just posted a variant of it on the Hook forum) which aims to:

  1. Ensure that the binder is open and has focus, and
  2. copy both item and name and x-scrivener-item link to the clipboard in the Mardkown format.

Depending on your system you may (as this script depends on UI scripting) need to slightly increase the delay values in the script.

(Could be assigned to a shortcut key with Keyboard Maestro or FastScripts etc)

[code]-- Rob Trew 2020
– Ver 0.01

tell application “Scrivener” to activate
tell application “System Events”
set ps to processes where its name is “Scrivener”
if {} ≠ ps then
set menuBar to menu bar 1 of (item 1 of ps)

    -- FOCUS TO BINDER
    set focusBinder to menu item "Binder" of menu 1 of ¬
        (menu item "Move Focus To" of (menu "Navigate" of menuBar))
    if enabled of focusBinder then
        click focusBinder
    else
        -- The Show Binder / Hide Binder label state is not always quite as expected.
        click (first menu item of (menu "View" of menuBar) ¬
            where name ends with "Binder")
        delay 0.1
        click focusBinder
        delay 0.1
    end if
    
    -- NAME COPIED
    set menuEdit to (menu "Edit" of menuBar)
    click (menu item "Copy" of menuEdit)
    delay 0.2
    set strName to the clipboard
    
    -- URL COPIED
    click (menu item "Copy Document as External Link" of menu ¬
        of (menu item "Copy Special" of menuEdit))
    delay 0.2
    set mdLink to "[" & strName & "](" & (the clipboard) & ")"
    set the clipboard to mdLink
    return mdLink
else
    ""
end if

end tell[/code]

There is another context from which you can copy a link: the editor itself. If you hit the ⌃⌘R shortcut to navigate “up” from your current position, you should be viewing a corkboard or outliner with the previously edited item selected. After copying the link, ⌘[ (to go “Back” in history) is probably the easiest point of return. That’s probably the best route to scripting, since the binder may not even be open, or you may not want to use the Reveal in Binder command to force hierarchy expansion and such. If you’re in Scrivenings mode it is even easier as you can just toggle modes to get a selectable item for the edited chunk of text (easier from a human standpoint, I mean to say, the existence of Scrivenings makes scripting a bit more complicated).

Ultimately though, I do agree that it would be nice to copy a document link from within the document itself. That’s something I’ve run into a fair bit as well, and while I have methods like the above hard-wired into my fingers and often forget about it (it’s a useful sequence for a lot of different things, not just copying links), occasionally I do remember I’m taking three or four actions to do one thing.

Thanks ! I’ll take a look at that and perhaps update the script this evening.

In the meanwhile, a Keyboard Maestro macro taking the route suggested by AmberV:

Copy MD link to current Scrivener item.kmmacros.zip (11.4 KB)