File metadata query expression syntax

File metadata queries are built using a query language that is a subset of the predicate string format. The metadata search expression syntax allows applications to build searches “on the fly,” allowing advanced users to build their own queries. The syntax is relatively simple, including comparisons, language types, and time and date variables. Built from the NSPredicate class

Comparing the grammar

The file metadata query expression syntax is a simplified form of filename wildcard familiar to shell users. The query has the following format:

attribute == value
Copy the code

Attribute is the standard metadata attribute (see File Metadata Attribute Reference) or the importer’s custom metadata attribute.

For example, to query Spotlight for all files created by “Steve”, the query would look like this:

Authors ==[c]"Copy the code

The available comparison operators are listed in Table 3-1.

The operator describe
= = equal
! = Not equal to the
< Less than (numeric and date only)
> Greater than (numeric and date only)
< = Less than or equal to (numeric and date only)
> = Greater than or equal to (numeric and date only)
InRange(attributeName,minValue,maxValue) Specifies the value in the attributeName range from minValue to maxValue

The ‘and’ characters in the value string should be escaped using that character.

The search value in the example has the modifier “C”. These modifiers specify how comparisons are made. Table 3-2 describes the comparison modifiers available.

Search modifiers should be enclosed in square brackets immediately after comparison operators […] .

The modifier describe
c Comparison is case insensitive.
d Is less sensitive to diacritics.

Table 3-3 shows several examples of using comparison modifiers.

Request parameters The results of
kMDItemTextContent == "Paris" Matches “Paris” but does not match “Paris”.
kMDItemTextContent ==[c] "Paris" Match “Paris” and “Paris”.
kMDItemTextContent ==[c] "*Paris*" Match “Paris”, “Paris”, “I love Paris” and “Paris-France.jpg”.
Frederic kMDItemTextContent = = "" Matches “Frederic” but does not match “Frederic”.
Frederic kMDItemTextContent = = [d] "" Match “Frederic” and “Frederic” regardless of case.

Use wildcards (* and?) You can match substrings at the beginning of a string, at the end of a string, or anywhere within a string. Table 3-4 shows several common uses.

The * character matches multiple characters, while? A wildcard matches a single character.

Request parameters The results of
kMDItemTextContent == "paris*" Matches the value of the property beginning with “Paris”. For example, match “Paris”, but not “comparison”.
kMDItemTextContent == "*paris" Matches the value of the property ending in “Paris”.
kMDItemTextContent == "*paris*" Any position in the match value contains the property of “Paris”. For example, match “Paris” and “comparison”.
kMDItemTextContent == "paris" Matches exactly the value of the property of “Paris”.

You can use AND (&) AND OR (| |) portfolio of class C syntax. For example, to limit the query to audio files created by “Steve”, the query would be:

kMDItemAuthors ==[c] "Steve" && kMDItemContentType == "public.audio"
Copy the code

Parentheses can be used to further group query matches. For example, to search for audio files created by “Steve” or “Daniel”, the query would be:

(kMDItemAuthors ==[c] "Daniel" || kMDItemAuthors[c] == "Steve") && kMDItemContentType == "public.audio"
Copy the code

You can extend this search to include video files using the following query:

(kMDItemAuthors ==[c] "Daniel" || kMDItemAuthors ==[c] "Steve" ) && (kMDItemContentType == "public.audio" || kMDItemContentType == "public.video")
Copy the code

Time and date variables

You can also create queries that use date and time as search values. Date and time values are formatted as CFDate compatible floating-point values, with seconds relative to January 1, 2001.

In addition, $time provides variables that you can use to specify values relative to the current time, as shown in Table 3-5.

The time variable describe
$time.now Current date and time.
$time.today Current date.
$time.yesterday Yesterday’s date.
$time.this_week(-1) Date of the previous week.
$time.this_week Midweek date of the current week.
$time.this_month Date of the current month.
$time.this_year The date in the current year.
$time.now(NUMBER) By adding a positive or negative (in seconds) date and time to the current time.
$time.today(NUMBER) Date by adding a positive or negative value (in days) to the current date
$time.this_week(NUMBER) Determine the date by adding a positive or negative value (in weeks) to the current week.
$time.this_month(NUMBER) Determine the date by adding a positive or negative value (in months) to the current month.
$time.this_year(NUMBER) Determine the date by adding a positive or negative value (in years) to the current year.
$time.iso(ISO-8601-STR) Date by parsing the specified ISO-8601-STR compatible string.

Using the $time variable, you can use the following query to limit the search to only find files that have changed in the last week:

((kMDItemAuthors ==[c] "Daniel" || kMDItemAuthors[c] == "Steve") && (kMDItemContentType == "public.audio" || kMDItemContentType == "public.video")) && (kMDItemFSContentChangeDate == $time.this_week(-1))
Copy the code

Note: The value of the $time variable is set when the query is first executed. As the query continues to run, it is not updated to the current time.

\