Notification center

Today, I have questions about the notification center.

How to extend the time the notification is displayed?
Is it possible to create buttons like those in this screenshot:

use framework "Foundation"

my displayNotif("Script Debugger", "Les préférences ont été sauvegardées", "Cliquez ici pour ouvrir le dossier de sauvegarde.")

on displayNotif(theTitle, theSub, theInfo)
	set theNotif to current application's NSUserNotification's alloc()'s init()
	tell theNotif
		its setTitle:theTitle
		its setSubtitle:theSub
		its setInformativeText:theInfo
		its setSoundName:"Purr"
		--these 3 lines don't work----------------
		its setHasActionButton:true
		its setActionButtonTitle:"Original"
		its setOtherButtonTitle:"Sauvegarde"
	end tell
	tell current application's NSUserNotificationCenter's defaultUserNotificationCenter()
		its setDelegate:me
		its deliverNotification:theNotif
	end tell
end displayNotif

on userNotificationCenter:theCenter shouldPresentNotification:theNotif
	return yes
end userNotificationCenter:shouldPresentNotification:

on userNotificationCenter:theCenter didActivateNotification:theNotif
	set thePath to current application's NSString's stringWithString:"~/Library/Application Support/Script Debugger 6"
	set thePath to (thePath's stringByExpandingTildeInPath())
	set theWorkspace to current application's NSWorkspace's sharedWorkspace()
	(theWorkspace's openFile:thePath)
end userNotificationCenter:didActivateNotification:

Your code works fine if you change the notification style from Banners to Alerts. That has to be done by the user for the particular app in System Preferences.

The «Alerts» are already activated in System Preferences.
[I don’t understand why SD is showing 2 times?!]

This snippet goes after a script that makes a backup of the SD prefs.
It’s supposed to be launched from SD Scripts Menu (with a shortcut).
If I run the script when it’s open, everything goes well (the buttons an the response).
But with the script closed, no Alert is displayed.

If I save it as an applet and run it from the Finder, the alert is displayed without any button.
But if I click on it, it responses as it should.

Is it an issue with SD?

Those menu scripts are not actually run by Script Debugger these days, which explains what you see. I’m just not sure how to change that behavior in such a case.

You will have to add the applet to the list in System Preferences (and lock it or code-sign it to stop having to do so each time it runs).

After several tries, I’m stuck with this:

The script (as it’s part of the files I want to backup, and locked) was preventing the completion of copying.
So I decided to move it in FastScripts menu and run it from there.

Now the script shows twice in System Preferences and I can’t find any way to remove the old one.
I had deleted all copies and aliases of the applet and restarted the Mac dozens of times, but no matters.

The fact that it is listed 2 times seems to prevent the normal behavior of the notification:
The buttons are displayed but don’t work and the script restarts continuously.

What I’m doing wrong?
And how to get rid of those duplicates in System Prefs?

I found out how to reset the list of Applications in System Preferences> Notifications:
There’s a folder in /private/var/folders/ named com.apple.notificationcenter that contains a folder named db.
By simply deleting this folder, the list is reset to its default.

The fact that it is listed 2 times seems to prevent the normal behavior of the notification

Not at all. Even with no duplicates, the applet locked and launched directly from Finder, it relaunches itself every time the action button is clicked.

It’s a magical mystery!!
More seriously, there’s something wrong in my code.
Or AppleScriptObjC does not like Notifications!

1 Like

After a couple of days of stand-by and several restarts, the behavior of my script has partially changed.

Launched from the script menu in SD, it’s the same as before:
No matters if the script is saved as an app, it will work only if the script is open.
Even if it’s a signed or locked app.

But when launched from FastScript menu, as a compiled script, it’s working perfectly.

Can it be an issue with the delegate?

I don’t think so. I suspect that the way SD is running Script menu scripts, the fact that it’s saved as an app is irrelevant. I just can’t see how to change the settings for the system script runner.

Fortunately, there’s FastScripts.