Search code examples
javascriptmacosnotificationsautomationjavascript-automation

Send Mac OS X Notification Center message via JavaScript - 10.10 Yosemite


How can a message be sent to the Mac OS X Notification Center using the JavaScript for Automation functionality that was introduced in 10.10 Yosemite?


Solution

  • Messages can be sent to Notification Center via Automator and Script Editor JavaScripts by using the includeStandardAdditions method from the core library. For example:

    app = Application.currentApplication()
    app.includeStandardAdditions = true
    app.displayNotification('Basic message')
    

    The Script Editor application has documentation that shows they rest of the options. It can be accessed from the "Window -> Library" Menu and choosing the "StandardAdditions" Library and searching for the "displayNotification" command.

    This example uses the full set of options:

    app = Application.currentApplication()
    app.includeStandardAdditions = true
    
    app.displayNotification('Advanced message', { 
      withTitle: 'Message Title',
      subtitle: 'Subtitle', 
      soundName: 'Sosumi'
    })