First attempt of Readwise Script

It took me some days but I finally finished my Readwise script to split notes up into highlights and keep the metadata. To use this script you have to export all your highlight from Readwise via “Markdown” export and keep all switches off:

I am planning to include some extra features like locations but know it works for me. Every highlight is a standalone markdown file with the title as name plus an incrementing number.

Use it on your own risk and maybe try with test data first:

-- Main: Collect Markdown files and process their content in DEVONthink
tell application id "DNtp"
	-- Prompt to select a group
	set selectedFolder to display group selector "Select a group in DEVONthink:"
	
	-- Collect Markdown files recursively and process them
	my processItems(children of selectedFolder, selectedFolder)
end tell

-- Recursive function to process items
on processItems(selItems, targetFolder)
	tell application id "DNtp"
		repeat with currentItem in selItems
			if type of currentItem is group then
				-- Process children of subgroups
				my processItems(children of currentItem, currentItem)
			else if type of currentItem is markdown then
				-- Process the Markdown file content
				my processMarkdown(plain text of currentItem, targetFolder)
				delete record currentItem -- Optionally delete the original record
			end if
		end repeat
	end tell
end processItems

-- Process Markdown content
on processMarkdown(varText, targetFolder)
	-- Initialize variables
	set {entryAuthor, entryTitle, entryURL, currentHighlight, metadata} to {"", "", "", "", ""}
	set {isHighlight, skipEntry} to {false, false}
	set noteHighlights to {}
	set counter to 1
	
	-- Extract metadata and highlights
	repeat with anEntry in paragraphs of varText
		if skipEntry then
			set skipEntry to false
			-- Skip current entry, go to the next
		else if anEntry starts with "- Author:" and not isHighlight then
			set entryAuthor to text ((length of "- Author: ") + 1) thru -1 of anEntry
		else if anEntry starts with "- Full Title:" and not isHighlight then
			set entryTitle to text ((length of "- Full Title: ") + 1) thru -1 of anEntry
		else if anEntry starts with "- URL:" and not isHighlight then
			set entryURL to text ((length of "- URL: ") + 1) thru -1 of anEntry
		else if anEntry starts with "### Highlights" then
			set isHighlight to true
			set skipEntry to true
		else if isHighlight then
			-- Handle highlights, including multi-line entries
			if anEntry starts with "- " then
				if currentHighlight is not "" then set end of noteHighlights to currentHighlight
				set currentHighlight to text ((length of "- ") + 1) thru -1 of anEntry
			else
				set currentHighlight to currentHighlight & linefeed & anEntry
			end if
		end if
	end repeat
	
	-- Add the last highlight (if any)
	if currentHighlight is not "" then set end of noteHighlights to currentHighlight
	
	-- Construct metadata
	if entryTitle is not "" then set metadata to metadata & "title: " & entryTitle & linefeed
	if entryAuthor is not "" then set metadata to metadata & "author: " & entryAuthor & linefeed
	if entryURL is not "" then set metadata to metadata & "URL: " & entryURL & linefeed
	
	-- Create and save notes as Markdown files
	tell application id "DNtp"
		repeat with anEntry in noteHighlights
			set varNote to metadata & linefeed & anEntry
			set mdFileName to (entryTitle & "_" & counter)
			create record with {name:mdFileName, type:markdown, plain text:varNote} in targetFolder
			set counter to counter + 1
		end repeat
	end tell
end processMarkdown
1 Like

Thanks for sharing your approach.

“Select a folder in DEVONthink:”

It’s actually a group :wink: but no harm, no foul.


Do you have a few example documents you’re processing?

1 Like

Here is an example Markdown-File:

# 1 Simple Technique to Better Visualize

![](https://readwise-assets.s3.amazonaws.com/static/images/article4.6bc1851654a0.png)

### Metadata

- Author: lifehack.org
- Full Title: 1 Simple Technique to Better Visualize
- Category: #articles


- Document Tags: #Edited  
- URL: https://www.lifehack.org/446642/1-simple-technique-to-visualize-better

### Highlights

- the brain doesn’t know the difference between physically doing something and imagining it in your mind.
- Visualizing yourself as an entrepreneur will be much easier if you do something about your business than nothing at all.

They are all formatted accordingly if all switches are off. Later I may include more features to get more information form those notes. Images, Tags and Categories are fully left out for the moment.

What is the benefit of having separate files (plus duplicated metadata) for each individual highlight? Are you making further notes on those highlights, or some other analysis?

Personally, I have Readwise sync all highlights (per book or article file) to Obsidian and index that block of files managed by Readwise in a DEVONthink database. Saves the hassle of rolling my own Readwise export.

Hi korm,

I like to focus on single highlights, sentences, quotes and not scrolling through a bunch of notes from a book. It is like the daily review from Readwise only in DEVONthink.

I use these numerous small notes with a “random note opener”.

1 Like