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 pathkeys(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