terminal-notifier

repository·master·Indexed 27 days ago

https://github.com/julienxx/terminal-notifier

A command-line tool and Ruby gem for sending macOS User Notifications (macOS 10.10+). It allows developers to trigger system alerts, play sounds, open URLs, run shell commands, and manage notification groups via the terminal or Ruby API using methods like TerminalNotifier.notify, TerminalNotifier.remove, and TerminalNotifier.list.

Tokens
2K
Snippets
9
Records
15
Agent score
83%

What's inside terminal-notifier

  1. Use terminal-notifier via CLI

    master

    To use terminal-notifier, you must call the binary located inside the application bundle. If you installed the Ruby gem, you can use the terminal-notifier wrapper command directly.

    Direct binary usage:

    ./terminal-notifier.app/Contents/MacOS/terminal-notifier -[message|group|list] [VALUE|ID|ID] [options]

    Ruby gem wrapper usage:

    terminal-notifier -[message|group|list] [VALUE|ID|ID] [options]
    ./terminal-notifier.app/Contents/MacOS/terminal-notifier -[message|group|list] [VALUE|ID|ID] [options]
  2. Activate an application when a notification is clicked

    master

    Use the -activate option with a bundle identifier (e.g., com.apple.Safari) to launch or bring an application to the foreground when the notification is clicked.

    terminal-notifier -group 'address-book-sync' -title 'Address Book Sync' -subtitle 'Finished' -message 'Imported 42 contacts.' -activate 'com.apple.AddressBook'
  3. Manage notification groups with TerminalNotifier.remove and TerminalNotifier.list

    master

    You can manage active notifications using group identifiers (such as Process.pid):

    • TerminalNotifier.remove(group_id): Removes notifications belonging to the specified group.
    • TerminalNotifier.list: Lists all active notifications.
    • TerminalNotifier.list(group_id): Lists notifications belonging to a specific group.
    TerminalNotifier.remove(Process.pid)
    
    TerminalNotifier.list(Process.pid)
    TerminalNotifier.list
  4. Send macOS notifications with TerminalNotifier.notify

    master

    Use TerminalNotifier.notify to send notifications to the macOS Notification Center (macOS 10.10+). You can pass a message string as the first argument, followed by an optional hash of configuration options.

    Supported options include:

    • :title: The title of the notification.
    • :subtitle: A subtitle for the notification.
    • :activate: An application bundle ID to activate when the notification is clicked.
    • :open: A URL or file path to open.
    • :execute: A shell command to execute.
    • :group: A group identifier (e.g., Process.pid) to group notifications.
    • :sender: The bundle ID of the sender.
    • :sound: The sound to play (e.g., 'default').
    TerminalNotifier.notify('Hello World')
    TerminalNotifier.notify('Hello World', :title => 'Ruby', :subtitle => 'Programming Language')
    TerminalNotifier.notify('Hello World', :activate => 'com.apple.Safari')
    TerminalNotifier.notify('Hello World', :open => 'http://twitter.com/julienXX')
    TerminalNotifier.notify('Hello World', :execute => 'say "OMG"')
    TerminalNotifier.notify('Hello World', :group => Process.pid)
    TerminalNotifier.notify('Hello World', :sender => 'com.apple.Safari')
    TerminalNotifier.notify('Hello World', :sound => 'default')
  5. Reference: terminal-notifier CLI options

    master

    At a minimum, you must specify either the -message, -remove, or -list option.

    Note on Sticky Notifications: To make notifications stay on screen until dismissed, change the style from Banners to Alerts in System Preferences -> Notifications -> terminal-notifier. This cannot be done per-notification.

  6. Remove notifications with TerminalNotifier.remove

    master

    Use TerminalNotifier.remove(group, verbose) to clear existing notifications.

    Parameters:

    • group: The group ID of the notifications to remove. Use 'ALL' (default) to remove all notifications.
    • verbose: (Optional) Boolean to print command output to STDOUT.

    Returns true if the command executed successfully.

  7. List notifications with TerminalNotifier.list

    master

    Retrieve details about sent notifications using TerminalNotifier.list(group, verbose).

    Parameters:

    • group: The group ID to query. Use 'ALL' (default) to list all notifications.
    • verbose: (Optional) Boolean to print command output to STDOUT.

    Returns:

    • If group is 'ALL': An Array of Hash objects containing notification details.
    • If a specific group is provided: The Hash for the first matching notification, or nil if none found.

    Hash Keys:

    • :group
    • :title
    • :subtitle
    • :message
    • :delivered_at (parsed as a Time object)