Explode and group a note (split & title 2)

In this recent posting - Split and Title - Christian submitted a script in response to a request by grsilverman, who wants to explode a note at paragraph boundaries into separate notes.

For example, this could be very useful for breaking an annotation into new documents.

The script that follows takes the concept a step further by: exploding the note, numbering the exploded pieces (so that the sequence of the original can be maintained), putting the exploded pieces into their own group, placing the custom url (“item link” in DT parlance) of that group into the URL field of the original note, and placing the custom url of the original note into the URL fields of the exploded pieces. Thus allowing easy access.

Here’s one use case: explode a note. Open the note in 3 pane view. Cmd-click the URL for the source note, causing the exploded-note group to load into a tab, set View preference to “Show Details”. You’ll get a nice listing of the note names in the new tab, in which you can use quick look to view the contents of each note.

This is also possibly helpful for those who need to have multiple annotations for a document. (Drat, can’t find that thread right now - help sjk!)

-- Explode a document into multiple documents
-- Place the exploded pieces into a group
-- Link the original (unexploded) document to that group
-- Based on a submission from Christian in the scripting portion of the DT Forum 20100407
-- at http://www.devon-technologies.com/scripts/userforum/viewtopic.php?f=20&t=10676#p49988
-- a kormic creation

tell application id "com.devon-technologies.thinkpro2"
	set theDatabase to current database
	set theGroup to current group
	set theseItems to the selection
	if selection is {} then error "Please select some contents"
	tell text of think window 1
		set paranum to 1
		repeat with thisItem in theseItems
			set selectionURL to (("x-devonthink-item://" & uuid of thisItem) as string)
			set targetGroupName to "/Exploded:" & (name of thisItem as string)
			set targetGroup to create location targetGroupName in theDatabase
			set targetGroupURL to ("x-devonthink-item://" & uuid of targetGroup)
			set URL of thisItem to targetGroupURL
			
			repeat with thePara in the paragraphs
				set theContent to thePara as string
				if length of theContent is greater than 1 then
					set txtLength to (length of theContent)
					if txtLength < 45 then
						set theName to (paranum & ": " & (texts 1 through txtLength of theContent) as string)
					else
						set theName to (paranum & ": " & (texts 1 through 45 of theContent) & "..." as string)
					end if
					create record with {name:theName, content:theContent, URL:selectionURL, type:rtf} in targetGroup
					set paranum to paranum + 1
				end if
			end repeat
		end repeat
	end tell
end tell

Example of the use case:

Outstanding!

[Withdrawn; will repost the new script after it is rewritten.]

I’m trying to pull a script to number the grafs in a document out of this, but for some reason this doesn’t seem to do it:

tell application id "com.devon-technologies.thinkpro2"
	
	tell text of think window 1
		set paranum to 1
		repeat with thePara in the paragraphs
			set theContent to thePara as string
			set thePara to (paranum & ". " & theContent as string)
			set paranum to paranum + 1
			
		end repeat
	end tell
end tell

Any suggestions as to what I’m doing wrong?

This is messy, but will get you started (it will blow up if more than one item is selected):

tell application id "com.devon-technologies.thinkpro2"
	set theGroup to the current group
	set theseItems to the selection
	
	tell text of think window 1
		set paranum to 1
		set numberedVersion to ""
		repeat with thisItem in theseItems
			set theName to (name of thisItem as string)
			repeat with thePara in the paragraphs
				set theContent to thePara as string
				if the length of theContent is greater than 1 then
					set numberedVersion to numberedVersion & (paranum & ". " & theContent & return as string)
					set paranum to paranum + 1
				end if
			end repeat
		end repeat
		create record with {name:(theName & " (Numbered)"), type:rtf, rich text:numberedVersion} in theGroup
	end tell
end tell

Thank you so much!!!

George