Copy text from one document and pasting it with reference

Hi all,

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.

Did anyone already have that idea?

Best regards,
Michael

Interesting idea; this could be scripted

1 Like

Copy from what? Paste in what?

Plain Text? Markdown? RTF?

I thought of copying from a pdf and pasting to text/markdown. Do you have an idea of how to achieve this?

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

Wow great! Thanks a lot!

Just a tiny little question: Where do I put this code? (Sorry…!)

  • Paste in Script Editor.app
  • Save to DEVONthink’s script folder
    ~/Library/Application Scripts/com.devon-technologies.think3/Menu

On the top menu bar, you’ll see a script icon > open scripts folder
I created a subfolder under Menu called My Scripts
Screen Shot 2021-04-06 at 05.41.23

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?

The script got error checking, so if you were using it wrong it would show an alert.

Did you just select text and copied that or did you select text and ran the script?

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…

It’s not the reason why it’s not working but

What’s the extension of the script you saved?

Please restart DEVONthink and try again.

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

1 Like

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!

Great, thanks so much again!

1 Like

Updated script. It’s not necessary anymore to use menu Edit > Paste and Match Style.