Image from clipboard

I often want to clip an image from a document, place it in a specific group, name it with the image caption, and insert the URL to the page of the document where I got it from. Doing this manually slows me down quite a lot.

I’m wondering if there is a script to grab an image from the clipboard, name it with the currently selected text in the active document (caption), and insert URL to the page of the active document? It would also be great if you could decide where to place the image with the “Groups & Tags” window.

Anyone know?

I’d think the fastest way to get all the data you mentioned, plus choosing the destination group, is doing a PDF or WebArchive clip using the clip to DEVONthink extension.

If you just want to see the image and caption, you can later go back and crop the PDF in Preview to show just the image and its caption.

Another route is to use the Evernote clipper - it grabs most of the data (not the caption) but then you have to get it out of Evernote. :confused:

Hello again,
I’ve kept doing it manually for now, but it is getting frustrating since I do this quite often.

Just to clarify, the document I clip the image (with SnagIt) from is always a PDF document open in DevonThink.

Korm, I think you wrote a script that basically does the same thing but with text to RTF. It takes the currently selected text in a PDF document and creates a RTF document with a link to the page of the selected text, opening the “Groups & Tags” window for placement. This is the script:

-- Save selected text as RTF
-- Experimental: 20120829 revised Use at your own risk.  Loss of data is your risk.
-- Select some text in a document in DTPO
-- Script creates an RTF whose URL is a link that page of that document
-- Does not open the document with the text highlighted
-- When clicked in DT, opens the document in a new window (open in tabs seems not work)

-- the default new file name is the same as the source file; a default prefix and/or suffix can be defined here

--set o_Page to " (pg. " & the (((current page of think window --1) + 1) as string) & " )"
--		set theName to prefix & name of thisItem & o_Page & --suffix
property suffix : ""
property prefix : ""
property askName : false

tell application id "com.devon-technologies.thinkpro2"
	
	set theSelection to selection
	if theSelection is {} then
		error "Please Select Something"
	end if
	repeat with thisItem in theSelection
		
		set theText to selected text of think window 1
		
		if theText is {} then
			error "Select some text"
		end if
		set theName to prefix & theText & suffix
		if askName then
			set theName to the text returned of (display dialog "File name" default answer theName)
		end if
		
		if the current page of think window 1 ≠ -1 then
			set thePage to "?page=" & ((the current page of think window 1) as string)
		end if
		
		set theUrl to reference URL of thisItem & thePage
		-- if a hardcoded destination is desired, change the following line
		create record with {type:rtf, name:theName, URL:theUrl, rich text:theText} in display group selector
	end repeat
end tell

This simple script should work as desired:


tell application "DEVONthink Pro"
	try
		if not (exists content record) then error "Please open a document."
		set theSource to reference URL of content record
		set theText to selected text of think window 1
		
		set theGroup to display group selector "Destination"
		set theRecord to paste clipboard to theGroup
		set URL of theRecord to theSource
		
		try
			set name of theRecord to theText
		end try
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
	end try
end tell

This is a little riff on Christian’s script – this version checks to see if there is an image on the clipboard, and fails if not.

(*
	See https://discourse.devontechnologies.com/t/image-from-clipboard/14815/1
	adjusted by korm to check that clipboard contents are an image
	(Note: "TIFF picture" does not mean the image is a TIFF; 
	it's a "clipboard info" convention)
*)

tell application "DEVONthink Pro"
	set whatIs to (item 1 of item 1 of ((clipboard info) as list)) as string
	if whatIs = "TIFF picture" then
		try
			if not (exists content record) then error "Please open a document."
			set theSource to reference URL of content record
			set theText to selected text of think window 1
			
			set theGroup to display group selector "Destination"
			set theRecord to paste clipboard to theGroup
			set URL of theRecord to theSource
			
			try
				set name of theRecord to theText
			end try
		on error error_message number error_number
			if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
		end try
	else
		display dialog "No image on clipboard"
	end if
end tell

Works great - Thanks a lot!

Hello and thanks again for your efforts,

I’m now wondering if it would be possible to choose tags for the file on creation? This way I wouldn’t have to go back and tag them but instead do it on the spot.

Could this also be done for the RTF script that korm was kind enough to provide for me? (New rtf doc from selected text with URL to pdf)

It’s possible with both scripts (after a little pause for real work 8) ), but a question first: “choose tags” means what? Pick tags from a list of tags (not so simple) or just type in new tags at a prompt?

If the prompt would give popup suggestions for tags as you type (like it normally does when tagging files) that would be great. The ideal would be, in addition to the prompt, also seeing existing tags in a window like the Groups & Tags but I have no insight into the difficulty of this. :slight_smile:

Popup tag completion dialogs aren’t going to happen. Sorry. Way more time than I care to take.

Absolutely, I understand. Is something simpler possible?

An alternative that might actually work better is opening the newly created image/rtf.

On the same note: is it possible to place an item link to the newly created image/rtf on the clipboard?