Python Script To Control Apple Battery Charging - Claude
Python Script To Control Apple Battery Charging - Claude
NN write a python3 script thate first time you run it sets it self to run at login automatically
and that when on prevents apple computers from charging the battery over 83 %.It has to
be able to be activated and desactivated via command line
Here's a Python script that attempts to meet your requirements. Please note that this
script requires root privileges to modify system files and control battery charging. Use it
with caution.
python Copy
#!/usr/bin/env python3
import sys
import os
import subprocess
import plistlib
PLIST_PATH = '/Library/LaunchAgents/com.battery.limiter.plist'
SCRIPT_PATH = os.path.abspath(__file__)
def set_autorun():
plist_content = {
'Label': 'com.battery.limiter',
'ProgramArguments': [sys.executable, SCRIPT_PATH, 'run'],
'RunAtLoad': True,
}
print(f"Auto-run at login has been set up. Plist file created at {PLIST_PATH}")
subprocess.run(['launchctl', 'load', PLIST_PATH])
def limit_battery():
subprocess.run(['sudo', 'pmset', '-a', 'chargelimit', '83'])
print("Battery charge limit set to 83%")
def remove_limit():
subprocess.run(['sudo', 'pmset', '-a', 'chargelimit', '100'])
print("Battery charge limit removed")
def check_first_run():
if not os.path.exists(PLIST_PATH):
set_autorun()
def main():
Subscribe to Pro for 5x more usage with Claude 3.5 Sonnet. Subscribe to Pro
if len(sys.argv) < 2:
NN print("Usage: python3 script.py [activate|deactivate|run]")
Reply to Claude...
sys.exit(1)
3.5 Sonnet