def _load(dir_path): main = _get_main_file() launchd.load( rule_name=os.path.splitext(os.path.split(main)[-1])[0], rule_path=os.path.abspath(main), path_to_observe=dir_path )
def install(label, plist): ''' Utility function to store a new .plist file and load it :param label: job label :param plist: a property list dictionary ''' fname = launchd.plist.write(label, plist) launchd.load(fname)
def plist_launch_write(label, program_arguments, scope): plist = dict( Label=label, ProgramArguments=program_arguments.split(), RunAtLoad=True, KeepAlive=True, ) job = launchd.LaunchdJob(label) fname = launchd.plist.write(label, plist, scope) launchd.load(fname)
def insert_unload_reload(ar: rumps.MenuItem, j: launchd.LaunchdJob): print(j.plistfilename) ar.add( rumps.MenuItem("Unload", callback=lambda x: launchd.unload(j.plistfilename))) ar.add( rumps.MenuItem("Reload", callback=lambda x: launchd.unload(j.plistfilename) and launchd.load(j.plistfilename)))
def agent_to_menu_item(agent) -> rumps.MenuItem: ag = plistlib.readPlist(agent) label = ag['Label'] ar = rumps.MenuItem("") aj = launchd.LaunchdJob(label) exist = aj.exists() if exist: pid = aj.pid status = aj.laststatus if pid == -1 and status == 0: ar_title = NSAttributedString.alloc().initWithString_attributes_( label, b_attr) ar._menuitem.setAttributedTitle_(ar_title) insert_unload_reload(ar, aj) ar.add( rumps.MenuItem("Start", callback=lambda x: launchctl("start", label))) insert_log_menu_items(ar, ag) ar.add("Idle") ar.add("No Errors") elif pid > 0 and status == 0: ar_title = NSAttributedString.alloc().initWithString_attributes_( label, g_attr) ar._menuitem.setAttributedTitle_(ar_title) insert_unload_reload(ar, aj) ar.add( rumps.MenuItem("Stop", callback=lambda x: launchctl("stop", label))) insert_log_menu_items(ar, ag) ar.add(f"Running ({pid})") ar.add("No Errors") elif status != 0: ar_title = NSAttributedString.alloc().initWithString_attributes_( label, r_attr) ar._menuitem.setAttributedTitle_(ar_title) insert_unload_reload(ar, aj) ar.add( rumps.MenuItem("Start", callback=lambda x: launchctl("start", label))) insert_log_menu_items(ar, ag) ar.add("Stopped") ar.add(f"Error ({status})") else: ar.title = label sub_load = rumps.MenuItem( title="Load", callback=lambda x: launchd.load(f"{USER_AGENTS}/{label}.plist")) ar.add(sub_load) insert_log_menu_items(ar, ag) ar.add("Unloaded") return ar