script to split RTF files at delimiter

I’ve tried my best and failed at adapting one of the existing scripts that were posted for splitting RTF files. So far as I can tell the only scripts for splitting RTF files into many component files have the split occurring at the beginning of each paragraph. I’d very much appreciate it if someone would contribute a script for splitting an RTF into new files at an arbitrary delimiter (e.g., #$%). Even better, it would be nice if the titles of the corresponding new files were taken from text between the delimiter and line break.

In my case, I have a large chronology file made of of items like this:
1934.05.06 The Event Title.
A description of the event goes here.
1934.09.05 Another Event Title.
Another description here.

It would be great if the first resulting file was titled “1934.05.06 The Event Title.” with the content with the title + description or just the description.

I hope I’m describing this clearly.

Thank you!

I’ve modified a script by Korm to do that. Just set your choosen delimiter in the property at the top.
Careful: if the delimiter is on a line by its own you need to add the return at the end as well!```
– Explode a document into multiple documents
– Place the exploded pieces into a group
– Link the original (unexploded) document to that group
– Adapted by alastor933 from a script by Korm
Explode and group a note (split & title 2)

property delim : “” – enter your delimiter between the quotes

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”
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

	set theText to text of think window 1
	set {TIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, delim}
	set listSections to text items of theText
	set AppleScript's text item delimiters to TIDs
	
	repeat with cSection in the listSections
		if length of cSection ≠ 0 then
			set textTitle to paragraph 1 of cSection
			set textContent to paragraphs 2 thru end of cSection
			create record with {name:textTitle, content:textContent, URL:selectionURL, type:rtf} in targetGroup
		end if
	end repeat
	
end repeat

end tell

This script doesn’t seem to work for me. It creates the new group but it’s empty. I tried ## as the delimiter and ABC as the delimiter but neither worked.

Does it work for you?

It doesn’t work. There’s no error checking in the script, so it fails silently, but it fails because it is attempting to read portions of the text that do not exist. I’ve highlighted the error where the interpreter fails, but the author will need to fix the out of bounds problem.

Agree does not work!

The delimiter is not to blame. 'T was me. Sorry for the rushed job.
This version corrects the problem, and catches errors:```
– Extract multiple text sections
– Place the extracted pieces into a group
– Link the original (unexploded) document to that group
– Adapted by alastor933 from a script by Korm
Explode and group a note (split & title 2)

property delim : “” – enter your delimiter between the quotes

tell application id “com.devon-technologies.thinkpro2”
try
set theDatabase to current database
set theGroup to current group
set theseItems to the selection
if selection is {} then error “Please select some contents”
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

		set theText to text of think window 1
		set {TIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, delim}
		set listSections to text items of theText
		set AppleScript's text item delimiters to TIDs
		
		repeat with cSection in listSections
			if length of cSection ≠ 0 then
				set textTitle to paragraph 1 of cSection
				set textContent to paragraphs 2 thru end of cSection
				set {TIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
				set textContent to textContent as text
				set AppleScript's text item delimiters to TIDs
				create record with {name:textTitle, rich text:textContent, type:rtf} in theGroup
			end if
		end repeat
	end repeat
	
on error errMsg number errNum
	if errNum is not -128 then display alert "DEVONthink" message errMsg as warning
end try

end tell

This version works except if there are images within the RTF they are not carrried over to the split documents.

The error handling routine does not work and therefore the script will continue to fail silently.

It’s a good practice to not wrap code in a “try” block until the script has been debugged successfully.

Including images / attachments might be difficult since they are located within attribute runs, and the script breaks attribute runs in the middle (potentially). Would need deeper logic to parse attribute runs for attachment position within the run. This is, by the way, why exploding by paragraph boundry is easier since that boundary and the attribute run elements in are congruent.

Yes. Darn. The pnameScript variable should have been a literal.
It’s mended now.

Transferring anything but text is only possible with UI scripting, as far as I know, which means working on selected text. So in this case, impossible, as it is not possible to move a text selection from AppleScript. Again, as far as I know. I see nothing in DTP’s Extended Text Suite that would help.