-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.py
executable file
·114 lines (101 loc) · 3.79 KB
/
action.py
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
108
109
110
111
112
113
114
#!/usr/bin/python3
"""This file is executed when user press enter on one of the search result
"""
import os
import sys
import json
import subprocess
from launchbar import LaunchBar
from devonthink import DEVONthink
from devonthink import DEVONthink
from frequency import Frequency
from logger import logger
from config import UserConfig
items = []
SHORTCUT_PATH = UserConfig.shortcut_path
dt = DEVONthink()
def browse_group(dt, uuid):
logger.debug('browse_group')
items.extend(dt.group(uuid))
print(json.dumps(items))
def create_shortcut(record, is_smart_group=False):
if SHORTCUT_PATH is None:
return
expanded = os.path.expanduser(SHORTCUT_PATH)
os.makedirs(expanded, exist_ok=True)
devonthink_url = dt.get_reference_url(record['uuid'], is_smart_group)
content = """
<?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">
<dict>
<key>URL</key>
<string>{URL}</string>
</dict>
</plist>
""".format(URL=devonthink_url)
with open(os.path.join(expanded, record['name'] + '.inetloc'), 'w') as f:
f.write(content)
def action(record, uuid, candidate_uuids, return_key_to_browse):
logger.debug('Picked record is: ' + str(record))
Frequency().update_frequency(uuid, candidate_uuids)
record_type = record.get('type')
is_smart_group = (record_type == 'smart group')
if record_type in ('group', 'smart group'):
logger.debug('item is group')
if LaunchBar.is_command_key() and LaunchBar.is_alternate_key():
create_shortcut(record, is_smart_group)
LaunchBar.hide()
dt.open_item(uuid)
elif LaunchBar.is_command_key():
LaunchBar.hide()
dt.reveal_item(uuid)
elif LaunchBar.is_alternate_key():
browse_group(dt, uuid)
else:
if return_key_to_browse:
browse_group(dt, uuid)
else:
LaunchBar.hide()
dt.open_item(uuid)
elif record_type == 'bookmark':
if LaunchBar.is_command_key() and LaunchBar.is_alternate_key():
create_shortcut(record, is_smart_group)
LaunchBar.hide()
dt.open_item(uuid)
elif LaunchBar.is_command_key():
LaunchBar.hide()
dt.reveal_item(uuid)
elif LaunchBar.is_alternate_key():
LaunchBar.hide()
dt.open_item(uuid)
elif LaunchBar.is_shift_key():
subprocess.call(['open', os.path.dirname(record['path'])])
else:
subprocess.call(['open', record['path']])
else:
if LaunchBar.is_command_key() and LaunchBar.is_alternate_key():
create_shortcut(record, is_smart_group)
LaunchBar.hide()
dt.open_item(uuid)
elif LaunchBar.is_command_key():
LaunchBar.hide()
dt.reveal_item(uuid)
elif LaunchBar.is_alternate_key():
subprocess.call(['open', record['path']])
elif LaunchBar.is_shift_key():
subprocess.call(['open', os.path.dirname(record['path'])])
else:
LaunchBar.hide()
dt.open_item(uuid)
def main():
argument = json.loads(sys.argv[1])
record = argument['pickedRecord']
uuid = argument['pickedUuid']
candidate_uuids = argument['candidateUuids']
return_key_to_browse = argument.get('returnKeyToBrowseGroup')
action(record, uuid, candidate_uuids, return_key_to_browse)
if __name__ == "__main__":
main()
# record = {"name": "CSI7162 ITS", "kind": "Group", "location": "/", "type": "group", "path": "", "uuid": "0A02D63C-D815-474D-8D14-3897A6D4784C", "filename": "CSI7162 ITS"}
# create_shortcut(record, False)