Actions from Notification Centre

I think it was in 10.9 this handy ability was introduced for Applescript.

 display notification "A notification" with title "A title"

As were previously it was a good few lines to get that to work.

I was wondering if there is anyway to script an action when the user clicks on the notification? Haven’t managed to find any way as of yet. For example with a “Show” and “Close” button on the notification.

Hi,

in AppleScriptObjC use the NSUserNotification(Center) API with the appropriate delegate methods to handle user interaction.

By the way this works also in 10.8 :wink:

Cheers stephen, was hoping to be able to use that nice clean applescript but no matter!

I got the notifications working fine like this.

            if system version of (system info) is greater than 10.8 then
                set myNotification to current application's NSUserNotification's alloc()'s init()
                set myNotification's title to "AutoSave for Logic and Soundtrack Pro"
                set myNotification's informativeText to "Resumed"

                current application's NSUserNotificationCenter's defaultUserNotificationCenter's deliverNotification_(myNotification)
                
                else
                log "no notfication center support"
            end if

However when i tried to add buttons…

            if system version of (system info) is greater than 10.8 then
                set myNotification to current application's NSUserNotification's alloc()'s init()
                set myNotification's title to "AutoSave for Logic and Soundtrack Pro"
                set myNotification's informativeText to "Resumed"
                set myNotification's hasActionButton to yes
                set myNotification's actionButtonTitle to "OK"
                set myNotification's otherButtonTitle to "Close"

                current application's NSUserNotificationCenter's defaultUserNotificationCenter's deliverNotification_(myNotification)
                
                else
                log "no notfication center support"
            end if

no joy. Maby i have to assign a action to take place when the buttons are pressed? Gonna keep working on it and see if i can come across anything.

PS this isn’t specific code for a notification to do anything just testing.

you have to implement the delegate method

userNotificationCenter_didActivateNotification_()

it’s called after the notification badge is clicked