AppleScript: Get the latest chat with multiple active chats?

An instant messenger which can connect to AIM, GTalk, Jabber, ICQ, and more.
Post Reply
ultrus
Harmless
Posts: 5
Joined: Wed Mar 19, 2008 3:43 pm

AppleScript: Get the latest chat with multiple active chats?

Post by ultrus »

When I receive a message in Adium, I can activate an applescipt = totally cool! When I'm away, I want to activate a script that tells people random jokes when they send me a message. If I have one chat or chat window, that's easy. If I have multiple chats going on at once, I start sending the wrong people replies.

Any thoughts on how I can target the last chat that I got a message from?

So far I can get info for every active chat this way:

Code: Select all

repeat with i in every chat
	set userAccountService to title of service of account of i
	set userAccountName to title of account of i
	set chatAccountName to title of first contact of i
	set chatDisplayName to display name of contact of i
	set chatIndex to index of i
Thanks much in advance for the assist!
mtimmsj
Frappa
Posts: 144
Joined: Wed Jun 08, 2005 6:55 pm
Location: Olympia, WA

Re: AppleScript: Get the latest chat with multiple active chats?

Post by mtimmsj »

Here's how I handle this:

* I have a script that is triggered by a "Message Recieved (Initial)" event. So it will only run when someone sends me a message for the first time. This is configured in the "events" portion of Adium preferences.
* The script grabs all the dates opened values for all of the chats, saves them to a list and then sorts them saving the results to a different list. This puts the most recent date opened to the end of the sorted list.
* The script then finds the position of that date opened value in the original list. This position (just an index, i.e. a number) can be used to grab the chat you want.

Sample code (the simple_sort and list_position handlers came from Apple's Applescript website):

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

try
  tell application "Adium"
    set theDates to the date opened of every chat
    -- Now figure out which chat is the most recent
    -- 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)
    -- So no myItem is an index into the chats for the chat that is the one we want
    set theChat to the chat myItem
    -- And theChat now contains the most recent chat that was opened
  end tell
on error
  -- print debug message here
end try
Post Reply