The Power of mdfind
Pages: 1, 2, 3, 4
Summary
Remember that mdfind is a file-level utility. It finds files that match, but provides no context for them. It also only provides file-level granularity. For example, since I use Apple's Mail program, which stores individual mail messages as separate files, mdfind returns individual mail messages that match my searches. However, mail programs like Eudora store an entire folder of messages in one file in mbox format. If one message in that box matches a search, mdfind will show the file as a match, but not which message in the file made the match.
I hope you've found this overview of mdfind illuminating. Much of the information has been taken from other articles and comments around the Web, since Apple's documentation on mdfind is so sparse. Here's hoping that a future update to Tiger enhances the documentation.
Appendix A: A Summary of Common Options
From Chapter 2 of Mac OS X Tiger In A Nutshell.
- MDItemAttributeChangeDate
- The date and time that a metadata attribute was last changed.
- MDItemAudiences
- The intended audience of the file.
- MDItemAuthors
- The authors of the document.
- MDItemCity
- The document's city of origin.
- MDItemComment
- Comments regarding the document.
- MDItemContactKeywords
- A list of contacts associated with the document.
- MDItemContentCreationDate
- The document's creation date.
- MDItemContentModificationDate
- Last modification date of the document.
- MDItemContentType
- The qualified content type of the document, such as
com.adobe.pdffor PDF files andcom.apple.protected-mpeg-4-audiofor an Apple Advanced Audio Coding (AAC) file. - MDItemContributors
- Contributors to this document.
- MDItemCopyright
- The copyright owner.
- MDItemCountry
- The document's country of origin.
- MDItemCoverage
- The scope of the document, such as a geographical location or a period of time.
- MDItemCreator
- The application that created the document.
- MDItemDescription
- A description of the document.
- MDItemDueDate
- Due date for the item represented by the document.
- MDItemDurationSeconds
- Duration (in seconds) of the document.
- MDItemEmailAddresses
- Email addresses associated with this document.
- MDItemEncodingApplications
- The name of the application (such as Acrobat Distiller) that was responsible for converting the document in its current form.
- MDItemFinderComment
- This contains any Finder comments for the document.
- MDItemFonts
- Fonts used in the document.
- MDItemHeadline
- A headline-style synopsis of the document.
- MDItemInstantMessageAddresses
- IM addresses/screen names associated with the document.
- MDItemInstructions
- Special instructions or warnings associated with this document.
- MDItemKeywords
- Keywords associated with the document.
- MDItemKind
- Describes the kind of document, such as an iCal Event.
- MDItemLanguages
- Language of the document.
- MDItemLastUsedDate
- The date and time the document was last opened.
- MDItemNumberOfPages
- Page count of this document.
- MDItemOrganizations
- The organization that created the document.
- MDItemPageHeight
- Height of the document's page layout in points.
- MDItemPageWidth
- Width of the document's page layout in points.
- MDItemPhoneNumbers
- Phone numbers associated with the document.
- MDItemProjects
- Names of projects (other documents, such as an iMovie project) that this document is associated with.
- MDItemPublishers
- The publisher of the document.
- MDItemRecipients
- The recipient of the document.
- MDItemRights
- A link to the statement of rights (such as a Creative Commons or old-school copyright license) that govern the use of the document.
- MDItemSecurityMethod
- Encryption method used on the document.
- MDItemStarRating
- Rating of the document (as in the iTunes "star" rating).
- MDItemStateOrProvince
- The document's state or province of origin.
- MDItemTitle
- The title.
- MDItemVersion
- The version number.
- MDItemWhereFroms
- Where the document came from, such as a URI or email address.
Appendix B: Finding Long Songs
Here's a little Perl program to find songs longer than a certain number of minutes, and report on them in a friendly format, in reverse order of length. It uses mdfind to get a list of files for songs over a certain length, and then uses mdls to extract the details, and reports on its findings.
#!/usr/bin/perl -w
use warnings;
use strict;
# Get number of minutes from command line.
my $minutes = shift || 10; # default 10
my $seconds = $minutes * 60;
my @constraints = (
"kMDItemDurationSeconds > $seconds",
'kMDItemMediaTypes == "Sound"',
);
my $mdfind_args = join( " and ", @constraints );
my @filelist = `mdfind '$mdfind_args'`
or die "You don't have any songs over ",
"$minutes minutes long!\n";
chomp @filelist; # Remove trailing newlines
my @fileinfo; # List of matching files & stats
for my $filename ( @filelist ) {
my %fields;
# Call mdls on the file and scan each line
foreach ( qx{mdls "$filename"} ) {
# Find lines with key/value pairs
if ( /^kMDItem(\w+)\s+=\s+(.*)/ ) {
# Extract the keys and values
my ($key,$value) = ($1,$2);
# Strip surrounding parens & quotes
$value =~ s/^\(|\)$//g;
$value =~ s/^"|"$//g;
# Stash the key/value pair
$fields{$key} = $value;
}
} # for each mdls call
push( @fileinfo, \%fields );
}
# Sort in decreasing order of length
@fileinfo = sort {
$b->{DurationSeconds}
<=>
$a->{DurationSeconds}
} @fileinfo;
# Print the specs for each song
for my $file ( @fileinfo ) {
printf( qq{%2d:%02d "%s" by %s from "%s"\n},
$file->{DurationSeconds}/60,
$file->{DurationSeconds}%60,
$file->{Title},
$file->{Authors},
$file->{Album},
);
}
$ perl longsongs 9
20:34 "2112" by Rush from "2112"
18:36 "Alice's Restaurant Massacree" by Arlo
Guthrie from "The Best Of Arlo Guthrie"
...
9:16 "Between I And Thou" by The Mermen from
"A Glorious Lethal Euphoria"
9:05 "Watermelon In Easter Hay" by Frank Zappa
from "Joe's Garage"
9:03 "Slow Burn" by Silkworm from "Even A Blind
Chicken Finds A Kernel Of Corn Now And Then"
9:00 "The Load-Out / Stay" by Jackson Browne from
"Running On Empty"
Andy Lester is a QA & Release Manager for Socialtext. He is also in charge of PR for The Perl Foundation and maintains over 25 modules on CPAN.
Return to the Mac DevCenter
-
perl script in Appendix B ("longsongs")
2006-01-14 13:24:29 movdpoel [View]
-
perl script in Appendix B ("longsongs")
2006-01-14 16:09:18 Andy Lester |
[View]
-
perl script in Appendix B ("longsongs")
2006-01-15 08:57:51 movdpoel [View]
-
mdfind2
2006-01-05 18:09:06 schwa [View]
-
some more examples
2006-01-04 19:01:58 hayne [View]
-
Great stuff
2006-01-04 18:51:39 mnystedt [View]
-
Great stuff
2006-01-04 19:59:55 Andy Lester |
[View]

