Sunday, August 2, 2009

applescript for growl notifications in iChat

I got my first mac last week.

I really like that the main stream operating systems have finally implemented virtual desktops. I assigned each major application to its own 'space'.

The issue with virtual desktops is that it becomes hard to monitor, chats and emails that are happening in different 'spaces'.

I wanted to get a visible notification whenever someone sends me a message in iChat. Further, I wanted this visible notification to sit there forever till I click on it. I need this in case I get the IM while I am away from my desk. And my last requirement was that the notification should only appear if that chat window is in the background, similar to Adium's message received (background chat).

I had to pick up some applescript, but I was able to write a script that can trigger a growl notification for every incoming message. I extended the script to only show the growl notification if I am not actively chatting in that window.

Note that growl applescript interface does not support call backs... So, clicking on the notification can not take you to iChat or the iChat chat window.

The script goes something like this:

using terms from application "iChat"
on message received message from theBuddy for textChat
set whoDidIt to full name of theBuddy
set buddyIcon to image of theBuddy

tell application "System Events"
set frontApp to name of first application process whose frontmost is true
end tell
tell application frontApp
if the (count of windows) is not 0 then
set window_name to name of front window
end if
end tell

if window_name is not equal to "Chat with " & whoDidIt then
tell application "GrowlHelperApp" -- ** the daemon that is behind the scenes
-- Make a list of all the notification types that this script will ever send:
set the allNotificationsList to {"IM Received"}

-- Make a list of the notifications that will be enabled by default.
set the enabledNotificationsList to {"IM Received"}

-- Register our script with growl.
register as application "iChat Growl AppleScript" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "iChat"

if buddyIcon is equal to missing value then
notify with name "IM Received" title whoDidIt description message application name "iChat Growl AppleScript" sticky yes
else
notify with name "IM Received" title whoDidIt description message application name "iChat Growl AppleScript" sticky yes image buddyIcon
end if
end tell
end if
end message received
end using terms from

No comments:

Post a Comment