0% found this document useful (0 votes)
10 views

Python Script To Control Apple Battery Charging - Claude

Uploaded by

noecaffesse
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Python Script To Control Apple Battery Charging - Claude

Uploaded by

noecaffesse
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Python script to control Apple battery charging

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,
}

with open(PLIST_PATH, 'wb') as f:


plistlib.dump(plist_content, f)

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

You might also like