Expand - Collapse

- Spotlight Programming
  - Spotlight
    - Definition
      - Spotlight is a fast, extensible, desktop search technology that allows users to organize and search for files based on metadata.
    - Technologies
      - Database ("Spotlight Store")
        - meta-data store
        - content index
          - SearchKit
        - one per filesystem
      - Query API
      - Importer plugins
      - Plugin API
  - Concepts
    - Metadata
      - Attribute / Value
        - Metadata attribute
          - predefined
            - File system
            - Image related
            - Video related
            - Audio related
            - "common" attributes
              - author
              - city
              - email
              - organization
              - ...
            - Special Attributes
              - kMDItemPath
                - "expensive" attribute,
                - retrievable, but not use in query
              - lMDItemTextContent
                - use in query but not retrievable
            - Spotlight Metadata Attributes Reference
          - custom
            - only if existing (predefined) key not appropriate
            - Spotlight Importer
          - Properties
            - name
              - Naming Convention
                - reverse DNS naming with _ instead of .
                  - com_apple_mail
            - (Value Object) type
              - CFString
              - CFNumber
              - CFBoolean
              - CFDate
            - multivalue
              - array of values
            - uniqued
              - used when a very small number of values is possible
            - nosearch
              - attribute only searched when specifically declared
              - ignored in wildcard searches
            - Display Name
            - Description
        - Metadata attribute value
          - Localizable
            - Dictionary object instead of String
  - Command Line Tools
    - mdfind
      - finds files matching a given query
      - Example
        - Find all files with any metadata attribute value matching the string "image"
          - mdfind image
        - Find all files that contain "MyFavoriteAuthor" in the kMDItemAuthor metadata attribute
          - mdfind "kMDItemAuthor == '*MyFavoriteAuthor*'"
        - Find all images containing "apple", using the Spotlight menu syntax
          - mdfind -interpret "apple kind:image"
          - Does not seem to perform localization, i.e. mdfind -interpret "apple Art:Bilder" does not work on a german system.
        - Find "live" all files with any metadata attribute value matching the string "skateboard"
          - mdfind -live skateboard
          - The find continues to run after gathering the initial results, providing a count of the number of files that match the query.
      - developer.apple.com > Documentation > Darwin > Reference > ManPages > Man1 > Mdfind.1
    - mdimport
      - import file hierarchies into the metadata datastore
      - Also useful when testing own importers
      - Example
        - list all the currently recognized importers and their paths
          - mdimport -L
        - Print the schema file
          - mdimport -X
        - Test the import for a given file (e.g. when testing/debugging own importers)
          - mdimport -d2 test.myCustomDocument
      - developer.apple.com > Documentation > Darwin > Reference > ManPages > Man1 > Mdimport.1
    - mdls
      - lists the metadata attributes for the specified file
      - Example
        - List the metadata attributes of Dictionary.app
          - mdls /Applications/Dictionary.app
        - Print the ContentType of Dictionary.app
          - mdls -name kMDItemContentType /Applications/Dictionary.app
      - developer.apple.com > Documentation > Darwin > Reference > ManPages > Man1 > Mdls.1.html# > > Apple ref > Doc > Man > 1 > Mdls
    - mdutil
      - manage the metadata stores of mounted volumes
      - Example
        - Display the indexing status of all mounted volumes
          - mdutil -a -s
      - developer.apple.com > Documentation > Darwin > Reference > ManPages > Man1 > Mdutil.1.html# > > Apple ref > Doc > Man > 1 > Mdutil
    - mdcheckschema
      - validates the given mdimporter schema files and indicates whether the schema could be successfully parsed.
      - developer.apple.com > Documentation > Darwin > Reference > ManPages > Man1 > Mdcheckschema.1.html# > > Apple ref > Doc > Man > 1 > Mdcheckschema
  - Query
    - Defining a query
      - Query Expression Syntax
      - Using MDQuery
        - Result items: MDItem
      - Using NSMetadataQuery
        - Use NSPredicate
        - Sorting
        - "value list attributes"
        - Scope
          - URLs
          - directory pathes
          - predefined locations
            - user's home directory
            - locally mounted volumes
            - remotely mounted volumes
        - Result items: NSMetadataItem
    - Running a query
      - 1. Initial results gathering
      - 2. Live-update of results
    - Using the result
      - Disable updates while accessing the result
      - Customize result using callbacks
  - Spotlight Importer
    - Plugin-bundle to extract metadata information from files
      - located in
        - in application's bundle at MyApp.app/Contents/Library/Spotlight
        - ~/Library/Spotlight
        - /Library/Spotlight
        - Framework/PlugIn
        - System provided importers in: /System/Library/Spotlight
    - Custom document formats need their own importer
      - Associated through uniform type identifiers (UTIs)
    - Templates for CFPlugin project and schema file in Xcode
    - Additional Guidelines
      - Don't store metadata in external files
        - metadata in the same file as data
        - System store of metadata "volatile"
      - Import without user interaction
      - Import when application not running
      - User should be able to know/control metadata extracted/stored/searched
        - e.g. personal information
    - Schema.xml
    - Use Xcode's Spotlight Plug-In template to create an import plug-in. The template will generate a unique PLUGIN_ID (UUID) for your importer.
    - Performance
      - Extract metadata quickly and minimal effort
        - Consider this when designing the file format
      - No memory leaks
    - Links
      - Spotlight Importer Programming Guide
      - Sample Code
        - Spotlight
        - SpotlightFortunes
        - ImporterExample
        - SampleImporter
  - The "Spotlight menu" Query Syntax
    - Syntax Elements
      - “AND” “NOT” “OR”
      - ( ... )
      - "..."
      - attributeName:....
        - e.g. kind:music
        - name is localized. E.g. in Germany "Art:Musik"
        - name is case sensitive
          - (i.e. "art:Musik" will not work, but "Art:Musik")
        - For a complete list checkout
          - en
            - /System/Library/Frameworks/CoreServices.framework/Frameworks /Metadata.framework/Resources/English.lproj/schema.string
          - de
            - /System/Library/Frameworks/CoreServices.framework/Frameworks /Metadata.framework/Resources/German.lproj/schema.string
        - For dates special values are defined
          - this week, this month, this year; today, yesterday, tomorrow; next week, next month, next year
      - "<" ">" "-"
        - date:>1/1/07 will find any file modified after January 1, 2007. (The dates you enter must correspond to the short date format you have set in your International preferences.)
          date:<12/31/07 will find any file modified before December 31, 2007.
          date:1/1/07-12/31/07 will find files modified between those two dates.
    - "mdfind -interpret ..." also uses this syntax
  - Spotlight Usage Tips
    - cmd-option-Space
      - Open Spotlight search
    - cmd-Space
      - Open Spotlight menu
      - Return
        - Open selected file in Finder
      - option-Return
        - Show all results in Finder
      - cmd-Return
        - Show selected file in Finder
    - cmd-F
      - Open Spotlight search (from Finder). Only search Files and Folders, not other Spotlight items
    - cmd-F, cmd-F
      - Second search directory is Home (not current directory)
        - macworld.com > Article > 133540 > 2008 > 05 > 105spotlighthome
    - cmd-shift-F
      - Name search
        - macworld.com > Article > 133540 > 2008 > 05 > 105spotlighthome
    - Change the dictionary used for Spotlight definitions
      - macosxhints.com > Article ? ...
      - The first dictionary found in the Dictionary.app (Lexikon) preferences is used
      - For a German-English dictionary checkout BeoLingus
        - tekl.de > English > BeoLingus German-English
    - Spotlight and Timemachine
      - Exclude backup volume from Spotlight
    - Spotlight and "other" Backups
      - Exclude backup volume from Spotlight
      - Make sure, Backup does not overwrite .Spotlight-V100 directory on backup volume
    - Change the default Spotlight Preview zoom level
      - Preview's default zoom when opening PDFs from Spotlight is defined by Preview's property "Root -> Preview -> PVPDFSpotlightSelectionSize" (~/Library -> Preferences -> com.apple.Preview.plist).
        Default: 28 (280%). 10 = 100%
        (Edit properties using e.g. Apple's Property List Editor)
      - macosxhints.com > Article ? ...
      - macosxtips.co.uk > Index files > Category-preview
    - Run Spotlight searches based on Finder labels
      - To find files by label with Spotlight, or in the Finder's find box, type label:1 (or any other number up to 7). The numbers don't seem to correspond exactly to the order in the Finder; but you can see them by doing mdls on files. Here's how each number corresponds to one of the Finder's label colors: 0 - None 1 - Gray 2 - Green 3 - Purple 4 - Blue 5 - Yellow 6 - Red 7 - Orange The actual key name is kMDItemFSLabel in the mdls output.
      - macosxhints.com > Article ? ...
    - Add some "other" search attributes to the menu
      - System files - Include system Files, preference files, plug-ins, etc...
      - Spotlight items - Includes contacts, email, calendars, bookmarks, etc...
    - option-Click on (+) in Search rules row
      - adds an "Any/All/None" subgroup row
      - Can be nested
      - build complex boolean expressions
      - (+) becomes (…)
    - Build-in calculator
    - Start programs
      - Add easy to recall words to Spotlight comment if you always forget the apps name
    - Use 'mdfind -s path' to show contents of smart folder
      - Only documented in "usage" text when running mdfind without parameter
  - Sample Code
    - Developer/Examples/AppKit/Spotlighter
      - sample application demonstrating how to use the Cocoa NSMetadataQuery to access the new Spotlight feature in Mac OS 10.4 Tiger
    - Developer/Examples/Metadata
      - ImporterExample
        - provide a complete example of how to write a metadata importer, along with declaring a UTI for your document as part of your application project.
      - SampleImporter
        - an example importer dealing with Application bundle meta information.
        - Written in C (not Objective-C)
    - http://developer.apple.com/samplecode/SpotlightAPI/
      - Some small sample programs that Show how to use the Spotlight API in your programs.
        - MDItemREf uses MDItemRef to get metadata for a file dragged into the window.
        - MDQueryRef1 does a query using MDQuery
      - developer.apple.com > Samplecode > SpotlightAPI
    - http://developer.apple.com/samplecode/Spotlight
      - Sample code that shows how to write Spotlight Importers.
        - a simple Spotlight importer for the old IFF image file format
        - a Spotlight importer that shows how to define new attributes
        - a Spotlight importer that shows how to deal with a package based file format
      - developer.apple.com > Samplecode > Spotlight
    - http://developer.apple.com/samplecode/SpotlightFortunes
      - Demonstrates how to provision a Spotlight importer plug-in for a custom UTI, and how to use a NSMetadataQuery with bindings to display live query results in a table view.
        - This sample was inspired by the classic Unix fortune program. Instead of reading entries from a monolithic .fortunes file, it uses Spotlight to find all files with the com.example.fortune-file UTI and displays them in a Cocoa table view. Since the focus of this sample is Spotlight's live query support, only minimal support for creating new fortune files is provided.
      - developer.apple.com > Samplecode > SpotlightFortunes
    - http://developer.apple.com/samplecode/PredicateEditorSample
      - demonstrates how to use the NSPredicateEditor class. The NSPredicateEditor class is a subclass of NSRuleEditor that is specialized for editing NSPredicate objects. This sample is intended to show how to use the many different features and aspects of this control and leverages Spotlight to search your Address Book.
      - developer.apple.com > Samplecode > PredicateEditorSample > Index
    - http://developer.apple.com/samplecode/PhotoSearch/
      - uses Spotlight to perform a search for images on your computer. The NSPredicateEditor (introduced in Leopard) is used to create a custom search query.
      - developer.apple.com > Samplecode > PhotoSearch > Index
    - http://developer.apple.com/samplecode/SBSetFinderComment
      - Set/Get Finder Comments using Scripting Bridge
    - http://developer.apple.com/samplecode/WhackedTV/
      - demonstrate the .noindex technique for holding off Spotlight from prematurely indexing files being written by Sequence Grabber. This avoids frame droppage.
      - developer.apple.com > Samplecode > WhackedTV
    - http://developer.apple.com/samplecode/DepartmentAndEmployees/listing4.html
      - extract useful information from the file formats for your document, and set the values into the attribute dictionary for Spotlight to include
    - http://developer.apple.com/samplecode/CoreRecipes/listing29.html
      - use Spotlight to find the other Core Recipes store files on the disk (to load into the "Imported" section of the view.) Here we set up a predicate to match the UTI type (the content type of the file), and then start a metadata query with it. We get notifications each time the content changes.
    - http://developer.apple.com/samplecode/iSpend/
      - Demonstrates how to add Copy and Paste, Drag and Drop, Services, NSDatePicker, sorting, Undo support, NSViewAnimation, and searching support.
      - developer.apple.com > Samplecode > ISpend
  - References
    - Apple
      - Working with Spotlight (Tiger Developer Overview Series)
        - developer.apple.com > Macosx > Spotlight
      - Spotlight Overview
        - developer.apple.com > Documentation > Carbon > Conceptual > MetadataIntro > MetadataIntro
      - Spotlight Query Programming Guide
        - developer.apple.com > Documentation > Carbon > Conceptual > SpotlightQuery > SpotlightQuery
        - Query Expression Syntax
          - developer.apple.com > Documentation > Carbon > Conceptual > Spotlightquery > Concepts > Queryformat
      - Spotlight Metadata Attributes Reference
        - developer.apple.com > Documentation > Carbon > Reference > MetadataAttributesRef > MetadataAttrRef
      - Spotlight Importer Programming Guide
        - developer.apple.com > Documentation > Carbon > Conceptual > MDImporters > MDImporters
      - Spotlight Reference Collection
        - developer.apple.com > Documentation > Carbon > Reference > SpotlightReference Collection
      - Where does Spotlight search?
        - All Home folders (local and network-based, as well as FileVault and non-FileVault). This includes: The Documents, Movies, Music, and Pictures folders The Trash of all users and each mounted volume. ~/Library/Metadata/ ~/Library/Caches/Metadata/ ~/Library/Mail/ ~/Library/Caches/com.apple.AddressBook/Metadata/ ~/Library/PreferencePanes/
          Spotlight also searches these non-Home folder locations by default: /Library/PreferencePanes/ /System/Library/PreferencePanes/ /Applications
        - support.apple.com > Kb > TA23187
      - Spotlight Plugins (Downloads)
        - apple.com > Downloads > Macosx > Spotlight > Index1
      - Search Kit Programming Guide
        - developer.apple.com > DOCUMENTATION > UserExperience > Conceptual > SearchKitConcepts > SearchKit intro > SearchKit intro
    - Third Party
      - Spotlight tips (The X Lab)
        - thexlab.com > Faqs > Stopspotlightindex
      - Spotlight Strikes Back: In Leopard, It Works Great
        - db.tidbits.com > Article > 9283
      - Spotlight’s Secret Search Syntax
        - mcelhearn.com > ? ...
      - Chapter "Advanced Spotlight Menu Searches", OS X 10.5 Missing Manual, David Pogue (excerpt)
        - macosxhints.com > Dlfiles > Spotlight cmds
      - Programming with Spotlight (MacDevCenter.com)
        - macdevcenter.com > Lpt > A > 6034
      - Leopard - Optimizing Spotlight Search
        - usingmac.com > 2007 > 11 > 22 > Leopard-optimizing-spotlight-search
  - Copyright 2009, Udo Borkowski, Aachen