Script that outputs number of emails in Mail.app inbox

I'd like to have a command-line script I can invoke at the Terminal that will output the total number of messages in my "Work" Inbox in mail.app. Usage would look like this:

$ inbox-count
48

What does the Applescript code look like to do something like this?


Solution 1:

If you want to get the number of messages in said inbox, the short form of the code is

tell application "Mail" to ¬
  get the count of messages of mailbox "INBOX" of account "Work"

If you want the global inbox, then you can use get the count of messages of inbox instead. If you just want unread messages, then you can use get the unread count of mailbox "INBOX" of account "Work".

And if you want a more complete script, this will do the trick:

#!/usr/bin/osascript
property defaultAccount : "Work"
property defaultMailbox : "INBOX"

on run args
  set justUnread to false
  set theAccount to missing value
  set theMailbox to missing value

  if defaultAccount = missing value then set defaultAccount to "-g"
  if defaultMailbox = missing value then set defaultMailbox to "INBOX"

  set theCount to the count of args

  if theCount > 0 then
    if item 1 of args = "-u" then
      set justUnread to true
      set theCount to theCount - 1
      set args to the rest of args
    else if item 1 of args = "-ug" or item 1 of args = "-gu" then
      set justUnread to true
      set item 1 of args to "-g"
    else if theCount > 1 and ¬
            item 1 of args = "-g" and item 2 of args = "-u" then
      set justUnread to true
      set theCount to theCount - 1
      set args to the rest of args
      set item 1 of args to "-g"
    end if
  end if

  tell application "Mail"
    if theCount = 0 then
      set theAccount to defaultAccount
      set theMailbox to defaultMailbox
    else if theCount = 1 then
      set theAccount to item 1 of args
      set theMailbox to defaultMailbox
    else if theCount = 2 then
      set theAccount to item 1 of args
      set theMailbox to item 2 of args
    else
      error character id 10 ¬
          & "Usage: inbox-count [-u] [[account] mailbox]" & character id 10 ¬
          & "       inbox-count [-u] -g [mailbox]"
    end if

    set mailboxValue to missing value
    if theAccount = "-g" then
      if theMailbox = "INBOX" then
        set mailboxValue to inbox
      else
        set mailboxValue to mailbox theMailbox
      end if
    else
      set mailboxValue to mailbox theMailbox of account theAccount
    end if

    if justUnread then
      return the unread count of mailboxValue
    else
      return the count of messages of mailboxValue
    end if
  end tell
end run

Most of that is command-line parsing, because that's a pain to get right in AppleScript. But the upshot is that with that script in your path as inbox-count, then the following commands work:

  • inbox-count to check the number of messages in the default mailbox/account pair.
  • inbox-count -g to check the number of messages in the global (combined) inbox.
  • inbox-count Play to check the number of messages in the default mailbox for the account "Play".
  • inbox-count -g Important to check the number of messages in the global mailbox "Important".
  • inbox-count Play Facebook to check the number of messages in the mailbox "Facebook" for the account "Play".

You can also prepend a -u to any of those commands (e.g., inbox-count -u, inbox-count -ug, inbox-count -u Play Facebook) to just get the unread count. To change the default account and mailbox, change the lines property defaultAccount : "Work" and property defaultMailbox : "INBOX". If defaultAccount is missing value or "-g", then the default will be to not use an account; if defaultMailbox is missing value or "INBOX", then the default will be to either use a mailbox named "INBOX" or, if the account is "-g", to use the global inbox.