def set_icon(self, file_name): try: from Cocoa import NSImage, NSWorkspace path = (sys._MEIPASS + "/icon.icns/icon.icns") if hasattr( sys, "_MEIPASS") else "icon.icns" img = NSImage.alloc().initWithContentsOfFile_(str(path)) NSWorkspace.sharedWorkspace().setIcon_forFile_options_( img, str(file_name), 0) except ModuleNotFoundError: pass #silent fail writing icon
def __init__(self, paths: list[str], batch_size: int, file_filter: Optional[Callable[[str], bool]], include_mac_packages: bool = False) -> None: self.condition = Condition() self.paths = paths self.batches: list[list[str]] = [] self.batch_size = batch_size self.done = False self.errored = False self.file_filter = file_filter self.batch_buffer_size = 5 self._pending_batch: list[str] = [] self._include_mac_packages = include_mac_packages if self._include_mac_packages: # pylint: disable=useless-suppression # pylint: disable=no-name-in-module, import-error # noinspection PyUnresolvedReferences from Cocoa import NSWorkspace self._shared_nsworkspace = NSWorkspace.sharedWorkspace() # pylint: enable=useless-suppression else: self._shared_nsworkspace = None
def _screenImageOptions(screen): ''' Process an instance of NSScreen, returning its options as a hash ''' workspace = NSWorkspace.sharedWorkspace() if screen == NSScreen.mainScreen(): is_main = True else: is_main = False screen_options = workspace.desktopImageOptionsForScreen_(screen) scaling_factor = screen_options.objectForKey_( NSWorkspaceDesktopImageScalingKey) # NSImageScaling : NSNumber allow_clipping = screen_options.objectForKey_( NSWorkspaceDesktopImageAllowClippingKey) # NSNumber fill_color = screen_options.objectForKey_( NSWorkspaceDesktopImageFillColorKey) # NSColor image_url = workspace.desktopImageURLForScreen_( screen).absoluteString() # NSURL options_dict = { 'main': is_main, 'scaling_factor': scaling_factor, 'image_url': image_url, 'allow_clipping': allow_clipping } if fill_color: options_dict['fill_color'] = { 'r': fill_color.redComponent(), 'g': fill_color.greenComponent(), 'b': fill_color.blueComponent() } return options_dict
def add_workspace_notifications(nsw_config): # See http://developer.apple.com/documentation/Cocoa/Conceptual/Workspace/Workspace.html notification_center = NSWorkspace.sharedWorkspace().notificationCenter() for event in nsw_config: event_config = nsw_config[event] if "class" in event_config: obj = get_handler_object(event_config['class']) objc_method = "on%s:" % event py_method = objc_method.replace(":", "_") if not hasattr(obj, py_method) or not callable(getattr(obj, py_method)): print >> sys.stderr, \ "NSWorkspace Notification %s: handler class %s must define a %s method" % (event, event_config['class'], py_method) sys.exit(1) notification_center.addObserver_selector_name_object_(obj, objc_method, event, None) else: handler = NotificationHandler.new() handler.name = "NSWorkspace Notification %s" % event handler.callable = get_callable_for_event(event, event_config, context=handler.name) assert(callable(handler.onNotification_)) notification_center.addObserver_selector_name_object_(handler, "onNotification:", event, None) WORKSPACE_HANDLERS[event] = handler log_list("Listening for these NSWorkspace notifications: %s", nsw_config.keys())
def add_workspace_notifications(nsw_config): # See http://developer.apple.com/documentation/Cocoa/Conceptual/Workspace/Workspace.html notification_center = NSWorkspace.sharedWorkspace().notificationCenter() for event in nsw_config: event_config = nsw_config[event] if "class" in event_config: obj = get_handler_object(event_config['class']) objc_method = "on%s:" % event py_method = objc_method.replace(":", "_") if not hasattr(obj, py_method) or not callable(getattr(obj, py_method)): print >> sys.stderr, \ "NSWorkspace Notification %s: handler class %s must define a %s method" % (event, event_config['class'], py_method) sys.exit(1) notification_center.addObserver_selector_name_object_(obj, objc_method, event, None) else: handler = NSNotificationHandler.new() handler.name = "NSWorkspace Notification %s" % event handler.callable = get_callable_for_event(event, event_config, context=handler.name) assert(callable(handler.onNotification_)) notification_center.addObserver_selector_name_object_(handler, "onNotification:", event, None) log_list("Listening for these NSWorkspace notifications: %s", nsw_config.keys())
def frontmost(): ''' Get the name of the frontmost application ''' workSpace = NSWorkspace.sharedWorkspace() app = workSpace.frontmostApplication() return app.localizedName()
def add_distributed_notification(event, event_config, dist_center): if "class" in event_config: obj = get_handler_object(event_config['class']) objc_method = "on%s:" % event py_method = objc_method.replace(":", "_") if not hasattr(obj, py_method) or not callable(getattr(obj, py_method)): print >> sys.stderr, \ "NSDistributedNotification %s: handler class %s must define a %s method" % (event, event_config['class'], py_method) sys.exit(1) dist_center.addObserver_selector_name_object_(obj, objc_method, event, None) else: shouldrm = False process_event = {} if not event in DISTRIBUTED_IDS: DISTRIBUTED_IDS[event] = event_config handler = NSNotificationHandler.new() handler.name = "NSDistributed %s" % event if "process" in event_config: notification_center = NSWorkspace.sharedWorkspace( ).notificationCenter() process_event["process"] = event_config["process"] process_event["event"] = event process_event["event_config"] = event_config logging.info("Adding Process Monitor: %s" % process_event["process"]) if not event in RELAUNCH_IDS: add_workspace_notification( "NSWorkspaceDidLaunchApplicationNotification", process_event, notification_center) RELAUNCH_IDS[event] = process_event else: shouldrm = True handler.callable = get_callable_for_event(event, event_config, context=handler.name) assert (callable(handler.onNotification_)) event_name = event if event == '*': event_name = None if not shouldrm: dist_center.addObserver_selector_name_object_( handler, "onNotification:", event_name, None) else: del DISTRIBUTED_IDS[event] dist_center.removeObserver_name_object_( handler, "onNotification", event_name, None)
def add_workspace_notifications(nsw_config): # See http://developer.apple.com/documentation/Cocoa/Conceptual/Workspace/Workspace.html notification_center = NSWorkspace.sharedWorkspace().notificationCenter() for event in nsw_config: event_config = nsw_config[event] add_workspace_notification(event, event_config, notification_center) log_list("Listening for these NSWorkspace notifications: %s", nsw_config.keys())
def search(query): ''' Reveal the finder and search in Spotlight, with the given query. CLI Example:: salt '*' finder.search 'Documents' ''' workSpace = NSWorkspace.sharedWorkspace() status = workSpace.showSearchResultsForQueryString_(query) return status
def add_workspace_notifications(notification_list): notification_center = NSWorkspace.sharedWorkspace().notificationCenter() for notification in notification_list: handler = NSNotificationHandler.new() handler.name = "NSWorkspace Notification %s" % notification handler.callable = get_callable_for_event(notification, context=handler.name) assert(callable(handler.onNotification_)) notification_center.addObserver_selector_name_object_(handler, "onNotification:", notification, None)
def launch(application): ''' Open an Application by name. This does not need to be the full path to the application, and does not need to have an .app extension. CLI Example:: salt '*' desktop.launch 'TextEdit' ''' workSpace = NSWorkspace.sharedWorkspace() status = workSpace.launchApplication_(application) return status
def open_file(path): ''' Open a file in the current users session (using the default application). This requires the full path to the file. CLI Example:: salt '*' desktop.open_file '/Users/Shared' ''' workSpace = NSWorkspace.sharedWorkspace() status = workSpace.openFile_(path) return status
def select(path, finder_path=""): ''' Reveal the finder and select the file system object at the given path. If the second parameter is specified, a new finder window will be opened at that path. Otherwise, an existing finder window will be used. CLI Example:: salt '*' finder.select '/Users/Shared' '/Users' ''' workSpace = NSWorkspace.sharedWorkspace() status = workSpace.selectFile_inFileViewerRootedAtPath_(path, finder_path) return status
def add_workspace_notifications(notification_list): notification_center = NSWorkspace.sharedWorkspace().notificationCenter() for notification in notification_list: handler = NSNotificationHandler.new() handler.name = "NSWorkspace Notification %s" % notification handler.callable = get_callable_for_event(notification, context=handler.name) assert (callable(handler.onNotification_)) notification_center.addObserver_selector_name_object_( handler, "onNotification:", notification, None)
def processes(): ''' Get a list of running processes in the user session TODO: optional get by bundle ID TODO: optional get hidden ''' workSpace = NSWorkspace.sharedWorkspace() appList = workSpace.runningApplications() names = [app.localizedName() for app in appList] names.sort() return names
class ApplicationDelegate(NSObject): def init(self): self = super(ApplicationDelegate, self).init() self.hotkeys = dict() self.key_hooks = list() self.register_hotkey('ctrl-alt- ', self.show_launcher) self.register_hotkey('fn-alt-q', self.quit) return self def applicationDidFinishLaunching_(self, _): NSLog("applicationDidFinishLaunching_") if not axapi.ax_enabled(): NSLog("Accessibility Permissions not enabled") chili.ui.prompt( 'Accessibility permissions are required for Chili to run.\n' + 'The Security preferences will now open, Please go to "Privacy" pane and choose "Accessibility" on the left panel.\n' + 'Check the box next to Chili and run the app again.') chili.open('/System/Library/PreferencePanes/Security.prefPane') self.quit() return try: chili.load_user_settings() except Exception, e: self.alert_exception(e) # launcher window view controller self.ctrl = LauncherController.alloc().init() self.tap = EventTap(self.global_event, self.should_block_event) # regsiter for notifications on deactivated apps # this is useful for determining the app that was used # before the launcher window was popped up delegate = lambda notification: self.notification(notification) NSWorkspace.sharedWorkspace().notificationCenter() \ .addObserverForName_object_queue_usingBlock_( 'NSWorkspaceDidDeactivateApplicationNotification', None, None, delegate) chili.set_launcher(self.ctrl) n = NSUserNotification.alloc().init() n.setTitle_('Chili') n.setInformativeText_('Ready') NSUserNotificationCenter.defaultUserNotificationCenter() \ .deliverNotification_(n)
def add_distributed_notification(event, event_config, dist_center): if "class" in event_config: obj = get_handler_object(event_config['class']) objc_method = "on%s:" % event py_method = objc_method.replace(":", "_") if not hasattr(obj, py_method) or not callable(getattr(obj, py_method)): print >> sys.stderr, \ "NSDistributedNotification %s: handler class %s must define a %s method" % (event, event_config['class'], py_method) sys.exit(1) dist_center.addObserver_selector_name_object_(obj, objc_method, event, None) else: shouldrm = False process_event = {} if not event in DISTRIBUTED_IDS: DISTRIBUTED_IDS[event] = event_config handler = NSNotificationHandler.new() handler.name = "NSDistributed %s" % event if "process" in event_config: notification_center = NSWorkspace.sharedWorkspace().notificationCenter() process_event["process"] = event_config["process"] process_event["event"] = event process_event["event_config"] = event_config logging.info("Adding Process Monitor: %s" % process_event["process"]) if not event in RELAUNCH_IDS: add_workspace_notification("NSWorkspaceDidLaunchApplicationNotification", process_event, notification_center) RELAUNCH_IDS[event] = process_event else: shouldrm = True handler.callable = get_callable_for_event(event, event_config, context=handler.name) assert(callable(handler.onNotification_)) event_name = event if event == '*': event_name = None if not shouldrm: dist_center.addObserver_selector_name_object_(handler, "onNotification:", event_name, None) else: del DISTRIBUTED_IDS[event] dist_center.removeObserver_name_object_(handler, "onNotification", event_name, None)
def add_workspace_notifications(nsw_config): # See http://developer.apple.com/documentation/Cocoa/Conceptual/Workspace/Workspace.html notification_center = NSWorkspace.sharedWorkspace().notificationCenter() class NotificationHandler(NSObject): """Simple object for handling NSNotification events""" def __init__(self): self.callable = compile( 'raise NotImplementedError("No callable provided!")') def onNotification_(self, the_notification): if the_notification.userInfo: user_info = the_notification.userInfo() else: user_info = None self.callable(user_info=user_info) for event in nsw_config: event_config = nsw_config[event] if "class" in event_config: obj = get_handler_object(event_config['class']) objc_method = "on%s:" % event py_method = objc_method.replace(":", "_") if not hasattr(obj, py_method) or not callable( getattr(obj, py_method)): print >> sys.stderr, \ "NSWorkspace Notification %s: handler class %s must define a %s method" % (event, event_config['class'], py_method) sys.exit(1) notification_center.addObserver_selector_name_object_( obj, objc_method, event, None) else: handler = NotificationHandler.new() handler.name = "NSWorkspace Notification %s" % event handler.callable = get_callable_for_event(event, event_config, context=handler.name) assert (callable(handler.onNotification_)) notification_center.addObserver_selector_name_object_( handler, "onNotification:", event, None) log_list("Listening for these NSWorkspace notifications: %s", nsw_config.keys())
def force_quit(appname, blocking=False): ''' Force an application to quit aka `Force Quit`. Does not block until the application quits. :param application: :return: ''' workSpace = NSWorkspace.sharedWorkspace() applications = workSpace.runningApplications() for app in applications: if app.localizedName() == appname: acknowledged = app.forceTerminate() return acknowledged return None
def open_url(url): ''' Open a URL in the current users session. This works with anything that command-k would accept, such as: afp, smb, vnc, http CLI Example:: salt '*' desktop.open_url 'afp://server.local' ''' workSpace = NSWorkspace.sharedWorkspace() url = NSURL.URLWithString_(url) if url is None: log.error('Invalid URL given: %s' % url) return False status = workSpace.openURL_(url) return status
def quit(appname, blocking=False): ''' Ask an application to quit. Does not guarantee that the application will quit without user interaction. Does not block until the application quits. :param application: :return: ''' workSpace = NSWorkspace.sharedWorkspace() applications = workSpace.runningApplications() for app in applications: if app.localizedName() == appname: acknowledged = app.terminate() return acknowledged return None
def force_quit(appname, blocking=False): ''' Force an application to quit aka `Force Quit`. Does not block until the application quits. CLI Example:: salt '*' app.force_quit 'Safari' ''' workSpace = NSWorkspace.sharedWorkspace() applications = workSpace.runningApplications() for app in applications: if app.localizedName() == appname: acknowledged = app.forceTerminate() return acknowledged return None
def set_wallpaper(screen_index, path): ''' Set desktop wallpaper for screen at index CLI Example: .. code-block:: bash salt '*' desktop.set_wallpaper 0 '/Library/Desktop Pictures/Solid Colors/Solid Aqua Graphite.png' ''' workspace = NSWorkspace.sharedWorkspace() screens = NSScreen.screens() screen = screens[screen_index] file_url = NSURL.fileURLWithPath_isDirectory_(path, False) options = {} (status, error) = workspace.setDesktopImageURL_forScreen_options_error_(file_url, screen, options, None) return status
def set_wallpaper(screen_index, path): ''' Set desktop wallpaper for screen at index CLI Example: .. code-block:: bash salt '*' desktop.set_wallpaper 0 '/Library/Desktop Pictures/Solid Colors/Solid Aqua Graphite.png' ''' workspace = NSWorkspace.sharedWorkspace() screens = NSScreen.screens() screen = screens[screen_index] file_url = NSURL.fileURLWithPath_isDirectory_(path, False) options = {} (status, error) = workspace.setDesktopImageURL_forScreen_options_error_( file_url, screen, options, None) return status
def quit(appname, blocking=False): ''' Ask an application to quit. Does not guarantee that the application will quit without user interaction. Does not block until the application quits. CLI Example:: salt '*' app.quit 'Safari' ''' workSpace = NSWorkspace.sharedWorkspace() applications = workSpace.runningApplications() for app in applications: if app.localizedName() == appname: acknowledged = app.terminate() return acknowledged return None
def add_workspace_notifications(nsw_config): # See http://developer.apple.com/documentation/Cocoa/Conceptual/Workspace/Workspace.html notification_center = NSWorkspace.sharedWorkspace().notificationCenter() class NotificationHandler(NSObject): """Simple object for handling NSNotification events""" def __init__(self): self.callable = compile('raise NotImplementedError("No callable provided!")') def onNotification_(self, the_notification): if the_notification.userInfo: user_info = the_notification.userInfo() else: user_info = None self.callable(user_info=user_info) for event in nsw_config: event_config = nsw_config[event] if "class" in event_config: obj = get_handler_object(event_config['class']) objc_method = "on%s:" % event py_method = objc_method.replace(":", "_") if not hasattr(obj, py_method) or not callable(getattr(obj, py_method)): print >> sys.stderr, \ "NSWorkspace Notification %s: handler class %s must define a %s method" % (event, event_config['class'], py_method) sys.exit(1) notification_center.addObserver_selector_name_object_(obj, objc_method, event, None) else: handler = NotificationHandler.new() handler.name = "NSWorkspace Notification %s" % event handler.callable = get_callable_for_event(event, event_config, context=handler.name) assert(callable(handler.onNotification_)) notification_center.addObserver_selector_name_object_(handler, "onNotification:", event, None) log_list("Listening for these NSWorkspace notifications: %s", nsw_config.keys())
def _screenImageOptions(screen): ''' Process an instance of NSScreen, returning its options as a hash ''' workspace = NSWorkspace.sharedWorkspace() if screen == NSScreen.mainScreen(): is_main = True else: is_main = False screen_options = workspace.desktopImageOptionsForScreen_(screen) scaling_factor = screen_options.objectForKey_(NSWorkspaceDesktopImageScalingKey) # NSImageScaling : NSNumber allow_clipping = screen_options.objectForKey_(NSWorkspaceDesktopImageAllowClippingKey) # NSNumber fill_color = screen_options.objectForKey_(NSWorkspaceDesktopImageFillColorKey) # NSColor image_url = workspace.desktopImageURLForScreen_(screen).absoluteString() # NSURL options_dict = {'main': is_main, 'scaling_factor': scaling_factor, 'image_url': image_url, 'allow_clipping': allow_clipping} if fill_color: options_dict['fill_color'] = {'r': fill_color.redComponent(), 'g': fill_color.greenComponent(), 'b': fill_color.blueComponent()} return options_dict
def open(self, path, line): from Cocoa import NSWorkspace NSWorkspace.sharedWorkspace().openFile_(path) return True
def labels(): ''' Get Finder labels (Not including user defined tags, they are identified by NSURLTagNamesKey) ''' workSpace = NSWorkspace.sharedWorkspace() return list(workSpace.fileLabels())
def launch(app): workspace = NSWorkspace.sharedWorkspace() workspace.launchApplication_(app)
def open(file): workspace = NSWorkspace.sharedWorkspace() workspace.openFile_(file)
# Find the path to the just inserted volume path = aNotification.userInfo()["NSDevicePath"] for aFile in os.listdir(path): if readTheseFiles.match(aFile): # Found a readme file, try to open it using the Workspace API fullPath = os.path.join(path, aFile) success, app, _ = workspace.getInfoForFile_application_type_(fullPath) if not success: NSLog("Failed to find application to open file %s", fullPath) return workspace.openFile_withApplication_(fullPath, app) # Create an instance of our notification handler, and ask the workspace # notification center to tell us when a new volume is mounted. workspace = NSWorkspace.sharedWorkspace() notificationCenter = workspace.notificationCenter() notificationHandler = NotificationHandler.new() notificationCenter.addObserver_selector_name_object_( notificationHandler, "handleMountNotification:", NSWorkspaceDidMountNotification, None, ) NSLog("Listening for mount notifications....") AppHelper.runConsoleEventLoop()
def open_with(file, app): workspace = NSWorkspace.sharedWorkspace() workspace.openFile_withApplication_(file, app)
def open_url(url): workspace = NSWorkspace.sharedWorkspace() workspace.openURL_(NSURL.URLWithString_(url))
def GetForegroundWindow(): return NSWorkspace.sharedWorkspace().frontmostApplication( ).processIdentifier()