StartProgram - desktop-entry-lib 5.0 documentation

Skip to content
Back to top
View this page

# StartProgram

    import desktop_entry_lib
    import subprocess
    import sys
    import os

    def start_firefox() -> None:
        collection = desktop_entry_lib.DesktopEntryCollection()

        collection.load_menu()

        if "org.mozilla.firefox" not in collection:
            print("Firefox was not found", file=sys.stderr)
            return

        entry = collection["org.mozilla.firefox"]
        command = entry.get_command()

        subprocess.run(command, cwd=entry.get_working_directory())

    def open_url_in_firefox() -> None:
        collection = desktop_entry_lib.DesktopEntryCollection()

        collection.load_menu()

        if "org.mozilla.firefox" not in collection:
            print("Firefox was not found", file=sys.stderr)
            return

        entry = collection["org.mozilla.firefox"]
        command = entry.get_command(url_list=["https://example.com"])

        subprocess.run(command, cwd=entry.get_working_directory())

    def open_file_in_kate() -> None:
        collection = desktop_entry_lib.DesktopEntryCollection()

        collection.load_menu()

        if "org.kde.kate" not in collection:
            print("Kate was not found", file=sys.stderr)
            return

        entry = collection["org.kde.kate"]
        command = entry.get_command(file_list=[os.path.abspath("test.txt")])

        subprocess.run(command, cwd=entry.get_working_directory())

    def main() -> None:
        print("Select what you want to do by entering the number")
        print("1: Start Firefox")
        print("2: Open example.com in Firefox")
        print("3: Open test.txt in Kate")

        match input():
            case "1":
                start_firefox()
            case "2":
                open_url_in_firefox()
            case "3":
                open_file_in_kate()
            case _:
                print("Unsupported option", file=sys.stderr)

    if __name__ == "__main__":
        main()

View this example on Codeberg