+import objc
+from Foundation import *
+from AppKit import *
+MVConnectionsController = objc.lookUpClass('MVConnectionsController')
+
+kIdleSuffixKey = 'BetterIdleIdleSuffix'
+idleSuffix = ''
+sleepObserver = None
+wakeObserver = None
+
+class MyListener (NSObject):
+ def init(self):
+ return self
+
+ def onDisplaySleep_(self, notification):
+ appendIdleSuffix()
+ pass
+
+ def onDisplayWake_(self, notification):
+ removeIdleSuffix()
+ pass
+
+listener = MyListener.alloc().init()
+
+def setIdleSuffix( suffix ):
+ global idleSuffix
+ NSUserDefaults.standardUserDefaults().setObject_forKey_(suffix, kIdleSuffixKey)
+ idleSuffix = suffix
+
+def handleIdleSuffix( command, arguments ):
+ if not arguments:
+ return 'Idle suffix is currently "%s"' % idleSuffix
+ setIdleSuffix( arguments )
+ return 'Set idle suffix to "%s"' % idleSuffix
+
+def appendIdleSuffix():
+ for connection in MVConnectionsController.defaultController().connectedConnections():
+ connection.setNickname_('%s|%s' % (connection.preferredNickname(), idleSuffix))
+
+def removeIdleSuffix():
+ for connection in MVConnectionsController.defaultController().connectedConnections():
+ connection.setNickname_(connection.preferredNickname())
+
+# called on load and reload
+def load( scriptFilePath ):
+ global idleSuffix
+ defaults = NSUserDefaults.standardUserDefaults()
+ defaults.registerDefaults_({kIdleSuffixKey:'afk'})
+ idleSuffix = defaults.stringForKey_(kIdleSuffixKey)
+ nc = NSWorkspace.sharedWorkspace().notificationCenter();
+ nc.addObserver_selector_name_object_(listener, "onDisplaySleep:", "NSWorkspaceScreensDidSleepNotification", None)
+ nc.addObserver_selector_name_object_(listener, "onDisplayWake:", "NSWorkspaceScreensDidWakeNotification", None)
+ pass
+
+# called on unload and relead
+def unload():
+ NSUserDefaults.standardUserDefaults().synchronize()
+ NSWorkspace.sharedWorkspace().notificationCenter().removeObserver_(listener)
+ pass
+
+def printString(string, view):
+ if view.respondsToSelector_('addMessageToDisplay:asOutboundMessage:'):
+ view.addMessageToDisplay_asOutboundMessage_(string, False)
+ elif view.respondsToSelector_('addMessageToDisplay:fromUser:asAction:'):
+ view.addMessageToDisplay_fromUser_asAction_(string, None, True)
+ elif view.respondsToSelector_('addEventMessageToDisplay:withName:andAttributes:'):
+ view.addEventMessageToDisplay_withName_andAttributes_(string, 'BetterIdle', None);
+
+# process the command and return true if you handle it or False to pass on to another plugin
+def processUserCommand( command, arguments, connection, view ):
+ # return true if the command was handled or to prevent other plugins or Colloquy from handling it
+ commands = {'idlestring': handleIdleSuffix}
+ if command in commands:
+ function = commands.get(str(command))
+ result = function(str(command), arguments.string())
+ printString(result, view)
+
+ removeIdleSuffix()
+ return command in commands
+
+def processOutgoingMessage( message, view ):
+ removeIdleSuffix()
+ pass
\ No newline at end of file