Get Buddy Info

An automator action that returns the current status for an iChat buddy.

Sources

GetBuddyInfo.py

import sys
from Cocoa import NSAppleEventDescriptor
from Automator import AMBundleAction
from InstantMessage import IMService, IMPersonFirstNameKey, IMPersonLastNameKey, IMPersonServiceNameKey
from InstantMessage import IMPersonScreenNameKey, IMPersonStatusKey, IMPersonStatusUnknown
from InstantMessage import IMPersonStatusOffline, IMPersonStatusIdle, IMPersonStatusAway
from InstantMessage import IMPersonStatusAvailable, IMPersonStatusMessageKey
from AddressBook import ABAddressBook, ABPerson

class GetBuddyInfo (AMBundleAction):
    def runWithInput_fromAction_error_(self, input, anAction, errorInfo):
        people = []

        # convert the input to a list of ABPerson objects
        if isinstance(input, NSAppleEventDescriptor):
            count = input.numberOfItems()

            for i in range(1, count+1):
                personDescriptor = input.descriptorAtIndex_(i)
                if personDescriptor is not None:
                    # get the uid of the person from this descriptor
                    if (personDescriptor.descriptorType() == typeObjectSpecifier):
                        container = personDescriptor.descriptorForKeyword_(keyAEContainer)
                        if container is not None:
                            uidDescriptor = container.descriptorAtIndex_(i).descriptorForKeyword_(keyAEKeyData)
                            if uidDescriptor is not None:
                                uid = uidDescriptor.stringValue()
                                if uid is not None:
                                    # get the person object from the uid
                                    person = ABAddressBook.sharedAddressBook().recordForUniqueId_(uid)
                                    if person is not None:
                                        people.append(person)

        info = []
        for person in people:
            for service in IMService.allServices():
                if isinstance(person, ABPerson):
                    screenNames = service.screenNamesForPerson_(person)
                    if screenNames is not None:
                        for screenName in screenNames:
                            dict = service.infoForScreenName_(screenName)
                            if dict is not None:
                                # build the description
                                description = "\rName: "

                                firstName = dict[IMPersonFirstNameKey]
                                if firstName is not None:
                                    description += firstName
                                    description += ' '

                                lastName = dict[IMPersonLastNameKey]
                                if lastName is not None:
                                    description += lastName

                                description += '\r'

                                serviceName = dict[IMPersonServiceNameKey]
                                if serviceName is not None:
                                    description += "Service: "
                                    description += serviceName
                                    description += "\r"

                                screenName = dict[IMPersonScreenNameKey]
                                if screenName is not None:
                                    description += "Screen Name: "
                                    description += screenName
                                    description += "\r"

                                status = dict[IMPersonStatusKey]
                                if status is not None:
                                    description += "Status: "

                                    if status == IMPersonStatusUnknown:
                                        description += "Unknown"
                                    elif status == IMPersonStatusOffline:
                                        description += "Offline"
                                    elif status == IMPersonStatusIdle:
                                        description += "Idle"
                                    elif status == IMPersonStatusAway:
                                        description += "Away"
                                    elif status == IMPersonStatusAvailable:
                                        description += "Available"

                                    description += "\r"

                                message = dict[IMPersonStatusMessageKey]
                                if message is not None:
                                    description += "Message: "
                                    description += message
                                    description += "\r"

                                info.append(description)
        return str(info)

setup.py

"""
Script for building the example.

Usage:
    python3 setup.py py2app

Then install the bundle in dist into ~/Library/Automator.
"""
from setuptools import setup

infoPlist = dict(
    AMAccepts=dict(
        Container="List",
        Optional=False,
        Types=["com.apple.addressbook.person-object"],
    ),
    AMApplication=[
        "Address Book",
        "iChat",
    ],
    AMCanShowWhenRun = True,
    AMCategory = "iChat",
    AMDefaultParameters = dict(),
    AMDescription = dict(
        AMDAlert="iChat must be running for this action to work properly.",
        AMDNote="Information will not be returned for the current user.",
        AMDSummary="This action returns the Instant Message information of the people passed from the previous action.",
    ),
    AMIconName="iChat",
    AMKeywords=(
        "Instant",
        "Message",
        "IM",
    ),
    AMName="Get Buddy Info",
    AMProvides=dict(
        Container="List",
        Types=[
            "com.apple.cocoa.string"
        ],
    )
)

setup(
    name="Get Buddy Info",
    plugin=["GetBuddyInfo.py"],
    data_files=[],
    options=dict(
        py2app=dict(
            extension=".action",
            plist=infoPlist,
        ),
    ),
    setup_requires=[
        "py2app",
        "pyobjc-framework-Automator",
        "pyobjc-framework-AddressBook",
        "pyobjc-framework-InstantMessage",
        "pyobjc-framework-Cocoa",
    ]
)

Resources