New rtf doc from selected text with URL to pdf

Hello,

I’ve been trying to find a way of creating a new rtf document in the INBOX with selected text as title and content, and the URL being the link to the page of the pdf containing the text.

Basically similar to “Data>New>With Clipboard” but with selected text from a pdf, URL to the page, and placed in the inbox instead of the current group.

I found a script doing almost exactly what I want, only it creates a bookmark and not a rtf doc (and puts the file in the global inbox instead of current database inbox).

Is it possible to modify this script to create a rtf document, preferably in the database inbox?

-- Experimental
-- 20101019 Use at your own risk.  Loss of data is your risk.
-- Select some text in a document in DTPO
-- Script creates a bookmark (inetloc) to 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)


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 theTitle to selected text of think window 1
      if theTitle is {} then
         error "Select some text"
      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 "x-devonthink-item://" & (uuid of thisItem) & thePage
      create record with {type:bookmark, name:theTitle, URL:theUrl} in incoming group
   end repeat
end tell

I’m also wondering the exact same script as “Data>New>With Clipboard” exists, only placing the rtf in the inbox instead of current group?

Since I wrote the original (attribution is polite among coders), I’ll fix this up. Here you go:

This script will copy text from the current window of the current document and create an RTF whose name is the same as the original document. The text of the RTF is the selected text of the source document, and the URL of the RTF is a link to that page of the source document. Rather than hardcode a destination for the new RTF, I used the group selector. I find this gives the greatest flexibility in any script. Thus, DEVONthink will always ask where you want to put the file. (If you prefer a hardcoded destination (e.g., the inbox of the current database), that’s a bit more lifting than I have time for, and you’ll need to do that piece the way you want it.)

A few switches can be adjusted in the script. If you want a standard prefix and/or suffix for your RTF files, then set the properties at the head of the script. If you want to be prompted for the file name each time, then turn the askName property to true. (Askname will apply your default prefix and/or suffix before asking for the final name.)

BTW, Data > New > With Clipboard is not a script, it is an inbuilt command.

-- 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
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 & name of thisItem & 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

Brilliant, thank you!

Hopefully this isn’t too late to get advice on a few basic tweaks.

I figured out how to prompt to name the file and also how to add the file name (in my case, the source citation) to the Spotlight Comment. I then rename the file something substantive, like a summary.

  1. What code do you use to OPEN the newly-created RTF file? I’d like to take additional notes in the RTF, but currently I have to navigate to the appropriate folder to open the RTF up (and then bring the original PDF window up from behind the 3-pane view). Opening the new RTF seems simple, but the few things I’ve tried don’t work and I can’t find any other DT scripts that do this.
  2. Is there an alternative way to choose where to put the RTF file? E.g. the group selector doesn’t allow you to include tags - is there a way to call up something like the Group & Tags window?

I’m a total Applescript novice, in case you can’t tell, so any help (especially #1) would be greatly appreciated.

I don’t know what #1 means.

#2: the Group Selector is limited to, emmm, “groups”. There is no scriptable command to open Groups & Tags.

Try this

-- Save selected text as RTF
-- 20140818 -- made by korm -- adjust as your wish, but please attribute copies
-- see https://discourse.devontechnologies.com/t/new-rtf-doc-from-selected-text-with-url-to-pdf/14512/1

property suffix : ""
property prefix : ""
property askName : false

tell application id "DNtp"
	
	set thisItem to content record
	
	set theText to selected text of think window 1
	
	if theText is {} then
		error "Select some text"
	end if
	
	set theName to prefix & name of thisItem & 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
	set theDocument to create record with {type:rtf, name:theName, URL:theUrl, rich text:theText} in display group selector
	open window for record theDocument
	
end tell

Thanks very much - that’s exactly what I needed.

If any other newbies want to know how to add the file name to the Spotlight Comment, just add:
set theComment to name of thisItem.

That instruction is incomplete. The comment may be added to the record when it is created, or after, but merely creating a variable named “theComment” will achieve neither.

Use this block:

set theUrl to reference URL of thisItem & thePage
	set theComment to the name of thisItem
	set theDocument to create record with {type:rtf, name:theName, URL:theUrl, rich text:theText, comment:theComment} in display group selector
	open window for record theDocument

Oops. I forgot to mention that part - guess I know just enough to be dangerous.

If anyone also wants to add the filename (e.g. if you type in a summary of the note as your filename) to the content of the RTF, in addition to the selected text, you can include in the create record:
rich text:theName & "
" & theText

I just stumbled upon this script in a need to take many notes from lots of PDSF docs in DVTP. When I tried to run the script I get the following:

error “The variable theText is not defined.” number -2753 from “theText”

does the script need updating since this is an older thread?

(total script novice here)