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?
Best Answer
If you want to get the number of messages in said inbox, the short form of the code is
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 useget the unread count of mailbox "INBOX" of account "Work"
.And if you want a more complete script, this will do the trick:
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 linesproperty defaultAccount : "Work"
andproperty defaultMailbox : "INBOX"
. IfdefaultAccount
ismissing value
or"-g"
, then the default will be to not use an account; ifdefaultMailbox
ismissing 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.