Notification center (bis)


(Jonas Whale) #1

To continue this topic, I have a question on task queueing.

I added the code below to several scripts.
They all have an action button that opens a Finder folder.
Of course, the folders are different for each script.

If I run 2 or more scripts one after another, clicking on their action buttons will open the same folder: the folder defined in the last ran script.

Is it possible to manage this with NSNotificationQueue or some task management method?

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

my displayNotif("Script Debugger", "Applications", "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"
		its setHasActionButton:true
		its setActionButtonTitle:"Révéler"
		its setOtherButtonTitle:"Fermer"
	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:"/Applications"
	set thePath to (thePath's stringByExpandingTildeInPath())
	set theWorkspace to current application's NSWorkspace's sharedWorkspace()
	(theWorkspace's openFile:thePath)
end userNotificationCenter:didActivateNotification:

(Shane Stanley) #2

If I understand correctly, it sounds like you need to pass the path in the notification’s userInfo property, and then extract it in userNotificationCenter:didActivateNotification:.

(Jonas Whale) #3

Thank you, Shane.
You get it at the first glance.

There is a question I already asked and for which I can’t find an answer:
Why, when this script is saved as an Applet, if I click on the action button the notification is dismissed and reappears right after, without triggering the action?