Send multiple highlights to Readwise in one action

Hi, sorry for asking help here because this script is a little difficult for me to adjust.

I want to send multiple highlights to Readwise and find this Send to Readwise | Drafts Directory . This is exactly what I want, but can I make some adjustments? I don’t need to add “page” to every highlights. How can rewrite this script to delete “page” only keep “highlights”, “Title”, “Author”?

Thanks for you time and kind help.

I’m not terribly familiar with Readwise, and I’m not sure if the author of that action is on these forums to comment. It appears from a quick once over you would need to adjust the addToRWArray function to not look for and parse out the @Page value, and remove thelocation_type and location parameters in the call to Readwise’s API (which are both optional values).

Do you have any scripting experience?

Thanks for your help.I don’t have any coding experience but still try my best to make some adjustments to this action. Luckily, it worked. The author explained the script very well and I just delete some lines. It’s working for me now. I post it here in case other people want to use.

Use it with this action to add "> " quickly. Markdown Quotation (>) | Drafts Directory

// Global Variables

let RWArray = [];
let d = draft;

// Get Title & Author

var title = d.displayTitle;
var content = d.content;
let author = content.match("Author: (.*)")[1];

// Get Array of Quotes
var content = d.content;
var passages = content.split("## Quotes")[1];
var passagesArray = passages.split("> ");
passagesArray.shift(); // First entry in array is blank. Get rid of it.

// Create Array of Objects
passagesArray.forEach(addToRWArray);

// Send items to Readwise
sendToReadwise(RWArray);

// Function to create JSON object out of highlight and add it to the final array.
function addToRWArray(item){

    //Create an array with 2 items: the passage and the array
    var highlightArray = item.split("@Page: ");
    //alert("Highlight Array: " + highlightArray);

    
    // Create JSON and add it to array
    var highlightObject = 
    {
        text: highlightArray[0],
        title: title,
        author: author,
    };
    //alert(JSON.stringify(highlightObject));

    RWArray.push(highlightObject);
    // alert({JSON.stringify(RWArray));
}


function sendToReadwise(RWArray) {
    // Create HTTP Request
    var rwToken = "YOUR READWISE TOKEN HERE"; //Get token here: https://readwise.io/access_token
    var http = HTTP.create();

    // Readwise Response
    var response = http.request({
        "url": "https://readwise.io/api/v2/highlights",
        "method": "POST",
        "data": {
        "highlights": RWArray,
        },
        "headers": {
            "Authorization": "Token " + rwToken,
            "Content-Type": "application/json",
        }
    });
}

/* Example Drafts Entry
# Moby Dick
Author: Herman Melville

## Quotes
> Call me Ishmael

> But don't ever call me an octopus.


*/

1 Like

Great! Glad you were able to sort it out and thanks for sharing the result.