def appLaunchCallback_(self, notification): recording = preferences.getValueForPreference('recording') # get event info t = cfg.NOW() app = notification.userInfo()["NSWorkspaceApplicationKey"] name = unicode(app.localizedName()) pid = int(app.processIdentifier()) # create app listener for this app's window events if app.activationPolicy() == 0: mess = acc.create_application_ref(pid = pid) mess.set_callback(self.windowCallback) mess.watch("AXMoved", "AXWindowResized", "AXFocusedWindowChanged", "AXWindowCreated","AXWindowMiniaturized", "AXWindowDeminiaturized") self.watched[pid] = mess if recording: # log that the application launched text = '{"time": '+ str(t) + ' , "type": "Open", "app": "' + name + '"}' utils_cocoa.write_to_file(text, cfg.APPLOG) # log that application is active if app.isActive(): text = '{"time": '+ str(t) + ' , "type": "Active", "app": "' + name + '"}' utils_cocoa.write_to_file(text, cfg.APPLOG) # take a screenshot eventScreenshots = preferences.getValueForPreference('eventScreenshots') if eventScreenshots: self.sniffer.activity_tracker.take_screenshot() # check if the screen geometry changed and update active window self.updateWindowList()
Cycles through an application's windows and performs the first available action. More than likely this is AXRaise, which brings the window to the front. """ from __future__ import print_function import sys import accessibility as acc try: sys.argv[1] except IndexError: print('Usage: windowswitcher.py PID') sys.exit(1) app = acc.create_application_ref(pid=int(sys.argv[1])) if app['AXRole'] != 'AXApplication': print('This PID is not associated with an application.') sys.exit(1) if not 'AXWindows' in app: print('The application does not seem to have any windows.') sys.exit(1) windows = app['AXWindows'] if windows is None or len(windows) == 0: print('The application does not seem to have any windows.') sys.exit(1)
"""hide.py Hides or unhides an application with the given PID. """ from __future__ import print_function import sys import accessibility as acc try: sys.argv[1] except IndexError: print('Usage: appinfo.py PID') sys.exit(1) app = acc.create_application_ref(pid = int(sys.argv[1])) if app['AXRole'] != 'AXApplication': print('This PID is not associated with an application.') sys.exit(1) if not 'AXHidden' in app or not app.can_set('AXHidden'): print('The application does not seem to be hide-able.') sys.exit(1) app['AXHidden'] = False if app['AXHidden'] else True
def start_app_observers(self): recording = preferences.getValueForPreference('recording') # prompt user to grant accessibility access to Traces, if not already granted acc.is_enabled() # get an early timestamp t = cfg.NOW() # create listeners for application events workspace = NSWorkspace.sharedWorkspace() nc = workspace.notificationCenter() s = objc.selector(self.appLaunchCallback_,signature='v@:@') nc.addObserver_selector_name_object_(self, s, 'NSWorkspaceDidLaunchApplicationNotification', None) s = objc.selector(self.appTerminateCallback_,signature='v@:@') nc.addObserver_selector_name_object_(self, s, 'NSWorkspaceDidTerminateApplicationNotification', None) s = objc.selector(self.appActivateCallback_,signature='v@:@') nc.addObserver_selector_name_object_(self, s, 'NSWorkspaceDidActivateApplicationNotification', None) s = objc.selector(self.appDeactivateCallback_,signature='v@:@') nc.addObserver_selector_name_object_(self, s, 'NSWorkspaceDidDeactivateApplicationNotification', None) # create listeners for system events s = objc.selector(self.wakeCallback_,signature='v@:@') nc.addObserver_selector_name_object_(self, s, 'NSWorkspaceDidWakeNotification', None) s = objc.selector(self.sleepCallback_,signature='v@:@') nc.addObserver_selector_name_object_(self, s, 'NSWorkspaceWillSleepNotification', None) # other events that may be useful to track in the future # https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSWorkspace_Class/ # NSWorkspaceDidHideApplicationNotification # NSWorkspaceDidUnhideApplicationNotification # NSWorkspaceActiveSpaceDidChangeNotification # NSWorkspaceWillPowerOffNotification # NSWorkspaceDidPerformFileOperationNotification # get list of active applications activeApps = workspace.runningApplications() regularApps = [] for app in activeApps: if app.activationPolicy() == 0: # those that show up in the Dock regularApps.append(app) # listen for window events of these applications for app in regularApps: try: p = int(app.processIdentifier()) name = unicode(app.localizedName()) mess = acc.create_application_ref(pid=p) mess.set_callback(self.windowCallback) mess.watch("AXMoved", "AXWindowResized", "AXFocusedWindowChanged", "AXWindowCreated","AXWindowMiniaturized", "AXWindowDeminiaturized") # AXMainWindowChanged self.watched[p] = mess # we need to maintain the listener or it will be deleted on cleanup if recording: # log that the app is open text = '{"time": '+ str(t) + ' , "type": "Open", "app": "' + name + '"}' utils_cocoa.write_to_file(text, cfg.APPLOG) if app.isActive(): text = '{"time": '+ str(t) + ' , "type": "Active", "app": "' + name + '"}' utils_cocoa.write_to_file(text, cfg.APPLOG) except: raise print "Could not create event listener for application: " + str(name) # get inital list of windows and add window listeners self.updateWindowList() # start event loop to track events from other applications CFRunLoopRun()