Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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?

share|improve this question
    
@Iorin: what is your question? whether it can be done? –  Robert S Ciaccio Feb 7 '11 at 15:52
    
@calavera: Looking for the Applescript code. I clarified the question. –  Lorin Hochstein Feb 7 '11 at 16:01
    
What do you mean by "size" number of kilobytes? unread messages? total messages? –  zevlag Feb 7 '11 at 16:18
    
@zevlag: total number of message. I've updated the question. –  Lorin Hochstein Feb 7 '11 at 21:55
add comment

1 Answer 1

up vote 11 down vote accepted

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.

share|improve this answer
1  
Wow. I was just going to suggest something like ls /Path/To/MailBox/*.emlx | wc -l for messages in a box, but yours is definitely the better answer. Thanks! –  zevlag Feb 7 '11 at 22:24
1  
Typo in the script above... else if theCount = 2 then set theAccount to item 1 of args set theMailbox to item 1 of args should be else if theCount = 2 then set theAccount to item 1 of args set theMailbox to item 2 of args –  user23814 Jun 9 '12 at 17:25
    
@SamuelMinter: Thanks; that's fixed now. –  Antal S-Z Jun 9 '12 at 17:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.