Last active
February 9, 2024 15:50
-
-
Save pioz/8bfa1b78f800fb103536400b88a0fa6c to your computer and use it in GitHub Desktop.
Run caffeinate from Mac OS X menu bar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import rumps | |
import subprocess | |
class CaffeinateApp(rumps.App): | |
def __init__(self): | |
super(CaffeinateApp, self).__init__("Caffeinate") | |
self.icon = "inactive_icon.png" | |
self.caffeinate_process = None | |
self.menu = ["Run Caffeinate"] | |
@rumps.clicked("Run Caffeinate") | |
def toggle_caffeinate(self, sender): | |
if self.caffeinate_process is None: | |
self.start_caffeinate(sender) | |
else: | |
self.stop_caffeinate(sender) | |
def start_caffeinate(self, sender): | |
self.caffeinate_process = subprocess.Popen(["caffeinate", "-disu"]) | |
sender.title = "Stop Caffeinate" | |
self.icon = "active_icon.png" | |
def stop_caffeinate(self, sender): | |
self.caffeinate_process.terminate() | |
self.caffeinate_process = None | |
sender.title = "Run Caffeinate" | |
self.icon = "inactive_icon.png" | |
if __name__ == "__main__": | |
app = CaffeinateApp() | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment