Applescript to Populate Address Book with Jabber-MSN Contact Info

I recently setup a Jabber account on my server and started to use the MSN transport on it to access my MSN account. Thus I am now able to use the fantastic iChat as my only IM client (since iChat itself can handle .Mac, and Google Talk). However there was one problem: all my MSN contacts shows up through Jabber as

some_name%some_server@msn

and in order for those MSN contact to be displayed as the person’s name, my Address Book entry for those people must have “some_name%some_server@msn” as the person’s Jabber Handle. Since I am a reasonably lazy person and wasn’t exactly prepared to enter all those entries by hand, I wrote the following Applescript to automate the process.

This script will go through all iChat service, look for contact address that ends in “@msn”. Then for all those contacts, search the Address Book entries that contain the corresponding MSN address (some_name@some_server). Then check to see if that person already have a Jabber entry, if not add it.

tell application “iChat”

    set all_jabber_accounts to {}

    set unique_pairs to {}

    set added_jabber to “”

    repeat with this_service in services

        set all_buddies to buddies of this_service

        repeat with this_buddy in all_buddies

            if name of this_buddy ends with “@msn” then

                set all_jabber_accounts_ref to (a reference to all_jabber_accounts)

                set buddy_jabber to name of this_buddy

            

                if all_jabber_accounts_ref does not contain buddy_jabber then

                    copy buddy_jabber to the end of all_jabber_accounts_ref

                

                    set AppleScript’s text item delimiters to “@”

                    set parts to text items of buddy_jabber

                    set AppleScript’s text item delimiters to “”

                

                    set msn_address to item 1 of parts

                    set AppleScript’s text item delimiters to “%”

                    set parts to text items of msn_address

                    set AppleScript’s text item delimiters to “”

                

                    set msn_name to item 1 of parts

                    set msn_server to item 2 of parts

                    set buddy_msn to msn_name & “@” & msn_server

                

                    set this_pair to {buddy_jabber, buddy_msn}

                    set unique_pairs_ref to (a reference to unique_pairs)

                    copy this_pair to the end of unique_pairs_ref

                end if

            end if

        end repeat

    end repeat

    repeat with pair_i from 1 to the count of items in unique_pairs

        set this_pair to item [pair_i] of unique_pairs

        set buddy_jabber to item 1 of this_pair

        set buddy_msn to item 2 of this_pair

    

        tell application “Address Book”

            repeat with this_person in every person

                set is_this_person to false

                if (count of MSN handles of this_person) is greater than 0 then

                    repeat with i from 1 to the count of MSN handle in this_person

                        set this_msn_handle to value of MSN handle [i] of this_person

                        if this_msn_handle is equal to buddy_msn then

                            set is_this_person to true

                        end if

                    end repeat

                end if

            

                if is_this_person then

                    set needs_to_add to true

                    if (count of Jabber handles of this_person) is greater than 0 then

                        repeat with j from 1 to the count of Jabber handle in this_person

                            set this_jabber_handle to value of Jabber handle [j] of this_person

                            if this_jabber_handle is equal to buddy_jabber then

                                set needs_to_add to false

                            end if

                        end repeat

                    end if

                

                    if needs_to_add then

                        set added_jabber to added_jabber & “* “ & (name of this_person) & ” “

                        make new Jabber handle at end of Jabber handles of this_person with

                    properties {label:”Jabber MSN”, value:buddy_jabber}

                    end if

                end if

            end repeat

        end tell

    end repeat

    if added_jabber is not equal to “” then

        display dialog “Jabber MSN Account added to: “ & added_jabber

    else

        display dialog “No Jabber MSN Account added”

    end if

end tell

  1. 2 Responses to “Applescript to Populate Address Book with Jabber-MSN Contact Info”

  2. By Firas on May 21, 2008

    Hey,

    I’m trying out your script because I’ve just set up MSN with Jabber and I’m also too lazy to choose cards for all my contacts….

    I’m using a different jabber server which is fine but all I did was modify your script to go from:

    if name of this_buddy ends with “@msn” then

    TO:

    if name of this_buddy ends with “@msn.netlab.cz”

    What’s happening is that the script appears to be going in an infinite loop counting my friends over and over again. I ran it twice and both times it got up to 2 hours.

    any ideas on what could be wrong? (ichat works fine btw just the name thing is a bit annoying).

    I’m on Leopard, Intel 2.1 ghz MB

    Thanks!

    Firas

  3. By qiushihe on May 28, 2008

    Hi Firas,

    I don’t see how the script could possibly run into a infinite loop since all loops in the script are “repeat with …” and thus would eventually end.

    I admit script is not very efficient (mea culpa). The script loops through every single entry in the address book for each jabber contacts and in my case it took 30 seconds to go through only 7 jabber contacts with around 100 entries in my Address Book (2.2Ghz MBP).

    An indication that the script is working would be that some of your Address Book entries should be modified already by the script.

    One way to solve your issue would be to optimize the code and I may do that if I have time. And another solution would be a script that would simply loop through your Address Book and add an Jabber IM Handle of the proper format to every one who has an MSN IM Handle (basically my script without the iChat part).

    Without Wax

    Billy

Post a Comment