Spotlight’s query expresion syntax is a simplified form of filename globbing familiar to shell users. Queries have the following format:
attribute == value |
where attribute
is a Spotlight metadata attribute (see “Spotlight Metadata Attributes Reference”) or a custom metadata attribute defined by an importer.
For example, to query Spotlight for all the files authored by “Steve” the query would look like the following:
kMDItemAuthors == "Steve"wc |
The available comparison operators are listed in Table 1.
Characters such as "
and ‘
in the value string should be escaped using the \
character.
The search value in the example has the suffix “wc
”. These are modifiers that specify how the Comparison is made. Table 2 describes the available Comparison modifiers.
Table 3 shows several examples that use the Comparison modifiers.
Using the wildcard character (*) you can match substrings at the beginning of a string, end of a string, or anywhere within the string. Table 4 shows several common usages.
Note: Typically, queries should use the “w” modifier to do smart word matching rather than relying on the wildcard character.
Queries can be combined using a C-like syntax for AND (&&) and OR (||). For example, to restrict a Spotlight query to audio files authored by “Steve” the query would be:
kMDItemAuthors == "Steve"wc && kMDItemContentType == "audio"wc |
Parenthesis can be used to further group query matching. For example, to search for audio files authored by “Steve” or “Kevin” the query would be:
(kMDItemAuthors == "Kevin"wc || kMDItemAuthors == "Steve"wc) && |
kMDItemContentType == "audio"wc |
You can expand this search to include video files using the following query:
(kMDItemAuthors == "Kevin"wc || kMDItemAuthors == "Steve"wc ) && |
(kMDItemContentType == "audio"wc || kMDItemContentType == "video"wc ) |
You can also create queries that use the date and time as the search value. The date and time value is formatted as a floating-point value that is compatible with CFDate, seconds relative to January 1, 2001.
Additionally, the $time
variable is provided that can be used to specify values relative to the current time, as shown in Table 5.
Time variable | Description |
---|---|
| The current date and time. |
| The current date. |
| Yesterday’s date. |
| Dates in before this week. |
| Dates in the current week. |
| Dates in the current month. |
| Dates in the current year. |
| The date and time by adding a positive or negative value, in seconds, to the current time. |
| The date by adding a positive or negative value, in days, to the current day |
| Dates by adding a positive or negative value, in weeks, to the current week. |
| Dates by adding a positive or negative value, in months, to the current month. |
| Dates by adding a positive or negative value, in years, to the current year. |
| The date by parsing the specified ISO-8601-STR compliant string. |
Using the $time
variable you can restrict a search to find only files that have been changed in the last week using the following query:
((kMDItemAuthors == "Kevin"wc || kMDItemAuthors = "Steve"wc) && |
(kMDItemContentType == "audio"wc || kMDItemContentType = "video"wc)) && |
(kMDItemFSContentChangeDate > $time.last_week) |
Note: The value of the $time
variable is set when the query is first executed. It does not update to the current time as the query continues to run.
© 2004, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-03-08)