]> gitweb.factorcode.org Git - factor.git/blob - extra/spotlight/spotlight.factor
a6a7a671affe07daf01727bd4626584720f1b8c5
[factor.git] / extra / spotlight / spotlight.factor
1 ! Copyright (C) 2013 Charles Alston, John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays formatting help.stylesheet io io.encodings.utf8
4 io.launcher io.pathnames io.styles kernel locals memoize
5 namespaces sequences sequences.generalizations splitting
6 wrap.strings ;
7 IN: spotlight
8
9 ! -----
10 ! TO DO:
11 ! -need to test sudo-mdutil; intercept auth prompt
12 ! -handle case-sensitivity properly (OS X)
13 ! -test composing variant shell command constructions,
14 !  i.e., those which do or don't need spaces, parens,
15 !  single quotes, etc. (work through examples at end of file)
16 ! -access o.e.d & calculator through spotlight
17 ! -emit some sort of 'not-found' msg for unsuccessful search
18 ! -trap errors! ...
19 ! -----
20
21 : attr== ( item-name attr-name -- string )
22     swap "%s == '%s'" sprintf ;
23
24 : attr!= ( item-name attr-name -- string )
25     swap "%s != '%s'" sprintf ;
26
27 : attr> ( item-name attr-name -- string )
28     swap "%s > '%s'" sprintf ;
29
30 : attr>= ( item-name attr-name -- string )
31     swap "%s >= '%s'" sprintf ;
32
33 : attr< ( item-name attr-name -- string )
34     swap "%s < '%s'" sprintf ;
35
36 : attr<= ( item-name attr-name -- string )
37     swap "%s <= '%s'" sprintf ;
38
39 : attr&& ( attr1 attr2 -- string )
40     " && " glue ;
41
42 : attr|| ( attr1 attr2 -- string )
43     " || " glue ;
44
45 <PRIVATE
46
47 : run-process-output ( command -- seq )
48     utf8 [ lines ] with-process-reader ;
49
50 PRIVATE>
51
52 : mdfind ( query -- results )
53     current-directory get "/" or swap
54     "mdfind -onlyin %s %s" sprintf run-process-output ;
55
56 : mdfind. ( query -- )
57     mdfind [ dup <pathname> write-object nl ] each ;
58
59 : mdls ( path -- )
60     absolute-path "mdls" swap 2array run-process-output
61     [ print ] each ;
62
63 : mdutil ( flags on|off volume -- seq )
64     [ "mdfind" swap "-" prepend "-i" ] 2dip 5 narray
65     run-process-output ;
66
67 : mdimport ( path -- seq )
68     absolute-path "mdimport " prepend run-process-output ;
69
70 : mdimport-with ( path options -- seq )
71     swap absolute-path "mdimport %s %s" sprintf run-process-output ;
72
73 MEMO: kMDItems ( -- seq )
74     "mdimport -A" run-process-output
75     [ "'kMDItem" head? ] filter
76     [ "\t" split harvest [ but-last rest ] map ] map ;
77
78 : kMDItems. ( -- )
79     kMDItems table-style get [
80         [
81             [
82                 [ [ 64 wrap-string write ] with-cell ] each
83             ] with-row
84         ] each
85     ] tabular-output nl ;