In DEVONthink I can get the URL of a document via Edit --> Copy Item Link. Additionally I can get the Name of the documentvia Clicking on the selected Item and then copying the pre-selected text of the Name.
Then I can assemble both to a Markdown link of the form
![<Name> ](<Item Link>)
to paste it into another Markdown document. Is there a direct way to gather this Markdown snippet? After all both operations (getting the URL and getting the name) operate on the same item…
-- Copy record link(s) as markdown link(s)
tell application id "DNtp"
try
set windowClass to class of window 1
if {viewer window, search window} contains windowClass then
set currentRecord_s to selection of window 1
else if windowClass = document window then
set currentRecord_s to content record of window 1 as list
end if
if currentRecord_s = {} then
display notification "Nichts ausgewählt!" with title "DEVONthink"
return
end if
if (count of currentRecord_s) = 1 then
set theRecord to item 1 of currentRecord_s
set theMarkdownLink to "[" & (name of theRecord) & "](" & (reference URL of theRecord) & ")"
set the clipboard to theMarkdownLink
display notification theMarkdownLink with title "Markdown-Link kopiert"
else
set theMarkdownLinksString to ""
set theCount to 0
repeat with thisRecord in currentRecord_s
set thisMarkdownLink to "[" & (name of thisRecord) & "](" & (reference URL of thisRecord) & ")"
set theCount to theCount + 1
if theCount ≠ (count of currentRecord_s) then
set theMarkdownLinksString to theMarkdownLinksString & thisMarkdownLink & return
else
set theMarkdownLinksString to theMarkdownLinksString & thisMarkdownLink
end if
end repeat
set the clipboard to theMarkdownLinksString
display notification theMarkdownLinksString with title "Markdown-Links kopiert"
end if
on error error_message number error_number
if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
end try
end tell
-- Copy record link(s) as markdown link(s)
-- Updated for DEVONthink 3.6
tell application id "DNtp"
try
set currentRecord_s to selected records
if currentRecord_s = {} then
display notification "Nichts ausgewählt!" with title "DEVONthink"
return
end if
if (count of currentRecord_s) = 1 then
set theRecord to item 1 of currentRecord_s
set theMarkdownLink to "[" & (name of theRecord) & "](" & (reference URL of theRecord) & ")"
set the clipboard to theMarkdownLink
display notification theMarkdownLink with title "Markdown-Link kopiert"
else
set theMarkdownLinksString to ""
set theCount to 0
repeat with thisRecord in currentRecord_s
set thisMarkdownLink to "[" & (name of thisRecord) & "](" & (reference URL of thisRecord) & ")"
set theCount to theCount + 1
if theCount ≠ (count of currentRecord_s) then
set theMarkdownLinksString to theMarkdownLinksString & thisMarkdownLink & return
else
set theMarkdownLinksString to theMarkdownLinksString & thisMarkdownLink
end if
end repeat
set the clipboard to theMarkdownLinksString
display notification theMarkdownLinksString with title "Markdown-Links kopiert"
end if
on error error_message number error_number
if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
end try
end tell
I took the liberty to translate this one into JavaScript. It’s instructive because it shows two opposing tendencies: compactness and chattiness.
The relevant code is much shorter than the AppleScript equivalent. That’s mostly because it doesn’t differentiate between one and more than one selected record(s) (cf. the forEach loop). On the other hand, it goes through completeley useless hoops with curApp vs. app, because displayNotificication apparently only works with the currentApplication. Also, setTheClipboardTo looks really weird here, one would expect a simple clipboard= like with all properties.
For those not fluent in JavaScript: The individual MD links are saved in an array (mdLinks) which is then converted into a string with the individual links separated by a newline (mdLinks.join(\n’)`). That’s kind of a pattern in JavaScript.
(() => {
let app = Application("DEVONthink 3");
let curApp = Application.currentApplication();
app.includeStandardAdditions = true;
curApp.includeStandardAdditions = true;
let rec = app.selectedRecords();
if (!rec || rec.length === 0) {
curApp.displayNotification("Nichts ausgewählt", {withTitle: "DEVONthink"});
return;
}
let mdLinks = [];
app.selectedRecords().forEach(rec => {
let curLink = `[${rec.name()}](${rec.referenceURL()})`;
mdLinks.push(curLink);
})
let txt = mdLinks.join(`\n`);
app.setTheClipboardTo(txt);
curApp.displayNotification(txt, {withTitle: "MD-Links kopiert"});
})()
This one is basically the same and needs 21 lines, yours needs 19.
tell application id "DNtp"
try
set currentRecord_s to selected records
if currentRecord_s = {} then
display notification "Nichts ausgewählt!" with title "DEVONthink"
return
end if
set theMarkdownLinks to {}
repeat with thisRecord in currentRecord_s
set end of theMarkdownLinks to "[" & (name of thisRecord) & "](" & (reference URL of thisRecord) & ")"
end repeat
set d to AppleScript's text item delimiters
set AppleScript's text item delimiters to linefeed
set theMarkdownLinks_string to theMarkdownLinks as text
set AppleScript's text item delimiters to d
set the clipboard to theMarkdownLinks_string
display notification theMarkdownLinks_string with title "Markdown-Link(s) kopiert"
on error error_message number error_number
if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
end try
end tell
They’d be equal again if I’d remove the one line about standard additions and the assignment to curLink. Regardless, I livke the (un)wrapping of the string with an array a lot more than the complicated loop in your first version
That’s an quite old script I never looked into again and just posted when halloleo asked. Obviously I didn’t know a better way back then. Yes, it’s very complicated
As I have to update some scripts here anyway due to DEVONthink 3.6’s new reporting of errors for invalid syntax I’ll clean up such things in one go. I didn’t stop to use “search windows” in a lot of scripts in order to keep them useable with DEVONthink 2 - now, due to the new error reporting, they all fail However in general the new behaviour is very welcome.
tell application id "DNtp"
set theMarkdownLinks to {}
repeat with thisRecord in (selected records)
copy ("[" & (name of thisRecord) & "](" & (reference URL of thisRecord) & ") " & return) to end of theMarkdownLinks
end repeat
if theMarkdownLinks ≠ {} then
display notification ((count items of theMarkdownLinks) & " Markdown links copied" as string) with title "Markdown-Link(s) kopiert"
set the clipboard to (theMarkdownLinks as string)
end if
end tell
How do I add this script in my Devonthink? (Sorry, I’m a newbie at this). And, once added, can I add a keyboard shortcut to run the script? (I assume I need to have an item selected first). Thanks!
Select Script > Compile to ensure it’s compiling properly.
Select File > Save.
In the Save dialog, press Command-Shift-G and paste ~/Library/Application Scripts/com.devon-technologies.think3/Menu. You can save into that directory or a subfolder of your choice.
Give the script your desired name and save it. The script should now be available in the Scripts menu in DEVONthink.
Yes, this script only works on a selection of items in DEVONthink’s item list.
Regarding a shortcut, please check out this blog post…