AppleScript Growl Assist

Discussion in 'Mac Programming' started by rookpsu, Mar 29, 2011.

  1. rookpsu macrumors member

    Joined:
    Jul 10, 2008
    Location:
    Philadelphia
    #1
    I have zero experience with AppleScript although it seems like this SHOULD be a rather trivial enhancement.

    Here's a really cool script by Matt Gemmell (mattgemmell.com) that provides Outlook 2011 Growl notifications.

    The only issue is that when you start Outlook after not pulling down for a while you're bombarded with a notification for every new email received and occasionally Outlook chokes because of it.

    Ideally, I'm looking to provide it with the Twitter-like behavior where if there's more then x number of notifications (let's say 5) it'll only throw up ONE notification indicating that there are x notifications. Assuming this is possible, if anyone would be willing to point me in the right direction (or make the mod) I'd be greatly appreciative (as probably would everyone using this script). There is an included option to only throw the last one, but I frequently get more then one email at a time so that won't cut it.

    Here's the code:

    Code:
    -- Register a notification type called "New Mail" with Growl, and enable it.
    tell application "GrowlHelperApp"
    	set the allNotificationsList to {"New Mail"}
    	set the enabledNotificationsList to {"New Mail"}
    	register as application ¬
    		"Outlook" all notifications allNotificationsList ¬
    		default notifications enabledNotificationsList ¬
    		icon of application "Microsoft Outlook"
    end tell
    
    -- Get a list of all "current messages" in Outlook.
    tell application "Microsoft Outlook"
    	set theMessages to the current messages
    end tell
    
    -- Loop through the messages.
    repeat with theMsg in theMessages
    	tell application "Microsoft Outlook"
    		-- Only Growl about unread messages.
    		if is read of theMsg is false then
    			set growl to true
    			set mysubject to get the subject of theMsg
    			set mysender to sender of theMsg
    			set mycontent to content of theMsg
    			-- Get an appropriate representation of the sender; preferably name, but fall back on email.
    			try
    				if name of mysender is "" then
    					set mysender to address of mysender
    				else
    					set mysender to name of mysender
    				end if
    			on error errmesg number errnumber
    				try
    					set mysender to address of mysender
    				on error errmesg number errnumber
    					-- Couldn't get name or email; we'll just say the sender is unknown.
    					set mysender to "Unknown sender"
    				end try
    			end try
    		else
    			-- The message was already read, so we won't bother Growling about it.
    			set growl to false
    		end if
    	end tell
    	
    	-- Tell Growl to show our "New Mail" notification, with a custom title and description.
    	if growl is true then
    		tell application "GrowlHelperApp"
    			notify with name "New Mail" title "Mail from " & mysender description ("\"" & mysubject & "\"") application name "Outlook" --identifier "New Mail"
    			-- If you want multiple notifications to replace each other (i.e. only show one notification at a time, with newer ones replacing the older ones), uncomment the last part of the line above to make Growl coalesce the notifications.
    		end tell
    	end if
    end repeat
     
  2. McGordon, Mar 30, 2011
    Last edited: Mar 30, 2011

    McGordon macrumors member

    Joined:
    Dec 28, 2010
    Location:
    Scotland
    #2
    I don't have Outlook, so I can't test this properly, but as no one has replied in 12 hours, I'll have a look.

    Try this:
    Code:
    -- Register a notification type called "New Mail" with Growl, and enable it.
    tell application "GrowlHelperApp"
       set the allNotificationsList to {"New Mail"}
       set the enabledNotificationsList to {"New Mail"}
       register as application ¬
          "Outlook" all notifications allNotificationsList ¬
          default notifications enabledNotificationsList ¬
          icon of application "Microsoft Outlook"
    end tell
    
    -- Get a list of all "current messages" in Outlook.
    tell application "Microsoft Outlook"
       set theMessages to the current messages
    end tell
    
    -- Loop through the messages.
    set unreadCount to 0
    set growl to false
    repeat with theMsg in theMessages
       tell application "Microsoft Outlook"
          -- Only Growl about unread messages.
          if is read of theMsg is false then
             set unreadCount to unreadCount + 1
             if growl is false
                -- only store sender and subject for the first unread Mail
                set growl to true
                set mysubject to get the subject of theMsg
                set mysender to sender of theMsg
                set mycontent to content of theMsg
                -- Get an appropriate representation of the sender; preferably name, but fall back on email.
                try
                   if name of mysender is "" then
                      set mysender to address of mysender
                   else
                      set mysender to name of mysender
                   end if
                on error errmesg number errnumber
                   try
                      set mysender to address of mysender
                   on error errmesg number errnumber
                      -- Couldn't get name or email; we'll just say the sender is unknown.
                      set mysender to "Unknown sender"
                   end try
                end try
             end if
          end if
       end tell
    end repeat
    
    -- Tell Growl to show our "New Mail" notification, with a custom title and description.
    if growl is true then
       if unreadCount > 1 then
          set otherCount to return & "and " & (unreadCount - 1 as string) & " others"
       else
          set otherCount to ""
       end if
       
       tell application "GrowlHelperApp"
          notify with name "New Mail" title "Mail from " & mysender & otherCount description ("\"" & mysubject & "\"") application name "Outlook" --identifier "New Mail"
       end tell
    end if
    
    
    This should send one notification with the subject and sender of the first mail only. If there's more than one e.g. 8 new mails it should give the subject and sender of the first then say " and 7 others".


    Good luck!
     

Share This Page