# Script to convert Readwise highlights into a single note!

**URL:** <https://community.fibery.io/t/script-to-convert-readwise-highlights-into-a-single-note/5345>\
**Category:** Get Help\
**Created:** [October 31, 2023, 1:28pm UTC](https://community.fibery.io/t/script-to-convert-readwise-highlights-into-a-single-note/5345 "2023-10-31T13:28:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marloes](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/marloes/32/5926_2.png) [@Marloes](https://community.fibery.io/u/Marloes)\
**Post date:** [October 31, 2023, 1:28pm UTC](https://community.fibery.io/t/script-to-convert-readwise-highlights-into-a-single-note/5345/1 "2023-10-31T13:28:35Z")

</div>

Hi Guys,

I would like to pick someones brain for writing a script, unfortunately I can’t. 😅

I have a **readwise integration**. In this readwise integration I have a database called _Resources_ and a database called _Highlights_.

In the case of a book a _Resource_ contains **many highlights** (sometimes over 250). For this I want to create 1 note to which all _Text_ (= a Rich text field) from the _Highlights_ will be added. This way I basicly create a Summary.

What I need is a button which triggers a script in the _Resource_ database. I want to accomplisch this in 1 script, but for a clear explanation I have explained it in two parts.

**Part 1**

- After clicking the button, a new entity in the database _Notitie_ needs to be created.
- The following fields should be filled in with information from the source
  - Resource = Linked resource
  - Name = Resource name
  - Bron - persoon = Resource Bron - persoon
  - Tag = Resource tag
  - URL = Resource URL

**Part 2**

- A _Resource_ is linked to many _Highlights_ entities and these highlights contains a Richt text field called _Text_
- _Notitie_ also has a Rich text field, called _Omschrijving_
- I want to add _Text_ from _Hightlight_ to _Omschrijving_ in the created _Notitie_

**Important** in this is **the order** in which _Text_ from _Highlight_ should be placed in _Omschrijving_ of _Notitie_. This should be based on creation date. Otherwise, the order of my text makes no sense.

I hope there is someone who can help me with this.

Thanks in advance!

Marloes

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [October 31, 2023, 5:03pm UTC](https://community.fibery.io/t/script-to-convert-readwise-highlights-into-a-single-note/5345/2 "2023-10-31T17:03:23Z")

</div>

You’ll need an automation like this (in the Resource DB):

 ![image](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/d/dc6095853e1c0c308c9ca6ddede71e05f25f9c53.png)

In the first action, I have set the name of the new Notitie based on the Resource name. You 'll need to add extra field settings to take care of what you have listed in Part 1.

The script in the second action is as follows:

```auto
const fibery = context.getService('fibery');

for (const entity of args.currentEntities) {
    const notitie = await fibery.getEntityById('Readwise/Notitie', entity['Notitie']['Id'], ['Omschrijving']);
    const entityWithExtraFields = await fibery.getEntityById(entity.type, entity.id, ['Highlights']);
    let highlights = await fibery.getEntitiesByIds("Readwise/Highlight", entityWithExtraFields['Highlights'].map((h) => h.Id), ['Text']);
    const sortedHighlights = highlights.sort((h1, h2) => (h1['Creation Date'] > h2['Creation Date']) ? 1 : (h1['Creation Date'] < h2['Creation Date']) ? -1 : 0);
    for (const highlight of sortedHighlights) {
        const text = await fibery.getDocumentContent(highlight['Text']['Secret']);
        await fibery.appendDocumentContent(notitie['Omschrijving']['Secret'], text);
    }
}

```

It assumes that the Notitie db is in the Readwise space.

---

<div class="post-metadata">

**Author:** ![Marloes](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/marloes/32/5926_2.png) [@Marloes](https://community.fibery.io/u/Marloes)\
**Post date:** [October 31, 2023, 6:48pm UTC](https://community.fibery.io/t/script-to-convert-readwise-highlights-into-a-single-note/5345/3 "2023-10-31T18:48:07Z")

</div>

Hi @ChrisG ,

Thank you so much for responding!

I believe there is a small error in the script. I have changed the space where Notitie is in (DB second brain). But I get the following message in Fibery.

_Failed to execute Action “Script”: Cannot read property ‘Id’ of undefined_

```auto
const fibery = context.getService('fibery');

for (const entity of args.currentEntities) {
    const notitie = await fibery.getEntityById('DB second brain/Notitie', entity['Notitie']['Id'], ['Omschrijving']);
    const entityWithExtraFields = await fibery.getEntityById(entity.type, entity.id, ['Highlights']);
    let highlights = await fibery.getEntitiesByIds("Readwise/Highlight", entityWithExtraFields['Highlights'].map((h) => h.Id), ['Text']);
    const sortedHighlights = highlights.sort((h1, h2) => (h1['Creation Date'] > h2['Creation Date']) ? 1 : (h1['Creation Date'] < h2['Creation Date']) ? -1 : 0);
    for (const highlight of sortedHighlights) {
        const text = await fibery.getDocumentContent(highlight['Text']['Secret']);
        await fibery.appendDocumentContent(notitie['Omschrijving']['Secret'], text);
    }
}

```

To make shure the mistake wasn’t on my hand, I added a Notitie database in the Readwise space. But unfortunately that didn’t solved the problem.

I see one difference in our setup. Your Resource can have one Notitie. Ours can have many. Don’t know if this is a problem or not.

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [October 31, 2023, 10:29pm UTC](https://community.fibery.io/t/script-to-convert-readwise-highlights-into-a-single-note/5345/4 "2023-10-31T22:29:19Z")

</div>

> [@Marloes](#):
>
> I see one difference in our setup. Your Resource can have one Notitie. Ours can have many. Don’t know if this is a problem or not.

Yep, that will break it.  
I just assumed that you could only create one entity that summarised the Highlights related to a single Resource.

If there’s the possibility of multiple Notitie then I’ll need to change the strategy a little. I’ll get back to you tomorrow.

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [November 1, 2023, 7:38am UTC](https://community.fibery.io/t/script-to-convert-readwise-highlights-into-a-single-note/5345/5 "2023-11-01T07:38:38Z")

</div>

So here’s Plan B:

**Part 1**  
Create a basic button automation (in Resource DB) to create a Notitie from a Resource:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/7/72a9bc7600151d2cef409e584f4f19139d118114.png)  
(add in the necessary field settings to populate Notitie with the correct info)

**Part 2**  
Add an automation in the Notitie db, which triggers on a Notitie being created, with a filter for ‘Resource is not empty’.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/6/60768826f18eea30070673df0cd60a5bc9296c0d.png)

The script is as follows:

```auto
const fibery = context.getService('fibery');

for (const entity of args.currentEntities) {
    const resource = await fibery.getEntityById('Readwise/Resource', entity['Resource']['Id'], ['Highlights']);
    let highlights = await fibery.getEntitiesByIds("Readwise/Highlight", resource['Highlights'].map((h) => h.Id), ['Text']);
    const sortedHighlights = highlights.sort((h1, h2) => (h1['Creation Date'] > h2['Creation Date']) ? 1 : (h1['Creation Date'] < h2['Creation Date']) ? -1 : 0);
    for (const highlight of sortedHighlights) {
        const text = await fibery.getDocumentContent(highlight['Text']['Secret']);
        await fibery.appendDocumentContent(entity['Omschrijving']['Secret'], text);
    }
}

```

---

<div class="post-metadata">

**Author:** ![Marloes](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/marloes/32/5926_2.png) [@Marloes](https://community.fibery.io/u/Marloes)\
**Post date:** [November 1, 2023, 10:30am UTC](https://community.fibery.io/t/script-to-convert-readwise-highlights-into-a-single-note/5345/6 "2023-11-01T10:30:51Z")

</div>

Hi @Chr1sG , thank you so much!!

We have adjusted your script slightly. We found out that creation date is not the correct field to determine the order of the highlights, but Location is!

Location is the place of the highlight in your book. Due to location, the order of the highlights always remains correct. Even if you go back to the beginning and create new highlights!

So here is the updated script for everyone who is looking for the same solution.

```auto
const fibery = context.getService('fibery');

for (const entity of args.currentEntities) {
    const resource = await fibery.getEntityById('Readwise/Resource', entity['Resource']['Id'], ['Highlights']);
    let highlights = await fibery.getEntitiesByIds("Readwise/Highlight", resource['Highlights'].map((h) => h.Id), ['Text', 'Location']);
    const sortedHighlights = highlights.sort((h1, h2) => (h1['Location'] > h2['Location']) ? 1 : (h1['Location'] < h2['Location']) ? -1 : 0);
    for (const highlight of sortedHighlights) {
        const text = await fibery.getDocumentContent(highlight['Text']['Secret']);
        await fibery.appendDocumentContent(entity['Omschrijving']['Secret'], text);
    }
}

```
