-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdt_create_group.sh
executable file
·110 lines (88 loc) · 2.23 KB
/
dt_create_group.sh
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/sh
# dt_create_group.sh : Create a new group in the DT database.
# Copyright (c) 2006, Eric Fedel. Released under the BSD License.
# (http://home.earthlink.net/~efedel/code/devon_think/index.html)
HELP_STR="Usage: $0 [-x] group_path
Create options:
-c Check group (state is True)
-o Outline group (show state)
-s path Attach script at POSIX path ('Smart group')
-x Exclude from classification"
EXCLUDE_STR=""
SMART_GROUP=""
SHOW_STATE=""
CHECKED_STATE="set state of r to false"
while getopts cox\?s: opt
do
case "$opt"
in
c) CHECKED_STATE="set state of r to true";;
o) SHOW_STATE="set state visibility of r to true";;
s) SMART_GROUP="set attached script of r to \"$OPTARG\"";;
x) EXCLUDE_STR="set exclude from classification of r to true";;
\?) echo "$HELP_STR"; exit 1;;
esac
done
if [ $((OPTIND)) -gt $# ]
then
echo "ERROR: path argument is mandatory"
exit 2
fi
# Get Params
shift $((OPTIND - 1))
GRP_PATH=$1
# Input validation
if [ -z "$GRP_PATH" ]
then
echo "ERROR: path must be non-NULL"
exit 4
fi
# state cannot be set unless it is also shown
if [ -z "$SHOW_STATE" ]
then
CHECKED_STATE=""
fi
# Command to fill smart group
FILL_SMART_GROUP=""
if [ -n "$SMART_GROUP" ]
then
FILL_SMART_GROUP="
try
tell application \"DEVONthink Pro\"
copy attached script of r to script_path
end tell
set rec_script to load script alias (POSIX file script_path)
tell rec_script to triggered(r)
on error msg
tell application \"DEVONthink Pro\"
log message \"dt_create_group.sh\" info \"Unable to trigger script \" & script_path & \": \" & msg
end tell
end try
"
fi
# Applescript for create
VAR=`osascript <<EOF
set nl to "\\\\\\\\n" -- newline (escaped)
on create_group(path_str)
tell application "DEVONthink Pro"
try
set r to create location path_str
$EXCLUDE_STR
$SHOW_STATE
$CHECKED_STATE
on error msg
return "ERROR: Unable to create " & path_str & ": " & msg & my nl
end try
try
$SMART_GROUP
on error msg
return "ERROR: Unable to attach script to new group " & id of r as text & ": " & msg & my nl
end try
set out to (id of r) as text & my nl
end tell
$FILL_SMART_GROUP
return out
end
set result to create_group("$GRP_PATH")
EOF`
echo -ne $VAR