Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am using OS X and I want to know if there are any unread messages in my Mail.app? I want to put that number in my shell prompt.

How do I go about it?

share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

First, set your prompt to perform substitutions as explained here: Get Function Into PS1 (Zsh)?
Just append the following in your .zshrc file.

setopt PROMPT_SUBST

Then, find an AppleScript that counts your unread mail, for example this one from Geeklets by Matt Swain. Download the .scpt file and place it somewhere, e.g. ~/Scripts/mail_count.scpt.

Note that Mail.app needs to be running for all of this to work. In your .zshrc, create a function that calls the AppleScript:

function mail_count {
  osascript ~/Scripts/mail_count.scpt
}

Finally, redefine your prompt based on the new function. The following would be the most basic approach – but feel free to add your own variables as needed:

PROMPT='$(mail_count) '

If you don't want to mess with your prompt or use one of the oh-my-zsh styles, or even the promptinit module, you could also just use the precmd() function, in which you define anything that should be run before another command is executed. In that case, you wouldn't need the PROMPT_SUBST option.

precmd() {
  mail_count
}

Whichever method you use, your unread mail count will be output in plain text. If you want to change the messages, take a look at the source of the .scpt file and modify it accordingly.

share|improve this answer
    
This is O(n) where n is number of messages in my inbox, right? I have too many messages. Wouldn't it be too slow? –  snihalani Aug 27 '12 at 20:18
    
Yes, correct. It could be a little slow. I just tested it with dummy values, obviously, on a rather fast machine – near instantaneous. Your mileage may vary. You might be able to count differently – using unread count of every mailbox as explained here. –  slhck Aug 27 '12 at 20:24
    
I posted the better script as an answer. Check it out. –  snihalani Aug 27 '12 at 21:04
add comment
set output_string to ""
tell application "Mail"
    set Unread_Count to unread count of inbox
    if Unread_Count is 0 then
        set output_string to ""
    else if Unread_Count is 1 then
        set output_string to "1 message"
    else
        set output_string to ("" & (get Unread_Count) & " messages")
    end if
end tell
return output_string

I found this most efficient.

share|improve this answer
    
Cool. If you only want the inbox and don't use nested folders, that's definitely a more efficient solution. –  slhck Aug 27 '12 at 21:14
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.