Node.js bindings for macOS Spotlight's
mdimportcommand
The mdimport utility is used to import file hierarchies into the macOS Spotlight metadata datastore. It provides functionality for:
- Importing files and directories into the Spotlight index
- Testing Spotlight plugins
- Listing installed plugins and schema
- Re-indexing files when plugins are updated
import { mdimport } from 'mdfind-node'
// Import a single file
await mdimport('document.pdf')
// Import a directory recursively
await mdimport('~/Documents')
// Test import without modifying index
await mdimport('document.pdf', {
test: true,
debugLevel: '2'
})| Option | Type | Default | Description |
|---|---|---|---|
test |
boolean |
false |
When true, simulates import and returns attributes without modifying index |
debugLevel |
'1' | '2' | '3' |
- | Print debug info (requires test mode) |
outputFile |
string |
- | Store attributes in file (requires test mode) |
showPerformance |
boolean |
false |
Show performance metrics (requires test mode) |
immediate |
boolean |
true |
Force immediate indexing (default behavior) |
recursive |
boolean |
true |
Recursively import directories |
maxBuffer |
number |
512KB |
Maximum output buffer size |
1: Print summary of test import2: Print summary and all attributes (except kMDItemTextContent)3: Print summary and all attributes (including kMDItemTextContent)
import { listImporters } from 'mdfind-node'
const importers = await listImporters()
console.log('Installed importers:', importers)import { reimportForImporter } from 'mdfind-node'
// Reimport all chat files after plugin update
await reimportForImporter('/System/Library/Spotlight/Chat.mdimporter')import { getSchema } from 'mdfind-node'
const schema = await getSchema()
console.log('Spotlight schema:', schema)import { listAttributes } from 'mdfind-node'
const attributes = await listAttributes()
console.log('Available attributes:', attributes)The utility provides detailed error information through the MdimportError class:
import { mdimport, MdimportError } from 'mdfind-node'
try {
await mdimport('document.pdf')
} catch (error) {
if (error instanceof MdimportError) {
console.error('Import failed:', error.message)
console.error('Command output:', error.stderr)
if (error.requiresRoot) {
console.error('Root privileges required')
}
}
}// Get detailed attribute information without modifying index
const result = await mdimport('image.jpg', {
test: true,
debugLevel: '2',
showPerformance: true
})
console.log('Import test result:', result)// Save import results to file
await mdimport('document.pdf', {
test: true,
debugLevel: '3',
outputFile: '~/Desktop/import-results.txt'
})// Import multiple directories
await mdimport(['~/Documents', '~/Pictures', '~/Downloads'])// Force immediate indexing of a new file
await mdimport('new-document.pdf', {
immediate: true
})-
The
-t(test) flag and its dependent options (debugLevel,outputFile,showPerformance) are mutually exclusive with normal import operations. -
Directory imports are always recursive, regardless of the
recursiveoption setting. -
Some stderr output is informational (like locale loading) and doesn't indicate an error.
-
Root privileges may be required for certain operations, particularly when modifying system-level importers.
- mdfind Documentation - File search functionality
- mdls Documentation - Metadata listing
- mdutil Documentation - Index management
- macOS mdimport Manual
- Spotlight Importers Documentation