This repository has been archived by the owner on Apr 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
songclip
executable file
·74 lines (69 loc) · 1.66 KB
/
songclip
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
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env bash
set -eu
# Usage
function usage {
cat <<EOD
Usage: $(basename "$0") <subcommand> [option]
now Displays current stream title
list Displays the contents
delete [id] Deletes the title (only one line)
purge Deletes the contents (delete all line)
help Displays the usage
EOD
return 1
}
# Gets the current stream title
function current_stream_title {
title=$(osascript -l JavaScript -e 'Application("iTunes").currentStreamTitle.get()')
if [ "$title" = "null" ]; then
echo "Can't retrieve the current stream title." >&2
return 1
fi
echo "$title"
}
# If the argument is not specified
if [ "$#" -eq 0 ]; then
title=$(current_stream_title)
if grep -Fqs "$title" "$CLIPFILE"; then
echo "Current stream title is already exists." >&2
exit 1
fi
echo "$title" >>"$CLIPFILE"
echo "Clipped: $title"
exit 0
fi
# Divides the processing by sub-command
case "$1" in
now)
title=$(current_stream_title)
echo "Now playing: $title"
;;
list)
cat -n "$CLIPFILE"
;;
delete)
[ "$#" -eq 2 ] || usage
expr "$2" + 1 >/dev/null 2>&1 | true
if [ "${PIPESTATUS[0]}" -lt 2 ]; then
if [ "$2" -ne 0 -a "$2" -le $(awk 'END {print NR}' "$CLIPFILE") ]; then
sed -i "" "${2}d" "$CLIPFILE"
echo "Deleted a line."
exit 0
fi
fi
echo "There is no such line. Please try again." >&2
exit 1
;;
purge)
read -p "Are you sure you want to empty the file? (yes|no): " input
if [ "$input" = "y" -o "$input" = "yes" ]; then
cat /dev/null >"$CLIPFILE"
echo "Purged the contents."
fi
;;
*)
usage
;;
esac
# Done
exit 0