Wednesday, July 23, 2014

Applescript and Nerdtool for unread mail notification

I previously blogged about updating Mail Unread Menu (http://loganrockmore.com/mailunreadmenu/) -- http://www.k336.org/2012/09/mail-unread-menu-and-53.html  I've long liked the functionality (I really don't like notifications popping up all the time and distracting me -- much better to have an unobtrusive indicator that I can look at when I want), but that went away with my upgrade to Mavericks.  I understand the work involved in keeping software up to date when OSs etc. change, so I have no problems with it falling behind. Instead, I wrote an AppleScript and connected it to Nerdtool (http://mutablecode.com/apps/nerdtool.html) that I really like.  Here's the AppleScript:
on run argv
    set a to 0
    repeat with currentAccount in argv
        tell application "Mail"
            set a to a + the (unread count of mailbox "INBOX" of account currentAccount)
        end tell
    end repeat
end run

One needs to call it with the Mail.app accounts one wants checked:

What would be even better is if some wrote a Cocoa/Objective C app that monitors a file or listens to a name FIFO and displays a short text string in the menubar. I wish I had time to write such a beast -- maybe when I retire...

8 comments:

Ryan Harper said...

Does nerdtool behave well with the multiple desktop spaces in OSX?

Steve Beaty said...

Yes, from the point of view that whatever is set up on the primary space is on them all...

Matt Springer said...

Thanks for doing this Steve. I still can't fathom why Mail doesn't have this feature, and it's really frustrating that Mail Unread Menu no longer works either. However, for people like me who, despite having used Macs since the 80s and having learned simple coding in the punch card era, don't have a clue what to do with Applescript, could you please spoon-feed what to do with that applescript that you wrote after having downloaded Nerdtool? Thanks!

Steve Beaty said...

Matt: what I did was create an executable script in a directory that's on my "$PATH". It looks like:
-----
#! /bin/bash

osascript ~/Library/Scripts/MailUnreadCopy.scpt \
NCAR:INBOX \
Gmail:INBOX
-----
I named it "checkmail" and made sure it had execute (700) permission. Then, in nerdtool, I added a "shell" command, called every 60 seconds, that is the path to this script, "/usr/local/bin/checkmail" for example. Let me know if this makes enough sense. Thanks!

Matt Springer said...

Thanks Steve, I don't really have the background to completely understand it, but there's now a patch to repair the mailunreadmenu bundle so I guess it is ok for now!

Steve Beaty said...

Matt: cool; let me know if you'd like more detailed instructions at some point.

David Groover said...

I am looking for examples for how to adjust my iCal events for display on my desktop. There is icalbuddy, which works well with NerdTools. But, I Want to teak the output in a way icalbuddy does not do - adjust the event lines to Title - Time start and a space between this and the next entry.

Looking over your example, I was also surprised to learn that I can set Nerdtool to run an AppleScript. Although, I tried plugging you example into an AS editor and It got no results. What is the line after the path to the applescript in your example, apparently this makes It work? So I can understand this better,what do I need to understand about how Nerdtool and AS work together? This is the first I have heard of It.

I have an AS that digs through all my TaskPaper docs and pulls at all the tagged events then parses them into a BBEdit document. It would be useful if I understood how to do that in Nerdtools. Much thanks, David

Steve Beaty said...

David -- the 'osascript' is a command-line command that runs an AppleScript. The AppleScript take arguments that are the names of the accounts to check for new mail in their inbox. I have an updated script that can also check non-inbox mailboxes too, if you're interested in that. Let me know if this isn't clear. Thanks!