Custom Toolbar Buttons for Thunderbird


If you watched the video and wondered what the JavaScript code was for my "Move Junk" button in Thunderbird, then read on. If you haven't watched the video, this article describes how to add custom toolbar buttons to Thunderbird using the Custom Buttons addon.

The Custom Buttons addon for Thunderbird allows you to add buttons to Thunderbird's toolbar. The addon is simple to use: once it's installed, right click on the toolbar and select "Add new button...", give the button a name, pick an image to use, and then supply some JavaScript code for the button. Well, ok so that last step isn't so easy. Since you need to know how to write JavaScript and you need to know the Thunderbird API. Here's a couple of buttons that I use.

The first empties the Trash folder. My image for it is:


The code is simple:


Enter that under the "Code" tab and you should have a button for emptying the trash.

The second button moves all the junk mail in the current folder to the Junk folder. Thunderbird has the option to do this automatically as it downloads email and classifies it as Junk, but I like to see what's going into the Junk folder beforehand so that I have a chance to police Thunderbird's decisions. My image for this button is:


The code here is much more involved, and I didn't develop it from scratch, I used an existing function that I found in the Thunderbird source that deleted junk in the current folder. All I did is change it so that it moves it rather than deletes it.

// Modification of deleteJunkInFolder().
function junkJunkInFolder()
  var view = GetDBView();

  // Get junk mail folder.
  var msgURI = view.getURIForViewIndex(0);
  var msgHdr = messenger.msgHdrFromURI(msgURI);
  var server = msgHdr.folder.server;
  var spamSettings = server.spamSettings;
  var spamFolderURI = spamSettings.spamFolderURI;
  var currentFolderURI = msgHdr.folder.folderURL
  if ( !spamFolderURI ) {
    dump('no spam folder!');
  //alert("Current folder is: " + currentFolderURI + "\nJunk folder is: " + spamFolderURI);

  if ( currentFolderURI == spamFolderURI ) {
     //alert("Current folder is the Junk folder");

  var junkTargetFolder = GetMsgFolderFromUri(spamFolderURI);

  // Select junk messages.
  // Need to expand all threads, so we find everything

  var treeView = view.QueryInterface(Components.interfaces.nsITreeView);
  var count = treeView.rowCount;
  if ( !count ) return;

  var treeSelection = treeView.selection;
  var clearedSelection = false;

  for ( var i = 0; i < count; ++i ) {
    var messageUri = view.getURIForViewIndex(i);
    var msgHdr = messenger.msgHdrFromURI(messageUri);
    var junkScore = msgHdr.getStringProperty("junkscore");
    var isJunk = ((junkScore != "") && (junkScore != "0"));
    // if the message is junk, select it.
    if ( isJunk ) {
      // only do this once
      if ( !clearedSelection ) {
        // clear the current selection
        // since we will be deleting all selected messages
        clearedSelection = true;
        treeSelection.selectEventsSuppressed = true;
      treeSelection.rangedSelect(i, i, true /* augment */);

  // If we didn't clear the selection there was no junk, so bail.
  if ( !clearedSelection ) return;

  treeSelection.selectEventsSuppressed = false;
  // delete the selected messages
  view.doCommandWithFolder(nsMsgViewCommandType.moveMessages, junkTargetFolder);


I won't explain the code in detail, particularily since I didn't write most of it, but the operation is fairly simple: get a reference to the current folder and the junk folder, then loop through all the messages in the current folder and select the junk messages. After the loop if any messages are selected move them to the junk folder.

move-junk.png1.4 KB
empty-trash.png1.84 KB

Mitch Frazier is an Associate Editor for Linux Journal.

One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix