This script explodes RTF(D) records preserving images and attachments.
It removes leading blank lines, there’s no trying out necessary how many returns used together with your delimiter produce best results.
The script needs a closing delimiter at the end, if this is missing it adds one but it’s removed afterwards (there’s not really something removed from your source record, it restores the text as it was before the script ran).
There’s one important thing I recognized too late as I was too lazy to really look into at this point:
The delimiter has to be at the beginning or end of a paragraph - if not you won’t get the desired result. I’ll see if this can be easily changed.
Don’t forget to set theDelimiter
…
… and try with duplicates
Happy exploding!
-- Explode RTF(D)s at Delimiters (preserving images and attachments)
property theDelimiter : "^^^"
property trimStart : true -- remove leading blank lines
tell application id "DNtp"
try
set windowClass to class of window 1
if {viewer window, search window} contains windowClass then
set currentRecord_s to selection of window 1
else if windowClass = document window then
set currentRecord_s to content record of window 1 as list
end if
repeat with theRecord in currentRecord_s
set plainText to plain text of theRecord
if word -1 in plainText is not in theDelimiter then
tell text of theRecord
set endAttributeText to (text of attribute run -1) as string
set text of attribute run -1 to ((endAttributeText & return & theDelimiter) as string)
end tell
set addedEndDelimiter to true
else
set addedEndDelimiter to false
end if
set theName to name of theRecord
set theDelimiterCount to do shell script "echo " & quoted form of plainText & " | grep -c " & quoted form of theDelimiter
show progress indicator "Exploding... " & theName steps theDelimiterCount with cancel button
set currentDate to do shell script "date \"+%Y-%m-%d% %H.%M.%S\""
set theGroup to create location (location of theRecord) & "/" & theName & " (Exploded) " & currentDate & "/" in current database
set theAttributeRuns to {}
repeat with thisAttribute in attribute runs of text of theRecord
set end of theAttributeRuns to thisAttribute
end repeat
set thisName to ""
set theseAttributeRuns to {}
repeat with theAttributeRun in theAttributeRuns
set theAttributeText to ((text of theAttributeRun) as string)
if theAttributeText does not contain theDelimiter then
set end of theseAttributeRuns to theAttributeRun
if thisName = "" then
try
set w to first word of theAttributeText
set thisName to theAttributeText
end try
end if
else
if thisName = "" then
try
set w to first word of theAttributeText
set thisName to theAttributeText
end try
end if
step progress indicator "Creating... " & thisName
set theRecord to create record with {name:thisName, rich text:"", type:rtfd} in theGroup
tell text of theRecord
if theAttributeText = theDelimiter then
repeat with thisAttributeRun in theseAttributeRuns
make new attribute run at end with data thisAttributeRun
end repeat
else
set end of theseAttributeRuns to theAttributeRun
repeat with thisAttributeRun in theseAttributeRuns
make new attribute run at end with data thisAttributeRun
end repeat
set text of attribute run -1 to my findAndReplaceInText(theAttributeText, theDelimiter, "")
end if
if trimStart = true then
set i to 1
set cnt to number of paragraphs
repeat while i ≤ cnt
if length of ((text of paragraph i) as string) is 1 then
set text of paragraph i to ""
set cnt to cnt - 1
else
exit repeat
end if
end repeat
end if
end tell
set thisName to ""
set theseAttributeRuns to {}
end if
end repeat
if addedEndDelimiter = true then
tell text of theRecord to set text of attribute run -1 to endAttributeText
end if
hide progress indicator
end repeat
on error error_message number error_number
hide progress indicator
if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
return
end try
end tell
on findAndReplaceInText(theText, theSearchString, theReplacementString)
set AppleScript's text item delimiters to theSearchString
set theTextItems to every text item of theText
set AppleScript's text item delimiters to theReplacementString
set theText to theTextItems as string
set AppleScript's text item delimiters to ""
return theText
end findAndReplaceInText