-
Notifications
You must be signed in to change notification settings - Fork 0
/
rlc
executable file
·62 lines (55 loc) · 1.71 KB
/
rlc
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
#! /bin/bash
SCRIPT=`basename ${BASH_SOURCE[0]}`
ARGS=("$@")
# Print usage and exit
function USAGE {
echo -e "\nUsage: $SCRIPT 'topic'"
echo -e "\nCreate a reading list from documents currently open in an application."
echo -e "\nThe output is a series of comma separated value (CSV) lines written to standard"
echo -e "out consisting of a topic tag and absolute path to an open document/file."
echo -e "\nOptions recognized:"
echo -e "-h -- Print usage message and exit"
echo -e "-f -- Append output to named file (default: write to STDOUT)"
echo -e "-a -- The application name (default: Preview)"
echo -e "-t -- The document/file type opened by the application (default: '.pdf')"
echo -e "\nAll remaining positional (non-option) parameters are concatenated (joined with"
echo -e "a blank separator) and used as the reading list topic tag (default: 'ReadLater')\n"
exit 1
}
# Option defaults
APP="Preview"
TYPE=".pdf"
FILE="STDOUT"
# Command line options
while getopts :ha:t:f: OPT; do
case $OPT in
a)
APP=$OPTARG
;;
t)
TYPE=$OPTARG
;;
f)
FILE=$OPTARG
;;
h)
USAGE
;;
\?)
echo -e "\nInvalid command line option - $OPTARG"
USAGE
;;
esac
done
# Optionally redirect output and append to specified file
[ "$FILE" != "STDOUT" ] && exec >> $FILE
# Join remaining command line arguments
IND=$(expr $OPTIND - 1)
REST=("${ARGS[@]:$IND}")
TOPIC=${REST[@]:-"ReadLater"}
# Get all open files of application run by curent user filtered by document type
FILES=($(lsof -c $APP | grep $TYPE | awk -v user=$USER '{if ($3 == user) print $9}'))
# Associate each file with topic in output
for FILE in ${FILES[@]}; do
echo "$TOPIC, $FILE"
done