I was wondering wether it was possible to copy text from one document in DTP and pasting it into another document in DTP, and the pasted text contains a reference/backlink/file name of the file from which the text was originally copied.
This script copies selected PDF text and a Markdown link.
Change property theDelimiter if you want the Markdown link e.g. below the text.
-- Copy selected PDF text and Markdown Link
use AppleScript version "2.4"
use framework "AppKit"
use scripting additions
property useSuffix : true
property theDelimiter : space -- or e.g. linefeed & linefeed
tell application id "DNtp"
try
if not (exists think window 1) then
error "Please open a window"
else
set theWindow to think window 1
end if
set theRecord to content record of theWindow
if theRecord ≠ missing value then
set theType to (type of theRecord) as string
if theType is in {"PDF document", "«constant ****pdf »"} then
try
set theSelectedText to selected text of theWindow & "" as string
on error
error "Please select some text"
end try
set thePage to current page of theWindow
set theRefURL to reference URL of theRecord & "?page=" & thePage
if useSuffix = true then
set theName to name of theRecord
if theName does not end with ".pdf" then set theName to theName & ".pdf"
else
set theName to name without extension of theRecord
end if
set theMarkdownLink to "[" & theName & "](" & theRefURL & ")"
set theQuote to theSelectedText & theDelimiter & "(" & theMarkdownLink & ")"
my setClipboardToPlainText(theQuote) -- necessary in macOS Mojave
display notification "Copied"
else
error "Please open a PDF record"
end if
else
error "Please open a PDF record"
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
return
end try
end tell
on setClipboardToPlainText(theText)
set thePasteboard to current application's NSPasteboard's generalPasteboard()
thePasteboard's clearContents()
(thePasteboard's setString:theText forType:(current application's NSPasteboardTypeString))
end setClipboardToPlainText
Thanks a lot! So I created a new folder and saved the script as CopyRef.scpt.
I select text in a pdf file and laste it to a markdown doc - and nothing happens… What am I doing wrong?
I selected the text and then cllcked the script. When I then paste cmd-v nothing I paste something I copied earlier, not the selected text from the pdf file…
The script has the suffix .scpt.
I Pasted via Edit > Paste and Match Style.
I restarted DTP, still nothing…
I read the script, it should give an error message when I run the sciprt with no text selected. But I don’t get any error message if nothing is selected…
No idea, sorry. Before posting it I tested the script from within DEVONthink’s script menu, works fine. While writing it I tested all error handling and this also worked fine.
Please try to copy the selected text manually and paste it into a Markdown record. Does that work?
For troubleshooting, you can bypass the script menus
Open the script in Script Editor, run by clicking the run icon
It provides direct feedback of issues
I re-checked the schippt in the script folder - and it was full of color tags and so on - pasted into textedit and saved in some kind of rtf format… My error, how stupid of me.
Now its working like a charm, thanks so much for your effort!