docs » hs.spotlight
This module allows Hammerspoon to preform Spotlight metadata queries.
This module will only be able to perform queries on volumes and folders which are not blocked by the Privacy settings in the System Preferences Spotlight panel.
A Spotlight query consists of two phases: an initial gathering phase where information currently in the Spotlight database is collected and returned, and a live-update phase which occurs after the gathering phase and consists of changes made to the Spotlight database, such as new entries being added, information in existing entries changing, or entities being removed.
The syntax for Spotlight Queries is beyond the scope of this module's documentation. It is a subset of the syntax supported by the Objective-C NSPredicate class. Some references for this syntax can be found at:
- https://developer.apple.com/library/content/documentation/Carbon/Conceptual/SpotlightQuery/Concepts/QueryFormat.html
- https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html
Depending upon the callback messages enabled with the hs.spotlight:callbackMessages method, your callback assigned with the hs.spotlight:setCallback method, you can determine the query phase by noting which messages you have received. During the initial gathering phase, the following callback messages may be observed: "didStart", "inProgress", and "didFinish". Once the initial gathering phase has completed, you will only observe "didUpdate" messages until the query is stopped with the hs.spotlight:stop method.
You can also check to see if the initial gathering phase is in progress with the hs.spotlight:isGathering method.
You can access the individual results of the query with the hs.spotlight:resultAtIndex method. For convenience, metamethods have been added to the spotlightObject which make accessing individual results easier: an individual spotlightItemObject may be accessed from a spotlightObject by treating the spotlightObject like an array; e.g. spotlightObject[n]
will access the n'th spotlightItemObject in the current results.
- Constants - Useful values which cannot be changed
- commonAttributeKeys
- definedSearchScopes
- Constructors - API calls which return an object, typically one that offers API methods
- new
- newWithin
- Methods - API calls which can only be made on an object returned by a constructor
- callbackMessages
- count
- groupedResults
- groupingAttributes
- isGathering
- isRunning
- queryString
- resultAtIndex
- searchScopes
- setCallback
- sortDescriptors
- start
- stop
- updateInterval
- valueListAttributes
- valueLists
Signature | hs.spotlight.commonAttributeKeys[] |
---|---|
Type | Constant |
Description | A list of defined attribute keys as discovered in the macOS 10.12 SDK framework headers. |
Notes |
|
Signature | hs.spotlight.definedSearchScopes[] |
---|---|
Type | Constant |
Description | A table of key-value pairs describing predefined search scopes for Spotlight queries |
Notes |
|
Signature | hs.spotlight.new() -> spotlightObject |
---|---|
Type | Constructor |
Description | Creates a new spotlightObject to use for Spotlight searches. |
Parameters |
|
Returns |
|
Signature | hs.spotlight.newWithin(spotlightObject) -> spotlightObject |
---|---|
Type | Constructor |
Description | Creates a new spotlightObject that limits its searches to the current results of another spotlightObject. |
Parameters |
|
Returns |
|
| Signature | hs.spotlight:callbackMessages([messages]) -> table | spotlightObject
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Get or specify the specific messages that should generate a callback. |
| Parameters |
messages
- an optional table or list of items specifying the specific callback messages that will generate a callback. Defaults to { "didFinish" }.
- if an argument is provided, returns the spotlightObject; otherwise returns the current values
- Valid messages for the table are: "didFinish", "didStart", "didUpdate", and "inProgress". See hs.spotlight:setCallback for more details about the messages.
Signature | hs.spotlight:count() -> integer |
---|---|
Type | Method |
Description | Returns the number of results for the spotlightObject's query |
Parameters |
|
Returns |
|
Notes |
|
Signature | hs.spotlight:groupedResults() -> table |
---|---|
Type | Method |
Description | Returns the grouped results for a Spotlight query. |
Parameters |
|
Returns |
|
Notes |
|
| Signature | hs.spotlight:groupingAttributes([attributes]) -> table | spotlightObject
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Get or set the grouping attributes for the Spotlight query. |
| Parameters |
attributes
- an optional table or list of items specifying the grouping attributes for the Spotlight query. Defaults to an empty array.
- if an argument is provided, returns the spotlightObject; otherwise returns the current values
- Setting this property while a query is running stops the query and discards the current results. The receiver immediately starts a new query.
- Setting this property will increase CPU and memory usage while performing the Spotlight query.
Signature | hs.spotlight:isGathering() -> boolean |
---|---|
Type | Method |
Description | Returns a boolean specifying whether or not the query is in the active gathering phase. |
Parameters |
|
Returns |
|
Notes |
|
Signature | hs.spotlight:isRunning() -> boolean |
---|---|
Type | Method |
Description | Returns a boolean specifying if the query is active or inactive. |
Parameters |
|
Returns |
|
Notes |
|
Signature | hs.spotlight:queryString(query) -> spotlightObject |
---|---|
Type | Method |
Description | Specify the query string for the spotlightObject |
Parameters |
|
Returns |
|
Notes |
|
Signature | hs.spotlight:resultAtIndex(index) -> spotlightItemObject |
---|---|
Type | Method |
Description | Returns the spotlightItemObject at the specified index of the spotlightObject |
Parameters |
|
Returns |
|
Notes |
|
| Signature | hs.spotlight:searchScopes([scope]) -> table | spotlightObject
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Get or set the search scopes allowed for the Spotlight query. |
| Parameters |
scope
- an optional table or list of items specifying the search scope for the Spotlight query. Defaults to an empty array, specifying that the search is not limited in scope.
- if an argument is provided for
scope
, returns the spotlightObject; otherwise returns a table containing the current search scopes.
- Setting this property while a query is running stops the query and discards the current results. The receiver immediately starts a new query.
| Signature | hs.spotlight:setCallback(fn | nil) -> spotlightObject
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Set or remove the callback function for the Spotlight search object. |
| Parameters |
fn
- the function to replace the current callback function. If this argument is an explicit nil, removes the current callback function and does not replace it. The function should expect 2 or 3 arguments and should return none.
- the spotlightObject
- Depending upon the messages set with the hs.spotlight:callbackMessages method, the following callbacks may occur:
| Signature | hs.spotlight:sortDescriptors([attributes]) -> table | spotlightObject
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Get or set the sorting preferences for the results of a Spotlight query. |
| Parameters |
attributes
- an optional table or list of items specifying sort descriptors which affect the sorting order of results for a Spotlight query. Defaults to an empty array.
- if an argument is provided, returns the spotlightObject; otherwise returns the current values
- Setting this property while a query is running stops the query and discards the current results. The receiver immediately starts a new query.
Signature | hs.spotlight:start() -> spotlightObject |
---|---|
Type | Method |
Description | Begin the gathering phase of a Spotlight query. |
Parameters |
|
Returns |
|
Notes |
|
Signature | hs.spotlight:stop() -> spotlightObject |
---|---|
Type | Method |
Description | Stop the Spotlight query. |
Parameters |
|
Returns |
|
Notes |
|
| Signature | hs.spotlight:updateInterval([interval]) -> number | spotlightObject
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Get or set the time interval at which the spotlightObject will send "didUpdate" messages during the initial gathering phase. |
| Parameters |
interval
- an optional number, default 1.0, specifying how often in seconds the "didUpdate" message should be generated during the initial gathering phase of a Spotlight query.
- if an argument is provided, returns the spotlightObject object; otherwise returns the current value.
| Signature | hs.spotlight:valueListAttributes([attributes]) -> table | spotlightObject
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Get or set the attributes for which value list summaries are produced for the Spotlight query. |
| Parameters |
attributes
- an optional table or list of items specifying the attributes for which value list summaries are produced for the Spotlight query. Defaults to an empty array.
- if an argument is provided, returns the spotlightObject; otherwise returns the current values
- Setting this property while a query is running stops the query and discards the current results. The receiver immediately starts a new query.
- Setting this property will increase CPU and memory usage while performing the Spotlight query.
Signature | hs.spotlight:valueLists() -> table |
---|---|
Type | Method |
Description | Returns the value list summaries for the Spotlight query |
Parameters |
|
Returns |
|
Notes |
|