An automator action that returns the current status for an iChat buddy.
import sys
from Cocoa import *
from Automator import *
from AddressBook import *
from InstantMessage import *
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)
"""
Script for building the example.
Usage:
python setup.py py2app
Then install the bundle in dist into ~/Library/Automator.
"""
from distutils.core import setup
import py2app
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,
)),
)