rumps

repository·master·Indexed 25 days ago

https://github.com/jaredks/rumps

A Python library for creating Ridiculously Uncomplicated macOS status bar applications. It provides a simplified interface for building menu-based apps without requiring deep knowledge of PyObjC, offering features such as the rumps.App subclass, @rumps.clicked decorators, system notifications, alert dialogs, and TextFieldMenuItem for embedded text fields. It also supports bundling into standalone .app bundles using py2app.

Tokens
3.8K
Snippets
17
Records
28
Agent score
85%

What's inside rumps

  1. Overview of rumps for macOS status bar apps

    master
    rumps is a library designed to create 'Ridiculously Uncomplicated Mac os x Python Statusbar apps'. It exposes Objective-C classes as Python classes and functions, simplifying the creation of macOS status bar applications. It is intended as a lightweight alternative to heavy GUI frameworks like PyQt or Tkinter when you only need a simple interface for end-user interaction (such as configuration options or execution switches) in the macOS menu bar.
  2. Create standalone macOS applications with py2app

    master

    To bundle a rumps application into a standalone .app bundle, use the py2app library.

    When configuring your setup.py, you must include 'rumps' in the packages list within the py2app options.

    For status bar-based applications that should run in the background (without a Dock icon or the ability to be tabbed to in the App Switcher), set 'LSUIElement': True in the plist section of your options.

    from setuptools import setup
    
    APP = ['example_class.py']
    DATA_FILES = []
    OPTIONS = {
        'argv_emulation': True,
        'plist': {
            'LSUIElement': True,
        },
        'packages': ['rumps'],
    }
    
    setup(
        app=APP,
        data_files=DATA_FILES,
        options={'py2app': OPTIONS},
        setup_requires=['py2app'],
    )
  3. Configure Python virtual environments for rumps

    master

    It is not recommended to use virtualenv with rumps due to issues with how the Python executable is copied. Instead, use the standard venv module (included with Python 3) to ensure proper functionality.

    python3 -m venv env
  4. Package rumps as a standalone app with py2app

    master

    To create a standalone macOS application, use py2app. Ensure rumps is included in the packages list in your OPTIONS. For background apps (no Dock icon), set 'LSUIElement': True in the plist configuration.

    Example setup.py:

    from setuptools import setup
    
    APP = ['example_class.py']
    DATA_FILES = []
    OPTIONS = {
        'argv_emulation': True,
        'plist': {
            'LSUIElement': True,
        },
        'packages': ['rumps'],
    }
    
    setup(
        app=APP,
        data_files=DATA_FILES,
        options={'py2app': OPTIONS},
        setup_requires=['py2app'],
    )

    Then run the following command to build the app:

    python setup.py py2app
  5. Customize or remove the quit button in rumps.App

    master

    When initializing a rumps.App, you can use the quit_button parameter to customize the text of the quit button or remove it entirely by passing None.

    Warning: If you set quit_button=None, you must provide an alternative way to exit the application (e.g., a menu item that calls rumps.quit_application()), otherwise you will have to force quit the process.

    # Removing the default quit button
    app = rumps.App('Hallo Thar', menu=['Print Something', 'On/Off Test', 'Clean Quit'], quit_button=None)
    
    # Providing an alternative exit method
    @rumps.clicked('Clean Quit')
    def clean_up_before_quit(_):
        rumps.quit_application()
    
    app.run()
  6. View debug messages for py2app bundles

    master

    When testing a .app bundle generated via py2app, using the standard open {your app name}.app command will hide debug messages. To see the output, you must run the executable directly from the terminal.

    If your app is in the default dist folder, run the executable using the following path structure:

    ./dist/{your app name}.app/Contents/MacOS/{your app name}
  7. Create a macOS status bar app with rumps

    master

    To create a macOS status bar application, subclass rumps.App and use decorators like @rumps.clicked(name) to define callback functions for menu items. You can trigger system alerts using rumps.alert() and notifications using rumps.notification(). To start the application, call the .run() method on your class instance.

    import rumps
    
    class AwesomeStatusBarApp(rumps.App):
        @rumps.clicked("Preferences")
        def prefs(self, _):
            rumps.alert("jk! no preferences available!")
    
        @rumps.clicked("Silly button")
        def onoff(self, sender):
            sender.state = not sender.state
    
        @rumps.clicked("Say hi")
        def sayhi(self, _):
            rumps.notification("Awesome title", "amazing subtitle", "hi!!1")
    
    if __name__ == "__main__":
        AwesomeStatusBarApp("Awesome App").run()
  8. Create a basic application using a subclass of rumps.App

    master

    To build a standard rumps application, subclass rumps.App and implement your logic within methods. You can define the menu items in the __init__ method and use the @rumps.clicked decorator to register callback functions for specific menu items.

    import rumps
    
    class AwesomeStatusBarApp(rumps.App):
        def __init__(self):
            super(AwesomeStatusBarApp, self).__init__("Awesome App")
            self.menu = ["Preferences", "Silly button", "Say hi"]
    
        @rumps.clicked("Preferences")
        def prefs(self, _):
            rumps.alert("jk! no preferences available!")
    
        @rumps.clicked("Silly button")
        def onoff(self, sender):
            sender.state = not sender.state
    
        @rumps.clicked("Say hi")
        def sayhi(self, _):
            rumps.notification("Awesome title", "amazing subtitle", "hi!!1")
    
    if __name__ == "__main__":
        AwesomeStatusBarApp().run()