Mac::Spotlight::MDQuery - Make a query into OS X Spotlight


  use Mac::Spotlight::MDQuery ':constants';
  use Mac::Spotlight::MDItem ':constants';

  $mdq = new Mac::Spotlight::MDQuery('kMDItemTitle == "*Battlestar*"c');


  @results = $mdq->getResults();
  foreach $r (@results) {
    print $r->get(kMDItemTitle), "\n";
    print $r->get(kMDItemKind), "\n";

    $listref = $r->get(kMDItemAuthors);
    foreach $a (@$listref) {
      print "$a\n";

    if ($r->get(kMDItemStreamable)) {
      print "Content is streamable\n";
    print scalar localtime($r->get(kMDItemContentCreationDate)), "\n";


Mac::Spotlight is primarily accessed through two subpackages MDQuery and MDItem. An MDQuery object is used to run a query and obtain the results. The results are in a list containing zero or more MDItem objects. This POD documents the methods of MDQuery. See the MDItem POD for MDItem's methods and a complete list of all the available Spotlight attributes.



Create a new MDQuery that will run the given query string when it executes. The query string must be supplied to the constructor and cannot be changed once the object is created. For the full format of query strings see the URL For the full list of attributes that can be queried see the POD for MDItem. Do note that unlike the mdfind command, you must provide at least one attribute to be queried. (If you don't provide one to mdfind it picks a few for you.)

new() will return undef if the query string is malformed or if the underlying Core Foundation object cannot be allocated.


setScope() takes a list of zero or more MDQuery constants which define the scope of the query when it is executed. The constants are imported into your current namespace when you use the ':constants' tag. Currently there are three defined constants:


Limit the query to the current user's home directory


Limit the query to all locally mounted volumes


Try to include currently mounted remote volumes in the query

You can do $mdq->setScope() which will effectively stop the query from doing anything when it is executed.


Return the list of scopes set for this query.


Do it! This runs the query and holds the results until they are retrieved with getResults(). If the query fails to start for any reason execute() will return undef. All MDQuery queries are executed synchronously so execute() will not return until the Spotlight query is complete. Once execute() is called on an MDQuery object you cannot call execute() on that object again.


Even though execute() currently runs synchronously, that may not always be the case in the future. Spotlight has the ability to return an initial set of results and then continue to update those results in the background as it finds more matches. Running execute() synchronously tells Spotlight not to return anything until it finds everything. It is still a good idea to call stop() before you call getResults(). If you don't, then if something changes in Mac::Spotlight in the future, you may find that Spotlight is trying to update your search results at the same time as you are trying to access them.


Returns an array of zero or more MDItem objects. Each object represents one filesystem object that matched your query. See the POD for MDItem.


None by default.

Exportable values

If you use the ":constants" tag when you use Mac::Spotlight::MDQuery, you will pull the kMDQuery* constants into your current namespace. If you chose not to you can still access the constants via their fully qualified namespace.




Adrian Hosey, <>

Tatsuhiko Miyagawa <>


Copyright (C) 2005 by Adrian Hosey

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 193:

You forgot a '=back' before '=head2'