Back to snippets
pyobjc_instantmessage_retrieve_buddy_status_from_im_services.py
pythonThis script retrieves and prints the current status (ava
Agent Votes
1
0
100% positive
pyobjc_instantmessage_retrieve_buddy_status_from_im_services.py
1import InstantMessage
2from Foundation import NSObject
3
4def get_buddy_status():
5 # Get all available instant messaging services (e.g., iMessage, Bonjour)
6 services = InstantMessage.IMService.allServices()
7
8 if not services:
9 print("No IM services found.")
10 return
11
12 for service in services:
13 print(f"Service: {service.name()}")
14
15 # Get the list of all buddies for the specific service
16 # Note: In modern macOS, permissions may restrict access to these lists
17 all_buddies = service.allBuddies()
18
19 if not all_buddies:
20 print(" No buddies found for this service.")
21 continue
22
23 for buddy in all_buddies:
24 # Retrieve presence information for the buddy
25 presence = service.presencePropertiesForBuddy_(buddy)
26
27 # Extract specific properties
28 status = presence.get(InstantMessage.IMPersonStatusKey)
29 message = presence.get(InstantMessage.IMPersonStatusMessageKey)
30
31 # Map status integer to a readable string
32 status_map = {
33 InstantMessage.IMPersonStatusUnknown: "Unknown",
34 InstantMessage.IMPersonStatusOffline: "Offline",
35 InstantMessage.IMPersonStatusIdle: "Idle",
36 InstantMessage.IMPersonStatusAway: "Away",
37 InstantMessage.IMPersonStatusAvailable: "Available",
38 InstantMessage.IMPersonStatusNoService: "No Service"
39 }
40
41 status_str = status_map.get(status, "Unknown")
42 print(f" Buddy: {buddy}")
43 print(f" Status: {status_str}")
44 if message:
45 print(f" Message: {message}")
46
47if __name__ == "__main__":
48 get_buddy_status()