Digital nomad,  TDM,  Tips

How to send your reMarkable notes to Google Drive using a Gmail alias

I bumped into this excellent free script : Fetch Gmail Attachment to Google Drive using Google Apps Script. I edited a little bit to match reMarkable users’ needs.


This script will fetch all the attached files you send to your Gmail alias and save them on Google Drive.


First of all, the script uses Gmail & Google Drive APIs. I won’t have access to your Gmail or Google Drive. Google will host your script in your private space here.

1- Create a new project on script.google.com

2- On Editor, copy/paste my script

from this
to this

3- Here are a few things you may want to customize before using the script :

Line 9. Your Gmail alias. On Gmail, create aliases to send all your reMarkable files to. It’s easier and faster to sort them out instead of going through your 100 000 emails. See how to create Gmail aliases. You can create something like janedoe+rmk@gmail.com.

var yourMail = 'YOUR_GMAIL_ALIAS_HERE';

You may want to change the type of files you want to export to Google Drive. By default, I chose all reMarkable’s available file extensions: PDF, SVG, and PNG. Line 12.

var fileTypesToExtract = ['pdf', 'svg', 'png'];

Line 14. Name of the folder in Google Drive in which files will be stored. By default, it will be named reMarkabletoDrive

If the folder hasn’t been created, the script with automatically create it for you.

var folderName = 'reMarkabletoDrive';

Line 16. Name of the label which will be applied after processing your message thread.

If the label hasn’t been created, the script will automatically create it for you. Beware that the script will ignore all threads already having the label so that the script won’t fetch the same threads indefinitely.

var labelName = 'fetchedToDrive';

4- Rename your script. Save then Debug.

5- You have to give your script all the required authorizations to access Gmail and Google Drive.

Click on “review permissions”

Click on “advanced”

Go on, if you trust me 🙂

Allow

6- Now, select “Triggers” in the left-hand side menu

For the script to work efficiently, you have to schedule the script to be launched regularly (every 15 minutes if you send yourself lots of documents, or every day if you don’t). If the script doesn’t execute regularly, new incoming emails won’t be processed automatically. Don’t forget to allow failure notifications.

7- If you are too impatient to launch the script, after having scheduled the script, you can go back to the editor and click “run”

8 – If you haven’t renamed the default folder, you will see your files under the folder “reMarkabletoDrive” on Google Drive.

9- All processed threads will be labeled “fetchedToDrive” by the script and will be ignored during the next launch

Here is the script. Enjoy!

/*
 * This script is based on Apps Script Development's script : http://www.googleappsscript.org/home/fetch-gmail-attachment-to-google-drive-using-google-apps-script
 * It has been edited by nomadbento.com to match reMarkable users' needs
 */

// GLOBALS
//Enter your customized reMarkable alias gmail 
/*****************COMPULSORY********************/
var yourMail = 'YOUR_GMAIL_ALIAS_HERE';
/*****************THANK YOU********************/
//Array of file extensions which you would like to extract to Drive
var fileTypesToExtract = ['pdf', 'svg', 'png'];
//Name of the folder in google drive in which files will be put
var folderName = 'reMarkabletoDrive';
//Name of the label which will be applied after processing the mail message
var labelName = 'fetchedToDrive';


function GmailToDrive(){
  //build query to search emails
  var query = '';
  //filename:pdf OR filename:svg OR filename:jpg 
  for(var i in fileTypesToExtract){
 query += (query === '' ?('filename:'+fileTypesToExtract[i]) : (' OR filename:'+fileTypesToExtract[i]));
  }
  query = 'in:inbox has:attachment to:'+yourMail+' '+ query;
  var threads = GmailApp.search(query);
  var label = getGmailLabel_(labelName);
  var parentFolder;
  if(threads.length > 0){
    parentFolder = getFolder_(folderName);
  }
  
  for(var i in threads){
    var mesgs = threads[i].getMessages();
    var threadlabels = threads[i].getLabels();
    if (checkIfContainsLabel(threadlabels)=="ok"){
      for(var j in mesgs){
          // get the names of the labels attached to the message
          var recipient = mesgs[j].getTo();
          //check email address
          if (checkRecipient(recipient)=="ok"){
              //get attachments
              var attachments = mesgs[j].getAttachments();
            for(var k in attachments){
              var attachment = attachments[k];
              //check attachements extension
              var isDefinedType = checkIfDefinedType_(attachment);
              if(isDefinedType){
                var attachmentBlob = attachment.copyBlob();
                    var file = DriveApp.createFile(attachmentBlob);
                    parentFolder.addFile(file);
              }
            }
          }  
      }
      threads[i].addLabel(label);
    }
 
  }
}

//This function will get the parent folder in Google drive
function getFolder_(folderName){
  var folder;
  var fi = DriveApp.getFoldersByName(folderName);
  if(fi.hasNext()){
    folder = fi.next();
  }
  else{
    folder = DriveApp.createFolder(folderName);
  }
  return folder;
}


function getGmailLabel_(name){
  var label = GmailApp.getUserLabelByName(name);
  if(!label){
 label = GmailApp.createLabel(name);
  }
  return label;
}

//this function will check for filextension type.
// and return boolean
function checkIfDefinedType_(attachment){
  var fileName = attachment.getName();
  var temp = fileName.split('.');
  var fileExtension = temp[temp.length-1].toLowerCase();
  if(fileTypesToExtract.indexOf(fileExtension) !== -1) return true;
  else return false;
}


function checkIfContainsLabel(allLabels){
  var check = "ok"; 
  for (var index = 0; index < allLabels.length; index++) {  
    var messageLabel = allLabels[index].getName();
    var temp = messageLabel.split('/');
    for (var l = 0; l < temp.length; l++){
      var nameLabel = temp[l];
      if (nameLabel.indexOf(labelName) == 0) {
        check = "nok";
      }
    }
  }
  return check;

}


function checkRecipient(allRecipients){
    var check = "nok";
    var temp = allRecipients.split(',');
    for (var m = 0; m < temp.length; m++){
      var emailRecipient = temp[m].toLowerCase();
      if (emailRecipient.indexOf(yourMail.toLowerCase()) != -1){
        check = "ok";
      }
    }

  return check;  
}

Leave a Reply

Your email address will not be published.