Verified Commit c1b1cb38 authored by Mark Collins's avatar Mark Collins
Browse files

Fixed up script, readme, and META6 for first release

parent 464cb3f3
{
"perl": "6",
"name": "Mac::Battery::Alerter",
"version": "0.0.2",
"auth": "gitlab:Marcool04",
"description": "Raku script to alert in case of low battery",
"license": "GPL-3.0",
"version": "0.0.4",
"auth": "zef:Marcool04",
"description": "Raku script to alert with speech in case of low battery on macOS",
"license": "WTFPL",
"depends": [
"I18n::Simple"
],
......@@ -17,7 +17,7 @@
"i18n/fr.yml"
],
"authors": [
"Mark Collins <tera_1225@hotmail.com>"
"Mark Collins"
],
"tags": [
"macos",
......@@ -27,5 +27,5 @@
],
"test-depends": [
],
"source-url": "https://gitlab.com/Marcool04/raku-mac-battery-alerter.git"
"source-url": "https://gitlab.com/Marcool04/raku-mac-battery-alerter"
}
# GETTING STARTED
To install the script run:
````
$ zef install Mac::Battery::Alerter
$ battery_alerter.raku --debug
Command line arguments: [--debug]
Will use language code: en
Starting /usr/local/opt/rakudo-star/share/perl6/site/bin/battery_alerter.raku at 2021-07-08T18:20:57.994540+02:00
Getting battery status.
Battery status line: -InternalBattery-0 (id=12386403) 96%; charged; 0:00 remaining present: true
On battery is: False
Percentage string: 96
Currently plugged in (not on battery)
Have shouted is: False
Have not shouted, so nothing to do. Cherrio!
Plugged in action complete. Exiting.
````
To run on a regular basis and get spoken to when battery is low, you can use a plist file similar to this one:
Then test it to see it is functioning properly:
````
$ /raku/install/path/bin/battery_alerter.raku --debug
````
Now, to run the script on a regular basis and get spoken to when the mac's battery is low, you can use a plist file similar to this one:
```xml
<?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">
......@@ -51,8 +47,20 @@ To run on a regular basis and get spoken to when battery is low, you can use a p
</dict>
</dict>
</plist>
```
This should be placed in `~/Library/LaunchAgents/` (for instance) and started using:
````
launchctl enable batteryAlert
launchctl start batteryAlert
````
See https://www.launchd.info/ for more information
**Note:** there are commented out entries in this plist which allow you to debug the service should it not appear to be working. Simply uncomment those parts, reload the service, and check the /tmp/battery_script.txt file for errors.
# DESCRIPTION
This is a small script that interfaces with macOS system utilities to analyse what the current battery status is, and uses speech (macOS `say` utility) to warn when the battery is getting low. This is in response to the fact that there is no audible feedback when the "Low battery" dialog is shown, which is rather late (4% battery) and will give you little time to intervene and plug the mac in should you be looking away at that moment.
......@@ -60,10 +68,10 @@ It is inspired by this post: https://hints.macworld.com/article.php?story=201102
# AUTHOR
Mark Collins <tera_1225@hotmail.com>
Mark Collins (https://gitlab.com/Marcool04)
# COPYRIGHT AND LICENSE
# COPYRIGHT AND LICENCE
Copyright 2020 Mark Collins
This work is free. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2, as published by Sam Hocevar. See the LICENCE file for (almost no) more details.
......@@ -5,44 +5,38 @@ use Mac::Battery::Alerter::i18n;
unit sub MAIN(
#| (Int) Battery percentage under which to warn.
#| Must be > panic, and < panic+10 (default: 5%)
Int :w(:warn($GLOB_warn_percentage)) = 5,
Int :w(:warn($OPT_warn_percentage)) = 5,
#| (Int) Battery percentage under which to panic (default: 4%)
Int :p(:panic($GLOB_panic_percentage)) = 4,
#| (Str) Two letter language code to force a language (default: none),
Str :l(:lang(:$language))?,
Int :p(:panic($OPT_panic_percentage)) = 4,
#| (Str) Two letter language code to force a language (default: $OPT_language, or "en"),
Str :l(:lang(:language($OPT_language))) = (
(%*ENV<LANG>.defined && (%*ENV<LANG> ∉ <en fr>)) ?? %*ENV<LANG>.substr(0..1) !! "en"
),
#| (Bool) Print debug messages (default: False)
Bool :d(:debug($GLOB_debug)) = False,
Bool :d(:debug($OPT_debug)) = False,
#| (Str) A file to store state, used to remind script if it has shouted,
#| and then say thanks (default: "/tmp/battery_script_state_file")
Str :state_file($GLOB_state_file)= "/tmp/battery_script_state_file",
Str :state_file($OPT_state_file)= "/tmp/battery_script_state_file",
#| (Int) The volume to set audio level to before speeking (default: 80%)
Int :s(:sound($GLOB_sound_volume)) = 80,
Int :s(:sound($OPT_sound_volume)) = 80,
);
debug("Command line arguments: " ~ @*ARGS.gist);
# Language setup
my $lang;
if ($language) {
$lang = $language;
} elsif (%*ENV<LANG>.defined) {
$lang = %*ENV<LANG>.substr(0..1);
} else {
$lang = "en";
}
debug("Will use language code: $lang");
my $i18n = Mac::Battery::Alerter::i18n.new(language => $lang);
debug("Will use language code: $OPT_language");
my $i18n = Mac::Battery::Alerter::i18n.new(language => $OPT_language);
# Check warn/panic settings
if ($GLOB_warn_percentage < $GLOB_panic_percentage) {
if ($OPT_warn_percentage < $OPT_panic_percentage) {
say "Invalid combination of warn and panic settings.";
say "Warn: $GLOB_warn_percentage can't be smaller than panic: $GLOB_panic_percentage.";
say "Warn: $OPT_warn_percentage can't be smaller than panic: $OPT_panic_percentage.";
say $*USAGE;
exit;
}
if ($GLOB_warn_percentage > $GLOB_panic_percentage+10) {
if ($OPT_warn_percentage > $OPT_panic_percentage+10) {
say "Invalid combination of warn and panic settings.";
say "Warn: $GLOB_warn_percentage can't be greater than panic+10: "~ ($GLOB_panic_percentage+10);
say "Warn: $OPT_warn_percentage can't be greater than panic+10: "~ ($OPT_panic_percentage+10);
say $*USAGE;
exit;
}
......@@ -65,16 +59,16 @@ if (! $on_battery) {
exit;
}
debug("Currently not plugged in (on battery)");
if ($percentage ≤ $GLOB_warn_percentage) {
debug("Percentage $percentage is bellow $GLOB_warn_percentage. Warning.");
if ($percentage ≤ $OPT_warn_percentage) {
debug("Percentage $percentage is below $OPT_warn_percentage. Warning.");
speek($i18n.tl('low-battery'));
if ($percentage ≤ $GLOB_panic_percentage) {
debug("And even lower than $GLOB_panic_percentage. Panic warning.");
if ($percentage ≤ $OPT_panic_percentage) {
debug("And even lower than $OPT_panic_percentage. Panic warning.");
speek($i18n.tl('panic'));
}
have_shouted(True);
} else {
debug("Percentage $percentage is above $GLOB_warn_percentage. Not doing anything.");
debug("Percentage $percentage is above $OPT_warn_percentage. Not doing anything.");
}
debug("Not plugged in action complete. Exiting.");
exit;
......@@ -86,7 +80,7 @@ exit;
sub debug(
Str $message
) {
say $message if ($GLOB_debug == True);
say $message if ($OPT_debug == True);
}
sub get_battery_status() {
......@@ -146,7 +140,7 @@ multi volume(
}
debug("Setting volume level to $volume_level");
run("osascript", "-e", "set volume output volume $volume_level");
volume(); # just to have debug lines if needed
volume(); # just to print debug lines if needed from a volume status
}
multi volume(
......@@ -156,7 +150,7 @@ multi volume(
my Int $volume = run(
"osascript", "-e", 'output volume of (get volume settings)', :out
).out.slurp.Int;
if ( ! ($volume (elem) 0..100 ) ) {
if ( ! ($volume 0..100 ) ) {
die "Volume $volume from \"osascript output volume of get volume settings\""~
" not a number between 0 and 100"
}
......@@ -168,25 +162,25 @@ sub speek (
Str $this
) {
my Int $old_volume_level = volume();
volume($GLOB_sound_volume);
volume($OPT_sound_volume);
debug("Speeking: $this");
run("say", "-v", $i18n.tl('apple-say-voice'), '"'~$this~'"');
run("say", "-v", $i18n.tl('apple-say-voice'), "\"$this\"");
volume($old_volume_level);
}
multi have_shouted(
Bool $set_to_this
) {
if ($set_to_this eq $GLOB_state_file.IO.e) {
if ($set_to_this eq $OPT_state_file.IO.e) {
debug("Asked to change shouted state but file condition is suitable. Nothing to do.");
return;
}
if ($set_to_this) {
debug("Creating shouted state file.");
$GLOB_state_file.IO.open(:w).spurt("");
$OPT_state_file.IO.open(:w).spurt("");
} else {
debug("Deleting shouted state file.");
$GLOB_state_file.IO.unlink;
$OPT_state_file.IO.unlink;
}
return;
}
......@@ -194,7 +188,7 @@ multi have_shouted(
multi have_shouted(
--> Bool
) {
my Bool $result = $GLOB_state_file.IO.e;
my Bool $result = $OPT_state_file.IO.e;
debug("Have shouted is: $result");
return $result;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment