-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlock-quote
executable file
·62 lines (53 loc) · 1.79 KB
/
lock-quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env bash
# lock-quote by Amory Meltzer
# Stochastically change the login/lock screen message in macOS using a quotes file
function help {
cat <<END_HELP
Usage: $(basename "$0") [-fsnhH]
-f Provide a path to a file of quotes, one per line. Overrides LOCK_SCREEN_QUOTES.
-s Show the current text used on the login/lock screen. No sudo required. Overrides -n.
-n Show a quote but don't use it. No sudo required.
-h -H This help.
END_HELP
}
# Set the quotes file
# Default to using environment variable
if [[ -n "$LOCK_SCREEN_QUOTES" ]]; then
quotefile="$LOCK_SCREEN_QUOTES"
fi
while getopts 'f:snhH' opt; do
case $opt in
f) quotefile=$OPTARG;;
s) defaults read /Library/Preferences/com.apple.loginwindow.plist LoginwindowText
exit 0;;
n) dryrun=1;;
h|H) help
exit 0;;
*) printf "Invalid option provided, try %s -h\n" "$0" >&2
exit 1;;
esac
done
if [[ ! -e "$quotefile" ]]; then
# Get this script's directory: http://stackoverflow.com/a/246128/2521092
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
quotefile=$DIR/lockscreen_quotes.txt
fi
shuf_command="shuf"
# macOS doesn't have shuf, but coreutils installs gshuf
if [[ ! $(command -v 2>/dev/null "$shuf_command") ]]; then
shuf_command="gshuf"
# If that doesn't exist, approximate it via perl
# https://stackoverflow.com/a/6511327/2521092
if [[ ! $(command -v 2>/dev/null "$shuf_command") ]]; then
function gshuf() {
perl -MList::Util=shuffle -e 'print shuffle(<>);' "$@";
}
fi
fi
quote=$($shuf_command "$quotefile" | head -n 1 | tr -d '\n')
quote=$(sed -Ee 's/\[|\]//g; s/\"//g; s/ - / \]- /' <<< "$quote" | tr ']' '\n')
if [[ -n "$dryrun" ]]; then
echo "$quote"
exit 0
fi
defaults write /Library/Preferences/com.apple.loginwindow.plist LoginwindowText "$quote"