Scan Categorizer with Google Script

Michael Beutler
3 min readJun 7, 2021

Recently I started scanning all the letters and archiving emails I got from the Swiss Post and upload them to Google Workspace.

For Scanning, I use the “SwiftScan” app on iOS. To use it properly the pro version is required and costs currently CHF 33.00 per year.

Google Drive Moving Files Dialog

It took me quite some time to click through the menu every time I wanted to organize a document.

(Don’t get me wrong I am happy with Google Drive but the moving dialog isn’t the fastest out there and navigating through all my drive every time isn’t a good solution to me.)

Luckily Google offers the option to code your own workflows with Google App Script. So I wrote myself a script to analyze the scanned PDF with OCR and move it according to the contents.

The first step is to create a folder in Google Drive and configure “SwiftScan” so it will always upload into the folder. I named mine “Scans”. In the browser navigation, you can then grab the folder ID. (Watch out to copy the whole ID.) Past your folder ID into the parentheses at the top.

const SCAN_FOLDER_ID = "PASTE_YOUR_ID_HERE";

Once you have done that you also have to set your email address to get notified when a category is missing or a document is unprocessable.

const REPORT_EMAIL = "YOUR_EMAIL_ADDRESS";

Now we can define the categories and also define how to handle those. This process may be complicated for users without basic JavaScript knowledge. Each category has a name property for better logging and human readability. More important are the keywords definitions inside the array. When the scanned document includes (not case sensitive) one or more of these keywords the category will be applied. When multiple categories apply to one document the script will stop and notify you via email.

Let’s start by adding a simple category to categorize all documents including the word foo .

const CATEGORIES = [
{ name: "My first Category", keywords: ["foo"], path: "/" }
]

Now the category My first Category will be applied to all PDF’s including the word foo and will move these files to the root folder. If we now want to save them in a different location we can adjust the path property.

const CATEGORIES = [
{ name: "My first Category", keywords: ["foo"], path: "Foo/Bar/" }
]

This will create the folder Bar inside the folder Foo if the structure did not exist yet. The path can be more complex and can include dynamic variables like the year, the month, or the current date.

For more advanced configurations visit the GitHub repository linked at the end of the article or message me via GitHub.

--

--