This is exactly the approach I was going to adopt in my initial script too: to separate body of each note into parts. Your example directed me to rewrite the code to make it use delimiters only.
-- Replace evernote:/// Link URLs in Formatted Notes with DEVONthink Reference URLs
-- Note: This script finds a link's corresponding record by searching the link's URL in DEVONthink's URL property.
-- Select some formatted notes first
set anchorPreUrlPart to "<a href=\""
set urlToReplacePrefix to "evernote:///view/"
set currentNoteIndex to 0 # DEBUG
tell application id "DNtp"
try
set theNotes to selected records whose type = formatted note and URL starts with urlToReplacePrefix
set recordsCount to (count of theNotes) as text # DEBUG
repeat with thisNote in theNotes
set currentNoteIndex to currentNoteIndex + 1 # DEBUG
set currentNoteSource to source of thisNote
if currentNoteSource contains (anchorPreUrlPart & urlToReplacePrefix) then
log "========= Processing note " & (currentNoteIndex as text) & " of " & recordsCount & ":: " & (name of thisNote) as text # DEBUG
set AppleScript's text item delimiters to {anchorPreUrlPart}
set delimitedList to every text item of currentNoteSource
repeat with delimitedSource in delimitedList
if delimitedSource starts with urlToReplacePrefix then
set AppleScript's text item delimiters to {"\""}
set evernoteUrlToReplace to the first text item of delimitedSource
set theResults to search "type:formattednote url==" & evernoteUrlToReplace
if theResults ≠ {} then
set thisResult to the first item of theResults
set thisResult_ReferenceURL to reference URL of thisResult
log (evernoteUrlToReplace & " -> " & (name of thisResult) as text) & " => " & thisResult_ReferenceURL as text # DEBUG
set currentNoteSource to my findAndReplaceInText(currentNoteSource, evernoteUrlToReplace, thisResult_ReferenceURL)
else # DEBUG
log "No counterparts found with url: " & evernoteUrlToReplace as text # DEBUG
end if
end if
end repeat
set source of thisNote to currentNoteSource
else # DEBUG
log "========= Note " & (currentNoteIndex as text) & " of " & recordsCount & " doesn't seem to have any Evernote urls:: " & (name of thisNote) as text # DEBUG
end if
end repeat
on error error_message number error_number
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
This code based on splitting only. Still incurs 38 Gb memory leak when executed by Script Debugger app. It’s less than 1/2 of the previous result keeping in mind that after leak of 80 Gb my mac halted on about 1/2 of the records. And it runs through all of 4260 records in 27 minutes 06 seconds (4 times faster) and successfully finishes.
UPD1. set recordsCount to (count of theNotes) as text
line moved out from the cycle.