app.metadata - Spotlight Metadata API

Spotlight metadata queries and search.

Methods

app.metadata.get(path, keys?)

Get Spotlight metadata of a file.

Parameters:

  • path (string) - file path
  • keys (array, optional) - array of metadata key names to return

Returns: table, error - metadata dictionary

-- Get all metadata
local meta, err = app.metadata.get("/path/to/photo.jpg")
if meta then
    app.log.info("Creation date: " .. tostring(meta.kMDItemContentCreationDate))
    app.log.info("Pixel height: " .. tostring(meta.kMDItemPixelHeight))
end

-- Get specific keys
local meta, err = app.metadata.get("/path/to/file", {
    "kMDItemContentType",
    "kMDItemFSSize",
    "kMDItemContentCreationDate"
})

app.metadata.search(query, scope?, limit?)

Spotlight search.

Parameters:

  • query (string) - Spotlight query string (MDQuery format)
  • scope (string, optional) - search scope (directory path)
  • limit (number, optional) - maximum number of results (default 100)

Returns: array<table>, error - search results, each containing path and matched metadata

-- Search for PDF files in the current directory
local results, err = app.metadata.search(
    "kMDItemContentType == 'com.adobe.pdf'",
    context.currentDirectory,
    50
)

-- Search for files containing a keyword
local results = app.metadata.search("kMDItemTextContent == '*swift*'")

-- Search for images modified in the last 7 days
local results = app.metadata.search(
    "kMDItemContentTypeTree == 'public.image' && kMDItemFSContentChangeDate >= $time.today(-7)"
)

Common Metadata Keys

Key Description
kMDItemContentType File type (UTI)
kMDItemFSName File name
kMDItemFSSize File size (bytes)
kMDItemContentCreationDate Creation date
kMDItemContentModificationDate Modification date
kMDItemPixelWidth / kMDItemPixelHeight Image pixel dimensions
kMDItemDurationSeconds Media duration (seconds)
kMDItemAuthors Authors
kMDItemTitle Title

Examples

View Image EXIF Information

function MyPlugin:handleExifInfo(context)
    local file = context.selectedFiles[1]
    local meta, err = app.metadata.get(file, {
        "kMDItemPixelWidth", "kMDItemPixelHeight",
        "kMDItemColorSpace", "kMDItemBitsPerSample",
        "kMDItemFocalLength", "kMDItemExposureTimeSeconds",
        "kMDItemFNumber", "kMDItemISOSpeed"
    })

    if not meta then
        app.dialog.alert("Error", err or "Failed to get metadata")
        return
    end

    local info = ""
    for k, v in pairs(meta) do
        info = info .. k .. ": " .. tostring(v) .. "\n"
    end
    app.dialog.alert("Image Info", info)
end
Developer Documentation
User Guide
Getting Started Script Menus FAQ
Script Development
Development Guide
Plugin Development
Quick Start Development Guide Example Plugins
API Reference
Overview API Query Plugin Info Logging Finder Context Plugin Settings Internationalization
UI & Interaction
Dialog Progress Notification Chooser WebView Status Bar Dock
Files & Paths
File Operations Path Utilities Finder Actions Trash Extended Attributes Metadata File Watcher
Data Formats
JSON Plist CSV XML PDF Image
Text & Encoding
String Regex Date & Time Color Crypto
System
Shell Commands Process Application System Info AppleScript Shortcuts
System Info
Network Power/Battery Screen/Appearance Audio Bluetooth Location
Network
HTTP WebSocket URL
Input & Clipboard
Keyboard Mouse Hotkey Clipboard Window
Storage
SQLite Keychain UserDefaults
Media
OCR QR Code
Utilities
Archive UTI Share Timer Wake Lock Thread