How to differentiate "display notification"'s Show / Close buttons ???

Hi, I’m noo here.

I searched and found that Notification’s buttons (Show + Close) can be “clicked” programmatically by (simplified):

tell application "System Events"
tell process "NotificationCenter"
	set theWindows to every window whose subrole is "AXNotificationCenterAlert"
	repeat with i from 1 to number of items in theWindows
	        set this_item to item i of theWindows
	        click button "Close" of this_item
	end repeat
[...]

Obviously Notification Center does recognize their names.
How then can I find out vice-versa, which button has been clicked / pressed by a user?
(I’m aware that officially “display notification” doesn’t return a result.)

You may try this code :


my notify("The contact card is being located in the addressBook.", "Please wait...")

#=====

on notify(theNotification, theTitle)
	log return & "Entering the handler : notify"
	
	
	display notification theNotification with title theTitle # uniquement pour Yosemite
	
	tell application "System Events" to tell process "NotificationCenter" # the name of the process is a SINGLE name, not space embedded
		set frontmost to true
		set i to 0
		repeat 10 times
			delay 0.05
			if exists (window 1 whose subrole is "AXNotificationCenterAlert") then exit repeat
			set i to i + 1
		end repeat
		log i
		if i < 10 then
			tell (window 1 whose subrole is "AXNotificationCenterAlert")
				set buttonNames to name of buttons
				log result --> (*Fermer, Afficher*) CAUTION, they are localized strings
				title of buttons
				log result --> (*Fermer, Afficher*) CAUTION, they are localized strings
				value of attribute "AXIdentifier" of buttons
				log result --> {"close_button", "action_button"} --> Bingo, they are not localized
				click (first button whose value of attribute "AXIdentifier" is "close_button")
				-- click (first button whose value of attribute "AXIdentifier" is "action_button")
			end tell
		else
			if exists (window 1 whose subrole is "AXNotificationCenterBanner") then
				tell (window 1 whose subrole is "AXNotificationCenterBanner")
					log class of UI elements --> (*static text, image, scroll area, image, image*)
					# Force it to close
					click at first image -- typing image require less strokes than typing static text ;-)
				end tell
			end if
		end if
	end tell
	
end notify

#=====

The name of the process is “NotificationCenter”, not “Notification Center

Often I set the notifications created by Script Editor to banners which don’t have button.
This is why I inserted a test. If the loop is executed 10 times, the notification is not an alert so execute relevant code.

Yvan KOENIG running El Capitan 10.11.2 in French (VALLAURIS, France) samedi 12 décembre 2015 16:39:29

Merci, Yvan for your very detailed answer!

If I run your code it outputs
Result:
button “Close” of window 1 of application process “NotificationCenter” of application “System Events”

I fully understand that buttonNames, title of buttons and value of attribute “AXIdentifier” of buttons all return the names etc. of ALL buttonsBEFORE any button is pressed.
However if I add a simple command like set varTen to 10 (behind “click(…”) it will display
“Result: 10” in Script-Editor’s message window, NOT any logged result.
(Obviously it displays the last mentioned variable.)

What I’d like to get is the name/specifier/whatever of only one button AFTER it was pressed…
… some kind of “set chosenButton to button clicked”.

I’m afraid plain AS won’t give me that …

As far as I know, AppleScript doesn’t receive the information about the bouton depressed by the user.
What is available in AppleScript is the ability to send an alert but I’m not sure that it is warned of the creation of an alert by an application.
If you insert the instruction log result just after the click instruction you will get
(button Fermer of window 1 of application process NotificationCenter)
but if you disable the click instruction and let the user click a button , the log result will issue the error :
error “La variable result n’est pas définie.” number -2753 from result

Yvan KOENIG running El Capitan 10.11.2 in French (VALLAURIS, France) lundi 14 décembre 2015 17:45:13

Correct, nothing will give you that. Notifications are fire-and-forget tasks in the system. Once a notification is accepted by the user the system’s notification center will send an message back to your application. If the application uses it’s own delegate it can respond to user action with the notification it has send. However the notification center is an singelton and so is it’s delegate. Since user action osax commands are executed by remote application, overriding it’s the notification centers’ delegate would be a bad thing.

The only (safe) solution is creating an AppleScriptObjC application (read: not an AppleScriptObjC library) and use the singleton for your own application instance. You can send out an notification and respond to the user action by your notification center delegate methods.

Okay. As far as I now understand the “mechanisms” of a “notification”, things are like this:

1.) I can set whether a notification will contain buttons in System-preference’s “Notifications” pane for any receiving app.

2.) Which “button-choices” will be displayed (e.g.: Close/Show or Answer/Delete) depends on the sending application.
(In the case of my test script “Show” will bring Script-Editor to the front.)

So IMHO allowing buttons for an app only makes sense, when you expect to get relevant “decisions” sent.

I admit that I mainly tried to get any “special use” out of notifications because I like their styling and behavior more than dialogs and windows.
But now I learned that i cannot use them to catch a user’s reactions. Pity …

Thanks for all your thoughts on this!

If you aren’t satisfied with standard alerts/dialogs, you may have a look to Shane STANLEY’s dialog toolkit which give the ability to build fancy interfaces.
It’s available for free at :
http://www.macosxautomation.com/applescript/apps/

Yvan KOENIG running El Capitan 10.11.2 in French (VALLAURIS, France) mardi 15 décembre 2015 22:42:18