1import sys
2from Cocoa import *
3from Automator import *
4from AddressBook import *
5from InstantMessage import *
6
7class GetBuddyInfo (AMBundleAction):
8    def runWithInput_fromAction_error_(self, input, anAction, errorInfo):
9        people = []
10
11        # convert the input to a list of ABPerson objects
12        if isinstance(input, NSAppleEventDescriptor):
13            count = input.numberOfItems()
14
15            for i in range(1, count+1):
16                personDescriptor = input.descriptorAtIndex_(i)
17                if personDescriptor is not None:
18                    # get the uid of the person from this descriptor
19                    if (personDescriptor.descriptorType() == typeObjectSpecifier):
20                        container = personDescriptor.descriptorForKeyword_(keyAEContainer)
21                        if container is not None:
22                            uidDescriptor = container.descriptorAtIndex_(i).descriptorForKeyword_(keyAEKeyData)
23                            if uidDescriptor is not None:
24                                uid = uidDescriptor.stringValue()
25                                if uid is not None:
26                                    # get the person object from the uid
27                                    person = ABAddressBook.sharedAddressBook().recordForUniqueId_(uid)
28                                    if person is not None:
29                                        people.append(person)
30
31        info = []
32        for person in people:
33            for service in IMService.allServices():
34                if isinstance(person, ABPerson):
35                    screenNames = service.screenNamesForPerson_(person)
36                    if screenNames is not None:
37                        for screenName in screenNames:
38                            dict = service.infoForScreenName_(screenName)
39                            if dict is not None:
40                                # build the description
41                                description = "\rName: "
42
43                                firstName = dict[IMPersonFirstNameKey]
44                                if firstName is not None:
45                                    description += firstName
46                                    description += ' '
47
48                                lastName = dict[IMPersonLastNameKey]
49                                if lastName is not None:
50                                    description += lastName
51
52                                description += '\r'
53
54                                serviceName = dict[IMPersonServiceNameKey]
55                                if serviceName is not None:
56                                    description += "Service: "
57                                    description += serviceName
58                                    description += "\r"
59
60                                screenName = dict[IMPersonScreenNameKey]
61                                if screenName is not None:
62                                    description += "Screen Name: "
63                                    description += screenName
64                                    description += "\r"
65
66                                status = dict[IMPersonStatusKey]
67                                if status is not None:
68                                    description += "Status: "
69
70                                    if status == IMPersonStatusUnknown:
71                                        description += "Unknown"
72                                    elif status == IMPersonStatusOffline:
73                                        description += "Offline"
74                                    elif status == IMPersonStatusIdle:
75                                        description += "Idle"
76                                    elif status == IMPersonStatusAway:
77                                        description += "Away"
78                                    elif status == IMPersonStatusAvailable:
79                                        description += "Available"
80
81                                    description += "\r"
82
83                                message = dict[IMPersonStatusMessageKey]
84                                if message is not None:
85                                    description += "Message: "
86                                    description += message
87                                    description += "\r"
88
89                                info.append(description)
90        return str(info)
91