Apple Script runs fine, need kill switch The following script runs fine in the /etc folder and called by a plist. It basically says text when the battery is too low. This is a simplified version, the real script is much longer.
set Cap to (do shell script &quot;ioreg -w0 -l | grep ExternalChargeCapable&quot;)

tell Cap to set {wallPower} to {last word of paragraph 1}
if wallPower = &quot;Yes&quot; then
    return 0
else
    set Pct to (do shell script &quot;pmset -g batt | grep -Eo \&quot;\\d+%\&quot; | cut -d% -f1&quot;)
    if Pct ≤ 10 then
        display notification &quot;Batterij 10%. Meteen opladen.&quot; with title &quot;Batterij bijna leeg!&quot; sound name &quot;Sosumi&quot;
        ##      delay 300
        say &quot;Batterij 10%. Meteen opladen.&quot;
    end if
end if 

The problem is that I haven't found a good way for a kill switch. The kill switch will be operated by a user without admin rights.
I am open to any suggestions for a kill switch.
As an example, I found this topic that implements a condition if &quot;Do not disturb&quot; is active. I inserted this in the script and it works just fine in the Script Editor itself, but once the script is copied to the /etc folder and called by a plist the execution appears to halt at this line:
  set statusOfDND to ¬
        (do shell script checkDNDstatusCMD) ¬
            as number as boolean

Not sure why, could be regional settings (I'm using Dutch whereas the original script owner uses English (US).
Crossing the user session barrier is non-trivial. Typically a local host only network connection is used, such as TCP/IP or a Mach port. These are complex to set up with AppleScript.
Below are a couple of reasonable alternatives.
File Based Flag
You could use the existence (or lack of) a file to determine how the script behaves:

On launch, script creates an accessible file at a known path:
/Users/Shared/delete-to-stop-power-script.txt

Periodically, or via kqueue, the script can check the file still exists;

On exit, the script should remove the file.


Using files makes dealing with permissions easier. Use chmod 0666 to allow anyone to delete the file.
The access permissions of the file can be used to ensure only specific groups of users can delete the file.
notifyutil
Use the lesser known notifyutil utility:

notifyutil is a command-line utility for interacting with the notify(3)
notification system and the notifyd(8) server.  It may be used to post
notifications, detect and report notifications, and to examine and set
the state values associated with notification keys.

notifyd allows processes to exchange integer values across processes and user sessions. The format is deliberately simple and constrained to enforce security.
See the linked manual page and Brett Terpstra's Quick Tip: system-wide notifications with notifyutil for samples.
Non-polling
If you need a non-polling alternative, Power Manager can trigger scripts on battery levels, offers a power source scripting API, and non-administrator users can be granted rights to cancel or stop events.
I am a software engineer involved with Power Manager, so feel free to ask me technical questions about how it works.