# Copy text from one document and pasting it with reference

**URL:** <https://discourse.devontechnologies.com/t/copy-text-from-one-document-and-pasting-it-with-reference/63504>\
**Category:** DEVONthink\
**Created:** [April 5, 2021, 4:46pm UTC](https://discourse.devontechnologies.com/t/copy-text-from-one-document-and-pasting-it-with-reference/63504 "2021-04-05T16:46:11Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michaell](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/m/8e8cbc/32.png) [@Michaell](https://discourse.devontechnologies.com/u/Michaell)\
**Post date:** [April 5, 2021, 4:46pm UTC](https://discourse.devontechnologies.com/t/copy-text-from-one-document-and-pasting-it-with-reference/63504/1 "2021-04-05T16:46:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DTLow](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/dtlow/32/18161_2.png) [@DTLow](https://discourse.devontechnologies.com/u/DTLow)\
**Post date:** [April 5, 2021, 4:50pm UTC](https://discourse.devontechnologies.com/t/copy-text-from-one-document-and-pasting-it-with-reference/63504/2 "2021-04-05T16:50:34Z")

</div>

Interesting idea; this could be scripted

---

<div class="post-metadata">

**Author:** ![pete31](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/p/d26b3c/32.png) [@pete31](https://discourse.devontechnologies.com/u/pete31)\
**Post date:** [April 5, 2021, 9:44pm UTC](https://discourse.devontechnologies.com/t/copy-text-from-one-document-and-pasting-it-with-reference/63504/3 "2021-04-05T21:44:30Z")

</div>

> [@Michaell](#):
>
> copy text from one document in DTP and pasting it into another document in DTP

Copy from what? Paste in what?

Plain Text? Markdown? RTF?

---

<div class="post-metadata">

**Author:** ![Michaell](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/m/8e8cbc/32.png) [@Michaell](https://discourse.devontechnologies.com/u/Michaell)\
**Post date:** [April 6, 2021, 12:55pm UTC](https://discourse.devontechnologies.com/t/copy-text-from-one-document-and-pasting-it-with-reference/63504/4 "2021-04-06T12:55:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pete31](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/p/d26b3c/32.png) [@pete31](https://discourse.devontechnologies.com/u/pete31)\
**Post date:** [April 6, 2021, 2:49pm UTC](https://discourse.devontechnologies.com/t/copy-text-from-one-document-and-pasting-it-with-reference/63504/5 "2021-04-06T14:49:49Z")

</div>

This script copies selected PDF text and a Markdown link.

Change property `theDelimiter` if you want the Markdown link e.g. below the text.

```auto
-- 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

```

---

<div class="post-metadata">

**Author:** ![Michaell](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/m/8e8cbc/32.png) [@Michaell](https://discourse.devontechnologies.com/u/Michaell)\
**Post date:** [April 6, 2021, 5:38pm UTC](https://discourse.devontechnologies.com/t/copy-text-from-one-document-and-pasting-it-with-reference/63504/6 "2021-04-06T17:38:17Z")

</div>

Wow great! Thanks a lot!

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

---

<div class="post-metadata">

**Author:** ![pete31](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/p/d26b3c/32.png) [@pete31](https://discourse.devontechnologies.com/u/pete31)\
**Post date:** [April 6, 2021, 5:50pm UTC](https://discourse.devontechnologies.com/t/copy-text-from-one-document-and-pasting-it-with-reference/63504/7 "2021-04-06T17:50:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DTLow](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/dtlow/32/18161_2.png) [@DTLow](https://discourse.devontechnologies.com/u/DTLow)\
**Post date:** [April 6, 2021, 6:09pm UTC](https://discourse.devontechnologies.com/t/copy-text-from-one-document-and-pasting-it-with-reference/63504/8 "2021-04-06T18:09:09Z")

</div>

> [@Michaell](#):
>
> Where do I put this code?

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](https://devontech-discourse.s3.dualstack.us-east-1.amazonaws.com/uploads/original/3X/c/d/cd7d9d410a8d7ed1381b506d3e29a0f7e0b7e629.png)

---

<div class="post-metadata">

**Author:** ![Michaell](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/m/8e8cbc/32.png) [@Michaell](https://discourse.devontechnologies.com/u/Michaell)\
**Post date:** [April 6, 2021, 6:13pm UTC](https://discourse.devontechnologies.com/t/copy-text-from-one-document-and-pasting-it-with-reference/63504/9 "2021-04-06T18:13:41Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![pete31](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/p/d26b3c/32.png) [@pete31](https://discourse.devontechnologies.com/u/pete31)\
**Post date:** [April 6, 2021, 6:19pm UTC](https://discourse.devontechnologies.com/t/copy-text-from-one-document-and-pasting-it-with-reference/63504/10 "2021-04-06T18:19:17Z")

</div>

> [@Michaell](#):
>
> I select text in a pdf file and laste it to a markdown doc - and nothing happens

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?

---

<div class="post-metadata">

**Author:** ![Michaell](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/m/8e8cbc/32.png) [@Michaell](https://discourse.devontechnologies.com/u/Michaell)\
**Post date:** [April 6, 2021, 6:21pm UTC](https://discourse.devontechnologies.com/t/copy-text-from-one-document-and-pasting-it-with-reference/63504/11 "2021-04-06T18:21:34Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![pete31](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/p/d26b3c/32.png) [@pete31](https://discourse.devontechnologies.com/u/pete31)\
**Post date:** [April 6, 2021, 6:23pm UTC](https://discourse.devontechnologies.com/t/copy-text-from-one-document-and-pasting-it-with-reference/63504/12 "2021-04-06T18:23:49Z")

</div>

> [@Michaell](#):
>
> When I then paste cmd-v

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

> [@pete31](#):
>
> Note: In DEVONthink 3.6.2 you have to paste via menu `Edit > Paste and Match Style`.

What’s the extension of the script you saved?

Please restart DEVONthink and try again.

---

<div class="post-metadata">

**Author:** ![Michaell](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/m/8e8cbc/32.png) [@Michaell](https://discourse.devontechnologies.com/u/Michaell)\
**Post date:** [April 6, 2021, 6:28pm UTC](https://discourse.devontechnologies.com/t/copy-text-from-one-document-and-pasting-it-with-reference/63504/13 "2021-04-06T18:28:22Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![pete31](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/p/d26b3c/32.png) [@pete31](https://discourse.devontechnologies.com/u/pete31)\
**Post date:** [April 6, 2021, 6:36pm UTC](https://discourse.devontechnologies.com/t/copy-text-from-one-document-and-pasting-it-with-reference/63504/14 "2021-04-06T18:36:01Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![DTLow](https://discourse.devontechnologies.com/user_avatar/discourse.devontechnologies.com/dtlow/32/18161_2.png) [@DTLow](https://discourse.devontechnologies.com/u/DTLow)\
**Post date:** [April 6, 2021, 6:40pm UTC](https://discourse.devontechnologies.com/t/copy-text-from-one-document-and-pasting-it-with-reference/63504/15 "2021-04-06T18:40:11Z")

</div>

> [@Michaell](#):
>
> I selected the text and then cllcked the script.

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

---

<div class="post-metadata">

**Author:** ![Michaell](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/m/8e8cbc/32.png) [@Michaell](https://discourse.devontechnologies.com/u/Michaell)\
**Post date:** [April 6, 2021, 6:41pm UTC](https://discourse.devontechnologies.com/t/copy-text-from-one-document-and-pasting-it-with-reference/63504/16 "2021-04-06T18:41:13Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![pete31](https://discourse.devontechnologies.com/letter_avatar_proxy/v4/letter/p/d26b3c/32.png) [@pete31](https://discourse.devontechnologies.com/u/pete31)\
**Post date:** [April 7, 2021, 2:57pm UTC](https://discourse.devontechnologies.com/t/copy-text-from-one-document-and-pasting-it-with-reference/63504/17 "2021-04-07T14:57:51Z")

</div>

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