Page 1 of 1

AppleScript: Getting last chat received but not last chat :P

Posted: Wed May 21, 2008 9:12 pm
by ultrus
Hello,
I'm playing with Adium's AppleScript triggering capability and am referencing the following link to get chat information:
http://trac.adiumx.com/wiki/AppleScript_Support_1.2

The goal is to figure out who sent me the most recent chat message. So far I'm able to trigger AppleScript when a message is received, and also get the nickname of the contact of "the last chat" like this:

Code: Select all

tell application "Adium"
   try
      set nickname to display name of contact of last chat
      display dialog nickname
   end try
end tell
However, that seems based on the order of chat tabs, not most recent message. If I'm chatting with a friend in the first tab of the chat window, and a second friend is chatting with me in a second tab to the right (index of 2), if the first friend chats, the code above still returns my second tab friend's nickname. - not what I want

As a note, I think "chats" refer to a whole conversation, not an individual message. I also gather that the messages themselves are not accessible directly, so finding out the latest message contact may be difficult? Any thoughts?

Thanks in advance. :)

Re: AppleScript: Getting last chat received but not last chat :P

Posted: Thu May 22, 2008 12:02 am
by mtimmsj
So if I understand your request properly, you want to be able to trigger an applescript based on receiving a message and have that applescript do something only for the chat which you received that message in. Your assumptions about finding who sent you the most recent message are probably correct. Until the support for applescript to access messages is added, I don't see an easy way to find out who sent you the most recent message.

Maybe another solution would be to add a "date updated" or "date message received" property to the chat object so that you could go through all of the open chats and look for the one with the most recent date. I already have a solution similar to this for when the chats were opened using the 'date opened' property. This allows me to trigger a script when a new message is received (i.e. the first message in a chat), that script then figure out which is the most recent chat to be opened and prints out data about that contact (via growl) from a company database. Here's the logic I use in that script to get the most recent chat, maybe it will help someone else:

Code: Select all

on simple_sort(my_list)
	set the index_list to {}
	set the sorted_list to {}
	repeat (the number of items in my_list) times
		set the low_item to ""
		repeat with i from 1 to (number of items in my_list)
			if i is not in the index_list then
				set this_item to item i of my_list
				if the low_item is "" then
					set the low_item to this_item
					set the low_item_index to i
				else if this_item is less than low_item then
					set the low_item to this_item
					set the low_item_index to i
				end if
			end if
		end repeat
		set the end of sorted_list to the low_item
		set the end of the index_list to the low_item_index
	end repeat
	return the sorted_list
end simple_sort

on list_position(this_item, this_list)
	repeat with i from 1 to the count of this_list
		if (item i of this_list as string) is (this_item as string) then return i
	end repeat
	return 0
end list_position

tell application "Adium"
	set theDates to the date opened of every chat
	-- Now figure out which chat is the most recent and
	-- get the names of the contacts in that chat
	-- Sort the date to get the most recent one at the last place of the list
	set theSortedDates to me's simple_sort(theDates)
	-- Find the location in the original list the last item of the sorted list is at
	set myItem to me's list_position((the last item of theSortedDates), theDates)
	set theChat to item myItem of every chat
end tell
simple_sort and list_position are both from Apple:
http://www.apple.com/applescript/sbrt/sbrt-05.html
http://www.apple.com/applescript/sbrt/sbrt-07.html