actuallymentor / battery

CLI/GUI for managing the battery charging status for Apple silicon (M1, M32, M3) Macs
MIT License
3.78k stars 160 forks source link

Is there a way to enable charging off on login / startup? #103

Closed anarchy89 closed 1 year ago

anarchy89 commented 1 year ago

Is there a way to run the command battery charging off on every startup?

jakjakob commented 1 year ago

Maybe this thread can help you

anarchy89 commented 1 year ago

Maybe this thread can help you

So what's the syntax for launchd for battery charging off?

kevinm6 commented 1 year ago

Maybe this thread can help you

So what's the syntax for launchd for battery charging off?

This should work, put this in ~/Library/LaunchAgents as <name>.plist:

<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
  <dict>
    <key>Label</key>
    <string>com.battery_charging_off</string>
    <key>ProgramArguments</key>
    <array>
      <string>/usr/local/bin/battery</string>
      <string>charging</string>
      <string>off</string>
    </array>
    <key>RunAtLoad</key>   <!— This run the above array at startup -->
    <true/>
  </dict>
</plist>

then save it and run in a terminal: sudo launchctl load ~/Library/LaunchAgents/<name>.plist

anarchy89 commented 1 year ago

Maybe this thread can help you

So what's the syntax for launchd for battery charging off?

This should work, put this in ~/Library/LaunchAgents as <name>.plist:


<?xml version=\"1.0\" encoding=\"UTF-8\"?>

<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">

<plist version=\"1.0\">

  <dict>

    <key>Label</key>

    <string>com.battery_charging_off</string>

    <key>ProgramArguments</key>

    <array>

      <string>/usr/local/bin/battery</string>

      <string>charging</string>

      <string>off</string>

    </array>

    <key>RunAtLoad</key>   <!— This run the above array at startup -->

    <true/>

  </dict>

</plist>

then save it and run in a terminal: sudo launchctl load ~/Library/LaunchAgents/<name>.plist

Thanks for this, I have another question, can I launch a pop up box everytime the operating system launches, that asks me whether I want to disable charging, if I click yes it will launch the script and no will do nothing. I tried using apple script and adding it as a startup item but I can't get it to work. Can this be used to do something similar ?

kevinm6 commented 1 year ago

It should be possible! You can create a shell script that call Apple script with something like osascript -e "display dialog " and on the output it runs or not battery charging off!

After you create the script you have to change the ProgramArguments in the .plist file

anarchy89 commented 1 year ago

K> It should be possible!

You can create a shell script that call Apple script with something like osascript -e "display dialog " and on the output it runs or not battery charging off!

After you create the script you have to change the ProgramArguments in the .plist file

So like,


osascript - "<<EOF 
on run
  tell application "Terminal"
    do script "battery charging off"
  end tell
end run
EOF

I save it as batteryoff.scpt

Then I the plist file I do

<string>/path/to/batteryoff.scpt</string>?

kevinm6 commented 1 year ago

Ehm, no! You can do something in that way, personally I prefer use shell script more and leave Apple script do only the dialog part.

Something like:

#!/usr/bin/env bash

output=$(osascript -e 'tell app "System Events" to display dialog “Set battery charging off?”')

if [[ $out == “button returned:OK” ]]; then
  /usr/local/bin/battery charging off
fi

Now in the .plist change to:

<?xml version=\"1.0\" encoding=\"UTF-8\"?>

<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
  <dict>
    <key>Label</key>
    <string>com.battery_charging_off</string>
    <key>ProgramArguments</key>
    <array>
      <string>full_path_to_file_above(name)></string>
    </array>
    <key>RunAtLoad</key>
    <true/>
  </dict>
</plist>

Probably there’s some fix to do, but this is the general idea! When the system startup, it should prompt you the dialog and in case of “OK” pressed, it should run battery charging off.

actuallymentor commented 1 year ago

This is a good discussion, though outside the scope of the app. I'll close this issue for my own admin, but feel free to continue discussing!