Right so, currently I have several scripts for day-to-day tasks. I have one that notifies me on YouTube uploads, and downloads their thumbnails for GeekTool to show. I have one for cleaning out my downloads folder. I’m working on some stuff that integrates with my Android phone…
Currently, using the ‘Display Notification’ command, there are two ways I can display notifications:
¢ When saved as a basic script, they come under the heading “AppleScript Editor”. I don’t like this because it groups everything under one non-descriptive heading.
¢ When saved as an Application, they come under the heading of the saved title.
> Sometimes an application will not display it’s name (I think it happens if you save it and subsequently rename it?
Haven’t quite worked out…), but instead it displays a ‘random’ string of hex digits (see linked image). I’ve yet to work out what that string represents.
My question is this: Is there a way to customise the displayed app name, so I can have multiple scripts send under the same heading? Is it possible by writing some sort of daemon to forward my notifications through, so different scripts can come under the same heading?
Has anybody worked out or drilled down into exactly how the implementation of applescript with notification center works? What is the meaning of the hex strings, and can we manipulate this in some way to register ‘spoof’ app names with Notification Center?
tl;dr: I would like to be able to have my 3 YouTube scripts all notify under the name “Youtube”, and my housekeeping scripts to notify under the name “Housekeeping”.
Thanks in advance, all!
There is no way to send two application names to Notification Center (unless they added it in Mavericks). You would need to run two applications. You could use different titles. Here’s an Xcode project that sends notifications. I’ve been thinking about converting something like this, so it can run in the Library:
script AppDelegate
property parent : class "NSObject"
property myNotification : missing value
property notificationCount: 0
on applicationWillFinishLaunching_(aNotification)
-- initialize
return
end applicationWillFinishLaunching_
-- reopen handler
on applicationShouldHandleReopen_hasVisibleWindows_(theApplication, aFlag)
my applicationDidFinishLaunching_(missing value)
return NO
end applicationShouldHandleReopen_hasVisibleWindows_
-- main
on applicationDidFinishLaunching_(aNotification)
-- set the delegate to script
current application's NSUserNotificationCenter's defaultUserNotificationCenter's setDelegate_(me)
-- get current date and target date
set sentDate to current application's NSDate's |date|()
set sentDateAsString to my formatDate_(sentDate,1,2)
set numSeconds to 30
set targetDate to current application's NSDate's dateWithTimeInterval_sinceDate_(numSeconds,sentDate)
-- make user info NSDictionary
set notificationCount to notificationCount + 1
set nsDict to current application's NSDictionary's dictionaryWithObjectsAndKeys_(notificationCount, "notificationIndex", sentDateAsString, "sentDate", "value3", "key3", missing value)
-- send notification to NSUserNotificationCenter
my sendNotification_("MyNotifier",sentDateAsString,"It's time!","Restart","Stop",targetDate,"Boing",nsDict)
say "Notification sent."
return
end applciationDidFinishLaunching_
--
-- format NSDate to string using styles
-- 0 = none, 1 = short, 2 = med, 3 = long, 4 = full
on formatDate_(aNSDate, aDateStyle, aTimeStyle)
set myFormatter to current application's NSDateFormatter's alloc()'s init()
myFormatter's setDateStyle_(aDateStyle)
myFormatter's setTimeStyle_(aTimeStyle)
set formattedDate to myFormatter's stringFromDate_(aNSDate)
return formattedDate
end formatDate_
-- method for sending a notification
on sendNotification_(aTitle, aSubtitle, aMessage, aActionButtonTitle, aOtherButtonTitle, aDeliveryDate, aSound, aDict)
-- make the notification
set myNotification to current application's NSUserNotification's alloc()'s init()
set myNotification's title to aTitle
set myNotification's subtitle to aSubtitle
set myNotification's informativeText to aMessage
set myNotification's actionButtonTitle to aActionButtonTitle
set myNotification's otherButtonTitle to aOtherButtonTitle
set myNotification's deliveryDate to aDeliveryDate
set myNotification's soundName to aSound
set myNotification's userInfo to aDict
-- schedule the notification
current application's NSUserNotificationCenter's defaultUserNotificationCenter's scheduleNotification_(myNotification)
return
end sendNotification_
-- delegate instance methods
-- force presentation for when application process is frontmost
on userNotificationCenter_shouldPresentNotification_(aCenter, aNotification)
return yes
end userNotificationCenter_shouldPresentNotification_
-- deliver
on userNotificationCenter_didDeliverNotification_(aCenter, aNotification)
say "Notification Delivered"
return
end userNotificationCenter_didDeliverNotification_
-- user activation
on userNotificationCenter_didActivateNotification_(aCenter, aNotification)
say "Notification Activated"
-- 0 none
-- 1 contents clicked
-- 2 action button clicked
set userActivationType to (aNotification's activationType) as integer
if userActivationType is 1 then
say "contents clicked"
my contentsClicked_(aNotification)
else if userActivationType is 2 then
say "action button clicked"
my actionButtonClicked_(aNotification)
else -- userActivationType is 0
say "no user activation"
end if
return userActivationType
end userNotificationCenter_didActivateNotification_
--
on contentsClicked_(aNotification)
-- do something
return
end contentsClicked_
-- gets user info and deletes delivered notification from Notification Center
on actionButtonClicked_(aNotification)
-- delete the notification and send a new one (or some other action)
-- first get info on notification
set theInfo to aNotification's userInfo
set theValue to theInfo's valueForKey_("notificationIndex")
say (theValue as string)
-- delete notification from notification center
current application's NSUserNotificationCenter's defaultUserNotificationCenter's removeDeliveredNotification_(aNotification)
-- send a new notification
my applicationDidFinishLaunching_(missing value)
return
end actionButtonClicked_
--
on applicationShouldTerminateAfterLastWindowClosed_()
return true
end applicationShouldTerminateAfterLastWindowClosed_
-- quit
on applicationShouldTerminate_(sender)
-- Insert code here to do any housekeeping before your application quits
return current application's NSTerminateNow
end applicationShouldTerminate_
end script
I was actually thinking of the point of using the notification centre for this yesterday:
-My conclusion was that using the Notification Centre wasn’t feasible, at least not for this, because at least the display notification command are implemented as a command in AppleScript version 2.3, and because Applescript Script is single threaded, so there are no particular gains in using a delegate anyway.
Maybe I’m ignorant in some sort of way here, but I just use display notification, or tell me to display notification, with the title I opt for with the subtitle predicate.
You could write your own scriptable application to do it. Seems like a lot of work for something so small…
It presumably works pretty much the same as any other app works with it. Which means it has very little control.
They represent autosaved versions of your script app. If you have it open, it will autosave when you make another app active.
No.
You don’t seriously think that in security-obsessed times Apple would throw together something that would let viruses, for example, post notifications pretending to be TextEdit or Safari?