Script to insert variable text in note title

I clipped a bunch of notes from Kindle books and edited the titles in order to create a summary that made more sense to me. However, I just realized that I want to add the Kindle location number to the title for each clip. I’m hoping that it’s possible to script this because the idea of manually updating about 1,300 clips is a bit daunting.

The notes currently end with the text “Read more at location XXXX” (the location is between two and four digits). I’d like to modify the note title so it goes from “Original Title” to “XXXX: Original Title”. Is there a way to do this? I’d imagine there’s a workflow that would use a regex to extract the location number from the text and then modify the title to insert the number captured from the regex. I would imagine I could use the add Prefix script for the last part, but I don’t know how to pipe a variable into that script or how to get the contents of a note in order to feed it into a regex.

Please post an example of the exact syntax of the title and whether it’s consistently applied to all notes. Thanks.

Hi Jim,

I’m not sure what you’re asking for. If the original title was “Original Title” and the location for that clipping was 203 (so the note body ends with the text “Read more at location 203”), then I want the new title to be “203: Original Title”. Is that what you are asking?

Ahh… the location is in the body, not the title. I missed that.

This will work on a selection of files, not selecting a Group. I would also testing this out on a handful first to make sure you’re getting the expected results.

tell application id "DNtp"
	repeat with thisRecord in (selection as list)
		tell thisRecord
			set lastLine to (do shell script "echo " & (quoted form of (plain text as string)) & " | sed -n '$p'")
			if lastLine contains "Read more at location" then
				set name to (do shell script "echo " & lastLine & "  | sed -E 's_^.* ([0-9]{2,4}).*_\\1:" & (name) & "_'")
			end if
		end tell
	end repeat
end tell

Save it into ~/Library/Application Support/DEVONthink Pro 2/Scripts/ and whatever subfolder you choose if you think you’d run it again later. If not, just save it to your Desktop temporarily.

PS: There’s no Undo on this. Just sayin’

Excellent, I will test this out. Thank you so much!

No problem. :smiley: