--- /dev/null
+! Copyright (C) 2013 Charles Alston, John Benediktsson
+! See http://factorcode.org/license.txt for BSD license.
+USING: help.markup help.syntax io.pathnames sequences strings ;
+IN: spotlight
+
+HELP: mdfind
+{ $values { "query" string } { "results" sequence } }
+{ $description
+ "Finds files matching a given query."
+ $nl
+ { $snippet "mdfind [-live] [-count] [-onlyin directory] [-name fileName] query" }
+ $nl
+ "The mdfind command consults the central metadata store and returns a list of files that match the given metadata query. The query can be a string or a query expression."
+ $nl
+ { $subheading "Spotlight Keywords" }
+ "These can be included in the query expression to limit the type of documents returned:"
+ { $table
+ { "Applications" "kind:application, kind:applications, kind:app" }
+ { "Audio/Music" "kind:audio, kind:music" }
+ { "Bookmarks" "kind:bookmark, kind:bookmarks" }
+ { "Contacts" "kind:contact, kind:contacts" }
+ { "Email" "kind:email, kind:emails, kind:mail message, kind:mail messages" }
+ { "Folders" "kind:folder, kind:folders" }
+ { "Fonts" "kind:font, kind:fonts" }
+ { "iCal Events" "kind:event, kind:events" }
+ { "iCal To Dos" "kind:todo, kind:todos, kind:to do, kind:to dos" }
+ { "Images" "kind:image, kind:images" }
+ { "Movies" "kind:movie, kind:movies" }
+ { "PDF" "kind:pdf, kind:pdfs" }
+ { "Preferences" "kind:system preferences, kind:preferences" }
+ { "Presentations" "kind:presentations, kind:presentation" }
+ }
+ { $subheading "Date Keywords" }
+ "These can be included in the query expression to limit the age of documents returned:"
+ { $table
+ { "date:today" "$time.today()" }
+ { "date:yesterday" "$time.yesterday()" }
+ { "date:this week" "$time.this_week()" }
+ { "date:this month" "$time.this_month()" }
+ { "date:this year" "$time.this_year()" }
+ { "date:tomorrow" "$time.tomorrow()" }
+ { "date:next month" "$time.next_month()" }
+ { "date:next week" "$time.next_week()" }
+ { "date:next year" "$time.next_year()" }
+ }
+ { $subheading "Boolean Operators" }
+ "By default mdfind will AND together elements of the query string."
+ { $table
+ { "| (OR)" { "to return items that match either word, use the pipe character: " { $snippet "stringA|stringB" } } }
+ { "- (NOT)" { "to exclude documents that match a string: " { $snippet "-string" } } }
+ { "==" "equal" }
+ { "!=" "not equal" }
+ { "< and >" "\"less\" or \"more than\"" }
+ { "<= and >=" "\"less than or equal\" or \"more than or equal\"" }
+ }
+}
+{ $examples
+ "Return all files that have been modified today"
+ { $code "\"date:today\" mdfind" }
+ "Return all files that have been modified in the last 3 days"
+ { $code "\"kMDItemFSContentChangeDate >= $time.today (-3)\" mdfind" }
+ "Returns files with particular attributes"
+ { $code "\"com.microsoft.word.doc\" \"kMDItemContentType\" attr== mdfind" }
+ "Look for files with a particular file name"
+ { $code "\"Finding Joy in Combinators.pdf\" \"kMDItemFSName\" attr== mdfind" }
+ "Look for terms in documents"
+ { $code "\"Document cocoa.messages selector\" mdfind" }
+ "Return all files in the users home folder that have been modified in the last 3 days"
+ { $code "\"~\" [ \"kMDItemFSContentChangeDate >= $time.today (-3)\" mdfind ] with-directory" }
+}
+{ $notes
+ "This word uses the " { $link current-directory } " to restrict the search, choosing to search from the root ('" { $snippet "/" } "') if not set." } ;
+
+HELP: mdls
+{ $values { "path" "string or pathname" } }
+{ $description
+ "Lists the metadata attributes for the specified file."
+ $nl
+ { $snippet "mdls [-name attributeName] [-raw [-nullMarker markerString]] file ..." }
+ $nl
+ "The mdls command prints the values of all the metadata attributes associated with the files provided as an argument."
+} ;
+
+HELP: mdutil
+{ $values { "flags" string } { "on|off" string } { "volume" string } { "seq" sequence } }
+{ $description
+ "Manage the metadata stores used by Spotlight."
+ $nl
+ { $snippet "mdutil [-pEsav] [-i on | off] mountPoint ..." }
+ $nl
+ "The mdutil command is useful for managing the metadata stores for mounted volumes."
+} ;
+
+HELP: mdimport
+{ $values { "path" string } { "seq" sequence } }
+{ $description
+ "Import file hierarchies into the metadata datastore."
+ $nl
+ { $snippet "mdimport [-VXLArgn] [-d level | category] [-w delay] file | directory" }
+ $nl
+ "mdimport is used to test Spotlight plug-ins, list the installed plug-ins and schema, and re-index files handled by a plug-in when a new plug-in is installed."
+} ;
+
+HELP: kMDItems
+{ $values { "seq" sequence } }
+{ $description "Retrieves all the available kMDItemAttributes." } ;
+
+HELP: kMDItems.
+{ $description "Prints a table of all the available kMDItemAttributes." } ;
--- /dev/null
+! Copyright (C) 2013 Charles Alston, John Benediktsson
+! See http://factorcode.org/license.txt for BSD license.
+USING: arrays formatting help.stylesheet io io.encodings.utf8
+io.launcher io.pathnames io.styles kernel locals memoize
+namespaces sequences sequences.generalizations splitting
+wrap.strings ;
+IN: spotlight
+
+! -----
+! TO DO:
+! -need to test sudo-mdutil; intercept auth prompt
+! -handle case-sensitivity properly (OS X)
+! -test composing variant shell command constructions,
+! i.e., those which do or don't need spaces, parens,
+! single quotes, etc. (work through examples at end of file)
+! -access o.e.d & calculator through spotlight
+! -emit some sort of 'not-found' msg for unsuccessful search
+! -trap errors! ...
+! -----
+
+: attr== ( item-name attr-name -- string )
+ swap "%s == '%s'" sprintf ;
+
+: attr!= ( item-name attr-name -- string )
+ swap "%s != '%s'" sprintf ;
+
+: attr> ( item-name attr-name -- string )
+ swap "%s > '%s'" sprintf ;
+
+: attr>= ( item-name attr-name -- string )
+ swap "%s >= '%s'" sprintf ;
+
+: attr< ( item-name attr-name -- string )
+ swap "%s < '%s'" sprintf ;
+
+: attr<= ( item-name attr-name -- string )
+ swap "%s <= '%s'" sprintf ;
+
+: attr&& ( attr1 attr2 -- string )
+ " && " glue ;
+
+: attr|| ( attr1 attr2 -- string )
+ " || " glue ;
+
+<PRIVATE
+
+: run-process-output ( command -- seq )
+ utf8 [ lines ] with-process-reader ;
+
+PRIVATE>
+
+: mdfind ( query -- results )
+ current-directory get "/" or swap
+ "mdfind -onlyin %s %s" sprintf run-process-output ;
+
+: mdfind. ( query -- )
+ mdfind [ dup <pathname> write-object nl ] each ;
+
+: mdls ( path -- )
+ absolute-path "mdls" swap 2array run-process-output
+ [ print ] each ;
+
+: mdutil ( flags on|off volume -- seq )
+ [ "mdfind" swap "-" prepend "-i" ] 2dip 5 narray
+ run-process-output ;
+
+: mdimport ( path -- seq )
+ absolute-path "mdimport " prepend run-process-output ;
+
+: mdimport-with ( path options -- seq )
+ absolute-path swap "mdimport %s %s" sprintf run-process-output ;
+
+MEMO: kMDItems ( -- seq )
+ "mdimport -A" run-process-output
+ [ "'kMDItem" head? ] filter
+ [ "\t" split harvest [ but-last rest ] map ] map ;
+
+: kMDItems. ( -- )
+ kMDItems table-style get [
+ [
+ [
+ [ [ 64 wrap-string write ] with-cell ] each
+ ] with-row
+ ] each
+ ] tabular-output nl ;