In the past weeks I did a few AppleScripts for Apple Mail and Outlook for the Mac to get information that isn't directly available in both apps. I always thought it a pity that Gmail as web app doesn't have similar capabilities. 

It turns out that I was wrong and all Google apps are scriptable. Of course, they are not scriptable with AppleScript but with JavaScript and in a browser. There is even a debugger - with the debugger I can trace what exactly an app or script is doing.

I'm not very familiar with JavaScript. But I can read instructions (sometimes) and with some experimentation I was able to get a script working which I found on StackOverFlow.

The goal is again to make a list of empty labels. I'm going to use one of my test accounts which has at least one empty label:

Open Google Scripts. As I opened the web app the first time there isn't very much to see:

There are no scripts which are called projects. There is the usual sidebar on the left side for managing scripts and their executions.

Click on the New Project button at the upper left side. You will see this screen:

Grab the code from StackOverFlow:

// Set to 'false' if you want to actually delete labels 
// otherwise it will log them but not delete them.
var testing = true;

// Deletes labels with no email threads
function deleteEmptyLabels() {
    Logger.log("Starting label cleanup");
 var allLabels = GmailApp.getUserLabels();
 var emptyLabels = allLabels.filter(function(label){ return isTreeEmpty(label, allLabels); } );
 for (var i = 0; i < emptyLabels.length; i++){
        Logger.log('Deleting empty label ' + emptyLabels[i].getName());   
 if (!testing){
    Logger.log("Finished label cleanup");

// Finds labels below a parent
function getNestedLabels(parent, allLabels) { 
 var name = parent.getName() + '/';
 return allLabels.filter(function(label) {
 return label.getName().slice(0, name.length) == name;

// Tests a single label for 'emptiness'
function isLabelEmpty(label){
 return label.getThreads(0, 1) == 0;

// Tests a label, and nested labels for 'emptiness'
function isTreeEmpty(label, allLabels){
 if (!isLabelEmpty(label))
 return false;
 var nested = getNestedLabels(label, allLabels);
 for(var j = 0; j < nested.length; j++){
 if (!isTreeEmpty(nested[j], allLabels))
 return false;
 return true;

Delete the existing code and paste the code into the project window:

This was my first experiment with Gmail scripting. Therefore, I read through the script to check what the script is doing. The script even has some comments which usually help. Very important is the first line of code.

var testing = true

This line ensures that I don't make anything kaputt when executing the script. You should keep this line until you are very sure that the script works!!! Only then should you change the line to

var testing = false

So what does the script do? It gets all labels (getUserLabels). Then the script does some recursive code to get the empty labels (filter + isTreeEmpty). Finally the script loops through the empty labels. When testing is set to true the labels are only logged. Otherwise, the empty labels are deleted.

Before we can execute the script I need to save the script by clicking on the Save icon:

I give the script a nice name. But I don't want to share the script:

I need to explore the area with "Get link". I think this means I can make the script public.

Finally I can execute the script. First I need to select the main function which is "deleteEmptyLabels". Then I can click on the triangle to start the execution:

Whereupon it turns out that the script needs some permissions. I can understand that the user can do all sorts of dangerous things to their email account. That's why I recommended above to read the script even if you don't have a clue what the script is doing. So I need to work through a number of screens to get the script executed:

I clicked on Review Permissions to get started with the authorisation:

Now I need to select an account even though I'm already logged in:

Google hasn't verified the app (only the gods know what I'm supposed to do to get that done). So far I still trust myself a tiny bit. Therefore I really really want to continue:

Onwards to the next screen. After clicking "Allow" the script can do anything to my Gmail account:

Of course, I get the usual security alert in my Inbox:

Finally, the permissions are done and I executed the script successfully. Just nothing did happen. Where is the result? It's in the View menu:

And here is finally the result:

Remember that with "var testing = true" nothing at all happens to the Gmail account! I just made a (short) list of empty labels.

This was my first experiment with Google Scripts. Getting the authorisation was necessary but a bit annoying. In all I'm quite happy that Google Scripts exist. And I will learn more what is possible to do.