Thursday, July 24, 2014

Applescript and Nerdtool for unread mail notification redux

Okay, so I also wanted to check mailboxes other than the inbox for different accounts. I extended the previous AppleScript to this:
-- http://erikslab.com/2007/08/31/applescript-how-to-split-a-string/
on split(theString, theDelimiter)
    -- save delimiters to restore old settings
    set oldDelimiters to AppleScript's text item delimiters
    -- set delimiters to delimiter to be used
    set AppleScript's text item delimiters to theDelimiter
    -- create the array
    set theArray to every text item of theString
    -- restore the old setting
    set AppleScript's text item delimiters to oldDelimiters
    -- return the result
    return theArray
end split

on run args
    set a to 0
    repeat with arg in args
        set splitted to split(arg, ":")
        set thisAccount to item 1 of splitted
        set thisMailbox to item 2 of splitted
        tell application "Mail"
            if it is running then
                set a to a + the (unread count of mailbox thisMailbox of account thisAccount)
            end if
        end tell
    end repeat
    return a
end run

Now, one can pass any number of "Account:Mailbox" tuples to check all the mailboxes one wishes.

No comments: