def items(context): ''' Get a list of 'Login Items' context The shared file list context: 'user' (meaning the current session) or 'system'. The default login item list will be the 'system' list. CLI Example: .. code-block:: bash salt '*' login.items [context] ''' if context == 'user': defined_context = kLSSharedFileListSessionLoginItems else: defined_context = kLSSharedFileListGlobalLoginItems log.info('Getting login items for the %s context', 'user' if context == 'user' else 'system') lst = LSSharedFileListCreate(None, defined_context, None) snapshot, seed = LSSharedFileListCopySnapshot(lst, None) # snapshot is CFArray log.info('Login item display names:') login_items = [LSSharedFileListItemCopyDisplayName(item) for item in snapshot] log.info(login_items) return login_items
def __init__(self): self.sflRef = LSSharedFileListCreate(kCFAllocatorDefault, kLSSharedFileListFavoriteItems, None) self.snapshot = LSSharedFileListCopySnapshot(self.sflRef, None) self.favorites = dict() self.update()
def _get_login_items(): # Setup the type of shared list reference we want list_ref = LSSharedFileListCreate(None, kLSSharedFileListSessionLoginItems, None) # Get the user's login items - actually returns two values, with the second being a seed value # indicating when the snapshot was taken (which is safe to ignore here) login_items,_ = LSSharedFileListCopySnapshot(list_ref, None) return [list_ref, login_items]
def open_app_at_startup(enabled=True): """ This function adds/removes the current app bundle from Login items in macOS """ if sys.platform == 'darwin': from Foundation import NSDictionary from Cocoa import NSBundle, NSURL from CoreFoundation import kCFAllocatorDefault # CF = CDLL(find_library('CoreFoundation')) from LaunchServices import (LSSharedFileListCreate, kLSSharedFileListSessionLoginItems, LSSharedFileListInsertItemURL, kLSSharedFileListItemHidden, kLSSharedFileListItemLast, LSSharedFileListItemRemove) app_path = NSBundle.mainBundle().bundlePath() url = NSURL.alloc().initFileURLWithPath_(app_path) login_items = LSSharedFileListCreate( kCFAllocatorDefault, kLSSharedFileListSessionLoginItems, None) props = NSDictionary.dictionaryWithObject_forKey_( True, kLSSharedFileListItemHidden) new_item = LSSharedFileListInsertItemURL(login_items, kLSSharedFileListItemLast, None, None, url, props, None) if not enabled: LSSharedFileListItemRemove(login_items, new_item)
def register_folder_link_darwin(self, folder_path): try: from LaunchServices import LSSharedFileListCreate from LaunchServices import kLSSharedFileListFavoriteItems from LaunchServices import LSSharedFileListInsertItemURL from LaunchServices import kLSSharedFileListItemBeforeFirst from LaunchServices import CFURLCreateWithString except ImportError: log.warning("PyObjC package is not installed:" " skipping favorite link creation") return folder_path = normalized_path(folder_path) folder_name = os.path.basename(folder_path) lst = LSSharedFileListCreate(None, kLSSharedFileListFavoriteItems, None) if lst is None: log.warning("Could not fetch the Finder favorite list.") return url = CFURLCreateWithString(None, "file://" + quote(folder_path), None) if url is None: log.warning("Could not generate valid favorite URL for: %s", folder_path) return # Register the folder as favorite if not already there item = LSSharedFileListInsertItemURL(lst, kLSSharedFileListItemBeforeFirst, folder_name, None, url, {}, []) if item is not None: log.debug("Registered new favorite in Finder for: %s", folder_path)
def process(self): # Veriify if the path provided exists if not os.path.exists(self.path): raise ActionError('the path provided could not be found') # Create the new item's bookmark and properties url = NSURL.fileURLWithPath_(self.path) properties = {'com.apple.loginitem.HideOnLaunch': self.hidden} # Find a specific login item login_items = LSSharedFileListCreate( None, kLSSharedFileListSessionLoginItems, None) if self.state == 'present': # Search for the login item in the existing login items for login_item in login_items.allItems(): login_item_url, error = LSSharedFileListItemCopyResolvedURL( login_item, 0, None) # The item path was found and has the same hidden setting if (not error and login_item_url.path() == url.path() and login_item.properties() ['com.apple.loginitem.HideOnLaunch'] == self.hidden): return self.ok() # Add (or update) the login item to the list LSSharedFileListInsertItemURL(login_items, kLSSharedFileListItemLast, None, None, url, properties, None) return self.changed() else: # 'absent' # Search for the login item in the existing login items found_item = None for login_item in login_items.allItems(): login_item_url, error = LSSharedFileListItemCopyResolvedURL( login_item, 0, None) # The item path was found so we delete it if not error and login_item_url.path() == url.path(): found_item = login_item break if not found_item: return self.ok() LSSharedFileListItemRemove(login_items, found_item) return self.changed()
def open_app_at_startup(enabled=True): """ This function adds/removes the current app bundle from Login items in macOS or most Linux desktops """ if sys.platform == 'darwin': from Foundation import NSDictionary from Cocoa import NSBundle, NSURL from CoreFoundation import kCFAllocatorDefault # CF = CDLL(find_library('CoreFoundation')) from LaunchServices import (LSSharedFileListCreate, kLSSharedFileListSessionLoginItems, LSSharedFileListInsertItemURL, kLSSharedFileListItemHidden, kLSSharedFileListItemLast, LSSharedFileListItemRemove) app_path = NSBundle.mainBundle().bundlePath() url = NSURL.alloc().initFileURLWithPath_(app_path) login_items = LSSharedFileListCreate( kCFAllocatorDefault, kLSSharedFileListSessionLoginItems, None) props = NSDictionary.dictionaryWithObject_forKey_( True, kLSSharedFileListItemHidden) new_item = LSSharedFileListInsertItemURL(login_items, kLSSharedFileListItemLast, None, None, url, props, None) if not enabled: LSSharedFileListItemRemove(login_items, new_item) elif sys.platform.startswith('linux'): autostart_path = Path.home() / '.config' / 'autostart' if not autostart_path.exists(): autostart_path.mkdir() autostart_file_path = autostart_path / 'vorta.desktop' if enabled: if Path('/.flatpak-info').exists(): # Vorta runs as flatpak autostart_file_path.write_text( LINUX_STARTUP_FILE.format( 'flatpak run com.borgbase.vorta')) else: autostart_file_path.write_text( LINUX_STARTUP_FILE.format('vorta')) else: if autostart_file_path.exists(): autostart_file_path.unlink()
def devices(): ''' Get items listed as Finder devices, this normally appears on the sidebar. This would normally return mounted devices for root, but since mounts are usually system wide, this will be ok. CLI Example:: salt '*' finder.devices ''' lst = LSSharedFileListCreate(None, kLSSharedFileListFavoriteVolumes, None) snapshot, seed = LSSharedFileListCopySnapshot(lst, None) # snapshot is CFArray return [LSSharedFileListItemCopyDisplayName(item) for item in snapshot]
def favorites(): ''' Get items listed as Finder favorites, this normally appears on the sidebar. Because the minion runs as root, this function returns sidebar items for root which is not terribly useful. CLI Example:: salt '*' finder.favorites ''' lst = LSSharedFileListCreate(None, kLSSharedFileListFavoriteItems, None) snapshot, seed = LSSharedFileListCopySnapshot(lst, None) # snapshot is CFArray return [LSSharedFileListItemCopyDisplayName(item) for item in snapshot]
def open_app_at_startup(enabled=True): """ This function adds/removes the current app bundle from Login items in macOS or most Linux desktops """ if sys.platform == 'darwin': from Foundation import NSDictionary from Cocoa import NSBundle, NSURL from CoreFoundation import kCFAllocatorDefault # CF = CDLL(find_library('CoreFoundation')) from LaunchServices import (LSSharedFileListCreate, kLSSharedFileListSessionLoginItems, LSSharedFileListInsertItemURL, kLSSharedFileListItemHidden, kLSSharedFileListItemLast, LSSharedFileListItemRemove) app_path = NSBundle.mainBundle().bundlePath() url = NSURL.alloc().initFileURLWithPath_(app_path) login_items = LSSharedFileListCreate( kCFAllocatorDefault, kLSSharedFileListSessionLoginItems, None) props = NSDictionary.dictionaryWithObject_forKey_( True, kLSSharedFileListItemHidden) new_item = LSSharedFileListInsertItemURL(login_items, kLSSharedFileListItemLast, None, None, url, props, None) if not enabled: LSSharedFileListItemRemove(login_items, new_item) elif sys.platform.startswith('linux'): config_path = QtCore.QStandardPaths.writableLocation( QtCore.QStandardPaths.ConfigLocation) autostart_file_path = Path(config_path) / 'autostart' / 'vorta.desktop' if enabled: dir_entry_point = get_setuptools_script_dir() autostart_file_path.write_text( LINUX_STARTUP_FILE.format(dir_entry_point)) else: if autostart_file_path.exists(): autostart_file_path.unlink()
def _get_favorite_list() -> List[str]: return LSSharedFileListCreate(None, kLSSharedFileListFavoriteItems, None)
def open_app_at_startup(enabled=True): """ On macOS, this function adds/removes the current app bundle from Login items while on Linux it adds a .desktop file at ~/.config/autostart """ if sys.platform == 'darwin': from Foundation import NSDictionary from Cocoa import NSBundle, NSURL from CoreFoundation import kCFAllocatorDefault # CF = CDLL(find_library('CoreFoundation')) from LaunchServices import (LSSharedFileListCreate, kLSSharedFileListSessionLoginItems, LSSharedFileListInsertItemURL, kLSSharedFileListItemHidden, kLSSharedFileListItemLast, LSSharedFileListItemRemove) app_path = NSBundle.mainBundle().bundlePath() url = NSURL.alloc().initFileURLWithPath_(app_path) login_items = LSSharedFileListCreate( kCFAllocatorDefault, kLSSharedFileListSessionLoginItems, None) props = NSDictionary.dictionaryWithObject_forKey_( True, kLSSharedFileListItemHidden) new_item = LSSharedFileListInsertItemURL(login_items, kLSSharedFileListItemLast, None, None, url, props, None) if not enabled: LSSharedFileListItemRemove(login_items, new_item) elif sys.platform.startswith('linux'): from appdirs import user_config_dir from pathlib import Path is_flatpak = Path('/.flatpak-info').exists() with open( Path(__file__).parent / "assets/metadata/com.borgbase.Vorta.desktop") as desktop_file: desktop_file_text = desktop_file.read() # Find XDG_CONFIG_HOME unless when running in flatpak if is_flatpak: autostart_path = Path.home() / '.config' / 'autostart' else: autostart_path = Path(user_config_dir("autostart")) if not autostart_path.exists(): autostart_path.mkdir(parents=True, exist_ok=True) autostart_file_path = autostart_path / 'vorta.desktop' if enabled: # Replace command for flatpak if appropriate and start in background desktop_file_text = desktop_file_text.replace( "Exec=vorta", "Exec=flatpak run com.borgbase.Vorta --daemonize" if is_flatpak else "Exec=vorta --daemonize") # Add autostart delay desktop_file_text += (AUTOSTART_DELAY) autostart_file_path.write_text(desktop_file_text) elif autostart_file_path.exists(): autostart_file_path.unlink()
def _get_favorite_list(self): from LaunchServices import LSSharedFileListCreate from LaunchServices import kLSSharedFileListFavoriteItems return LSSharedFileListCreate(None, kLSSharedFileListFavoriteItems, None)