High frequency polling through applet, and programmatically interaction with Notification Center

Hi!

I have (also) been pondering lately on how to programmatically interact with the Notification Center. I have seen posts on this forum expressing interest in similar themes, and thought I'd share my findings, so far:


1) Apple Script can detect the number of dialog windows displayed by Notification Center

tell application "System Events"
	tell process "NotificationCenter"
		return count of windows
	end tell
end tell

In and of itself, this can, for instance, be used to make notifications appear sequentially (not asynchronously on top of each other).

TEST Synchronous-Sequential Notification display.kmmacros (2.9 KB)
(KM v11.0.3)

Macro Image



2) It is possible to make a simple lightweight triggering applet, running in the background, polling with high frequency (every second)

Below is a very simple and lightweight script that detects if there are 1 or more dialog windows displayed by Notification Center, and if so triggers a specified KM macro by a Script Trigger through its given UUID:

repeat
	tell application "System Events"
		tell process "Notification Center"
			if (count of windows) > 0 then
				tell application "Keyboard Maestro Engine"
					#You'll have to swap out the UUID below with the one defined in KM in your macros "Or by Apple Script" text box
					do script "932B52AD-82EB-4659-A2D2-624586F2A13D"
				end tell
				repeat while (count of windows) > 0
					delay 0.2
				end repeat
			end if
		end tell
	end tell
	delay 1
end repeat

It does poll every second, something that is maybe ill-advised, but exported and run as an app it consumes very little processing power and memory, (on my system much less than the KM engine does with a Periodic Trigger set to 1 second, it seems from my very initial quick tests here). So, so far, I'd say this applet approach is a viable option for high-frequency polling needs. You will have to change the UUID in the script to the one defined in KM in the macros "Or by Apple Script" on your system:


You can make a script into an app by Exporting it as "Application" from ScriptEditor:


As a whole this applet kind of works like a "pseudo-trigger" of sorts, and to make it into a background app (without appearance in the dock), you'll have to right-click the saved app, select Show Package Contents, then open Info.plist in a TextEditor and add the following inside the top-level <dict> block:

<key>LSUIElement</key>
<true/>

Now the tricky part with this Applet approach is getting Privacy & Security to grant it assistive access. Or at my system it took numerous attempts by adding/removing the applet to the list under System Settings > Privacy & Security > Accessibility, with computer restarts in between. I also tried sneaking it in by upping the 1-second delay in the script to 10, (something that seems to make it more probable that the system will grant it access), and changing it back to 1 after it's been accepted, but I am not sure if this is what finally made it be accepted as I tried so many things — but once it's in, it's in, or so it seems, and has not yet caused me more troubles! Maybe it would have been simpler to grant it access if I had stored the app in the Applications folder (I keep it in a KM folder of mine), I don't know, but it might be worth a test, if anyone wants to try this here approach.

The applet will of course have to be launched after every system restart, to obtain this the app can be added to the list of Login Items under System Preferences (or have a KM macro launching it from an At engine launch trigger, or something).



3) First Example of how to put this Applet pseudo-trigger in use

For now I've been testing this pseudo-trigger with the following macro that grabs static text from the topmost Notification — something I at least have not found a way to obtain directly through Apple Script. (And this part has an almost strange convergence with @Airy's recently shared macro).

This macro below finds the boundaries of the (topmost) notification, OCRs its content, and logs it with timecode as a single line in a global variable:

[Applet triggered- PollForNotification] TEST -- Log notification.kmmacros** (39 KB)
(KM v11.0.3)

Macro Image

I guess anyone trying this will have to swap out the images in the four Find Image on Screen actions, with screen grabs taken on their system. They need to be taken kind of precisely, and contain part of the bounding border on each of its sides. On my dark theme, there is a thin lighter gray border that needs to be part of the screen grab:



This became a really long and wordy post, but inspired by @Airy's sharing, I thought I'd just share these recent findings and ponderings of mine. And do not hesitate to suggest improvements or other approaches within the topic of programmatically interacting with the Notification Center. I am all ears!

Thanks, I hope to understand your code.

1 Like