MAc OS X 10.8 Notification Center and Applescript

Hello,
are there any posibilities to show customizable message in Mac OS X 10.8 Notification Center via AppleScript?
I found only RUBY-based command line application (https://github.com/alloy/terminal-notifier), which have to be installed as interface between Notification Center and AppleScript “ but I don’t want install any aditional app :expressionless:
Can I use Cocoa functions " NSUserNotification" and “NSUserNotificationCenter” http://developer.apple.com/library/prerelease/mac/releasenotes/Cocoa/Foundation.html#10_8UserNotification directly from AppleScript?

Thank You

+Ferite

I have the same question!

Don’t want to rely on other extension to make notifications work.

If you won’t use a helper app, you will have to use AppleScriptObjC:

set theNotif to current application's NSUserNotification's alloc()'s init()
tell theNotif
	setTitle_("A title")
	setInformativeText_("This is some information")
end tell
tell current application's NSUserNotificationCenter's defaultUserNotificationCenter() to deliverNotification_(theNotif)

Could you go int more details on how to use the script you posted? If I run it as a normal script I get error "žNSUserNotification" versteht die Nachricht žalloc" nicht." number -1708 from NSUserNotification.

When I create a new script with the “Cocoa-Apple Script-Applet” template, paste the code and execute it, a message is displayed once (multiple executions don’t create multiple messages) but its only created in the background in the notification center without a notification window.

Is there a way to display a notification window?

AppleScript: 2.2.2
Browser: Safari 537.4
Operating System: Mac OS X (10.8)

The user controls that via the Notifications panel of System Preferences. Even then, I think the dialogs don’t appear if the application sending the notification is front-most. And you can add an entry to the app’s Info.plist file for NSUserNotificationAlertStyle to set the default style (although the user always has last say).

Did anyone find out how to do it with applescript?

using the snippet code i get

You need to save it as a Cocoa-AppleScript app. It can’t be done in raw AppleScript at this stage.

Hi slashdot,

I just discovered today, that you can run a Cocoa-AppleScript app in the AppleScript Editor as people were saying all the time. Go to:

File > New from Template > Cocoa-AppleScript Applet

In that window you can compile Cocoa stuf with most AppleScript.

gl,
kel

Hey, welcome to Mac OS X 10.7 :wink: Here’s a bit more info:

macosxautomation.com/lion/applescript.html

Why didn’t anybody tell me about this?! :smiley: Another toy.

You just made a grown man weep…

Ok… so what am I missing here.

  1. open applescript editor
  2. file > new from template > Cocoa-AppleScript Applet
  3. paste
set theNotif to current application's NSUserNotification's alloc()'s init()
tell theNotif
   setTitle_("A title")
   setInformativeText_("This is some information")
end tell
tell current application's NSUserNotificationCenter's defaultUserNotificationCenter() to deliverNotification_(theNotif)

4.Script > Run Application or file > save > fileformat application

  1. both result in the same thing no notification in the notification center and the app just doesn’t execute in 10.8

From the docs:

Your app is frontmost when you run it. You can make your script the user notification centre’s delegate and override this action like this:

set theNotif to current application's NSUserNotification's alloc()'s init()
tell theNotif
	setTitle_("A title")
	setInformativeText_("This is some information")
end tell
tell current application's NSUserNotificationCenter's defaultUserNotificationCenter()
	setDelegate_(me)
	deliverNotification_(theNotif)
end tell

on userNotificationCenter_shouldPresentNotification_(cen, notif) -- delegate method
	return yes
end userNotificationCenter_shouldPresentNotification_

that did it. thanks.

This works fine, when I create new ASObjC script and simply paste, but when I have something like:


on initObject()
  script theObject
    on notify()
      set theNotif to current application's NSUserNotification's alloc()'s init()
      tell theNotif
	setTitle_("A title")
	setInformativeText_("This is some information")
      end tell
      tell current application's NSUserNotificationCenter's defaultUserNotificationCenter()
	setDelegate_(me)
	deliverNotification_(theNotif)
      end tell
    end notify

    on userNotificationCenter_shouldPresentNotification_(cen, notif) -- delegate method
	return yes
    end userNotificationCenter_shouldPresentNotification_
  end script
  
  return theObject
end initObject

When I init the object like:


use theObject : script "theObjectScript"
set instanceOfObject to theObject's initObject()
instanceOfObject's notify()

The OSX Mavericks’s AppleScript editor throws system exception on the problem with delegate method, am I doing something wrong???

Thanks,
Regards,

It looks like a bug to me, probably in AS or ASObjC rather than ASE. Log a bug report.

Bug report raised

Damn :frowning: since I have begun with ASObjC I started to find bug by bug :frowning: I think I should stick with other languages :smiley:

If you have 10.9, this’ll work:

display notification "Draw Something NOW!" with title "MacPaint " subtitle "v 1.3 says:" sound name "Sosumi"

http://macosxautomation.com/mavericks/notifications/01.html

thanks, this works like a charm :slight_smile: