-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.js
88 lines (79 loc) · 2.42 KB
/
search.js
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
function getDb(dtp, uuid) {
let db = dtp.getRecordWithUuid(uuid);
return db;
}
function search(dtp, query, dbUuids) {
let records = [];
if (dbUuids === null || dbUuids === undefined) {
records = records.concat(dtp.search(query));
} else {
for (let uuid of dbUuids) {
records = records.concat(dtp.search(query, {in: dtp.getRecordWithUuid(uuid)}));
}
}
return records;
}
function test() {
searchOnce({query: 'additionDate<=2020-02-23 17:32:36 name:(~com) tags!=exclude-from-launchbar', field: undefined})
searchOnce({query: 'additionDate>=2020-02-23 17:32:36 OR modificationDate>=2020-02-23 17:32:36 name:(~com) tags!=exclude-from-launchbar', field: 'part'})
}
function run(argv) {
let jsonArgv = JSON.parse(argv[0]);
if (Array.isArray(jsonArgv)) {
let results = [];
for (let arg of jsonArgv) {
results.push(searchOnce(arg));
}
return JSON.stringify(results);
} else {
return JSON.stringify(searchOnce(jsonArgv));
}
}
function searchOnce(jsonArg) {
let dtp = Application("DEVONthink 3");
let field = jsonArg.field;
let dbUuids = jsonArg.dbUuids;
let query = jsonArg.query;
let range = jsonArg.range;
let toJson;
if (field === 'all') {
toJson = (r) => {
let uuid = r.uuid();
return {
name: r.name(),
filename: r.filename(),
path: r.path(),
location: r.location(),
// referenceURL: r.referenceURL(),
score: r.score(),
// thumbnail: r.thumbnail(),
uuid: uuid,
type: r.type(),
kind: r.kind()
}
}
} else if (field === 'part') {
toJson = (r) => {
return {
score: r.score(),
uuid: r.uuid(),
modificationDate: r.modificationDate()
}
}
} else {
toJson = (r) => {
return {};
}
}
// let records = dtp.search(query, {'within': 'titles'});
let records = search(dtp, query, dbUuids);
if (range) {
let [begin, end] = range;
records = records.slice(begin, end);
}
results = records.map(toJson)
// results.sort(function(a, b) {
// return (a.score > b.score) ? -1 : (a.score < b.score) ? 1 : 0;
// });
return results;
}