osxmetadata

repository·master·Indexed 41 days ago

https://github.com/rhettbull/osxmetadata

A Python library and CLI tool for reading and writing macOS file metadata, including Finder tags, comments, authors, and Spotlight (kMDItem) attributes. Version 1.4.1 provides a unified interface to manipulate metadata that is often difficult to access via standard Apple APIs, supporting macOS 10.15 or later and Python 3.10+.

Tokens
17.7K
Snippets
56
Records
69
Agent score
50%

What's inside osxmetadata

  1. Overview of osxmetadata

    master

    osxmetadata is a Python library that provides a unified interface to access and manipulate various macOS file metadata attributes. While Apple's native APIs (like MDItem or NSURL) make it easy to read metadata, they often lack public interfaces to write/set it (e.g., for authors or Finder tags). osxmetadata bridges this gap using a combination of documented and undocumented APIs to allow both viewing and editing of attributes like:

    • Tags/Keywords
    • Finder comments
    • Authors
    • Other MDItem Spotlight metadata

    Requirements:

    • Operating System: macOS 10.15 or later (tested on macOS 13.5.1 Ventura).
    • Python Version: 3.10+.
  2. What is osxmetadata?

    master
    osxmetadata is a Python library that provides a simple interface for accessing various metadata attributes of macOS / OS X files. It allows developers to interact with file metadata that is typically managed by the Finder or the operating system.
  3. Handle Date/Time metadata attributes

    master

    Attributes like kMDItemDueDate or kMDItemDownloadedDate return datetime.datetime objects. These objects are timezone-naive and represent the local timezone. You can pass timezone-aware datetime objects when setting these attributes, and they will be converted appropriately.

    from osxmetadata import *
    import datetime
    
    md = OSXMetaData("test_file.txt")
    # Setting a naive datetime
    md.kMDItemDueDate = datetime.datetime(2022, 10, 1)
    
    # Setting a timezone-aware datetime
    md.kMDItemDownloadedDate = datetime.datetime(2022, 10, 1, tzinfo=datetime.timezone.utc)
  4. Manage Finder Tags and colors

    master

    Finder tags (the tags attribute) can include both a name and an optional color. To specify a color, append a comma followed by the color name to the tag name.

    Format: TagName,colorname Valid colors: gray, green, purple, blue, yellow, red, orange.

    If no color is specified, the tag will automatically inherit whatever color is already assigned to that tag name in Finder.

    # Example: Set a red tag named 'Project'
    osxmetadata --set tags Project,red FILE
  5. Set and manage metadata attributes

    master

    When using --set or --append, you can identify attributes using their short name, constant name, or long constant name.

    Examples of attribute naming:

    • --set findercomment "Hello world"
    • --set kMDItemFinderComment "Hello world"
    • --set com.apple.metadata:kMDItemFinderComment "Hello world"

    Behavior for different attribute types:

    • String attributes: --set overwrites the existing value. --append adds to the existing value.
    • Array/Multi-value attributes: You can specify --set multiple times to build an array (e.g., --set keywords foo --set keywords bar results in ['foo', 'bar']). This does not overwrite existing values; it adds to them. Use --append to add a value only if it is not already present.
  6. Use shortcut names for MDItem attributes

    master

    For every kMDItem attribute, osxmetadata defines a lowercase "shortcut name" that can be used as a class attribute. For example, kMDItemAuthors can be accessed via .authors and kMDItemDueDate via .duedate.

    from osxmetadata import *
    md = OSXMetaData("test_file.txt")
    
    # Shortcut for kMDItemAuthors
    md.authors = ["Jane Smith", "John Doe"]
    
    # Shortcut for kMDItemDueDate
    md.duedate = datetime.datetime(2022, 10, 1)
  7. Supported metadata attributes in osxmetadata

    master

    The library currently supports accessing and manipulating several types of macOS file metadata, including:

    • Tags/Keywords: Finder tags used for file organization.
    • Finder Comments: Textual comments attached to files in Finder.
    • Authors: Metadata identifying the creators of a file.
    • Other attributes: Various other macOS-specific file metadata.
  8. Understand how osxmetadata loads attribute metadata

    master
    The osxmetadata module uses JSON files located in the attribute_data/ directory to define the metadata for various attributes that can be set on macOS files. This metadata is automatically loaded into the module when it is imported. The underlying data is sourced from the macos_mditem_metadata project.
  9. Setup the development environment for osxmetadata

    master

    To set up a local development environment, use uv to manage the virtual environment and dependencies. Follow these steps:

    1. Install uv using the official installer.
    2. Create a virtual environment using uv venv (you can specify a version with --python 3.13).
    3. Activate the environment.
    4. Install dependencies, including development extras, using uv pip install or uv sync.
    # Install uv
    curl -LsSf https://astral.sh/uv/install.sh | sh
    
    # Create and activate virtual environment
    uv venv
    source .venv/bin/activate
    
    # Install dependencies with dev extras
    uv sync --extra dev
  10. Install osxmetadata using pipx

    master

    For a clean installation that manages its own virtual environment, use pipx. This is recommended if you want to use the osxmetadata command-line tool directly from your terminal.

    1. Install Homebrew (if not already installed) from https://brew.sh/.
    2. Install pipx via Homebrew: brew install pipx.
    3. Install osxmetadata: pipx install osxmetadata.
    4. To upgrade: pipx upgrade osxmetadata.
    brew install pipx
    pipx install osxmetadata
    # To upgrade
    pipx upgrade osxmetadata