Saturday, February 27, 2021

#1 2012-07-30 05:51:32 am

ferite
Member
Registered: 2006-06-06
Posts: 5

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 :-|
Can I use Cocoa functions " NSUserNotification" and "NSUserNotificationCenter" http://developer.apple.com/library/prer … tification directly from AppleScript?

Thank You

+Ferite

Offline

 

#2 2012-08-21 03:25:19 pm

jonathanwiesel
Member
Registered: 2012-01-12
Posts: 1

Re: MAc OS X 10.8 Notification Center and Applescript

I have the same question!

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

Offline

 

#3 2012-08-21 05:29:25 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6574

Re: MAc OS X 10.8 Notification Center and Applescript

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

Applescript:

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)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#4 2012-08-22 03:34:23 pm

oschrenk
Member
Registered: 2012-08-22
Posts: 1

Re: MAc OS X 10.8 Notification Center and Applescript

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)

Offline

 

#5 2012-08-22 05:06:58 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6574

Re: MAc OS X 10.8 Notification Center and Applescript

Is there a way to display a notification window?


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).


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#6 2013-06-11 11:13:29 am

slashdot
Member
Registered: 2003-11-13
Posts: 291

Re: MAc OS X 10.8 Notification Center and Applescript

Did anyone find out how to do it with applescript?

using the snippet code i get

AppleScript Error
NSUserNotification doesn’t understand the alloc message.

Last edited by slashdot (2013-06-11 11:14:00 am)

Offline

 

#7 2013-06-11 05:57:48 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6574

Re: MAc OS X 10.8 Notification Center and Applescript

slashdot wrote:

Did anyone find out how to do it with applescript?

using the snippet code i get

AppleScript Error
NSUserNotification doesn’t understand the alloc message.


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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#8 2013-06-11 06:52:20 pm

kel1
Member
Registered: 2013-01-11
Posts: 2188

Re: MAc OS X 10.8 Notification Center and Applescript

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


Os 10.10.3
Mbp

Offline

 

#9 2013-06-11 06:58:27 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6574

Re: MAc OS X 10.8 Notification Center and Applescript

kel1 wrote:

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


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

macosxautomation.com/lion/applescript.html


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#10 2013-06-11 09:15:48 pm

kel1
Member
Registered: 2013-01-11
Posts: 2188

Re: MAc OS X 10.8 Notification Center and Applescript

Why didn't anybody tell me about this?! big_smile Another toy.


Os 10.10.3
Mbp

Offline

 

#11 2013-06-11 09:21:22 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6574

Re: MAc OS X 10.8 Notification Center and Applescript

kel1 wrote:

Why didn't anybody tell me about this?!


You just made a grown man weep...


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#12 2013-06-12 12:15:40 am

slashdot
Member
Registered: 2003-11-13
Posts: 291

Re: MAc OS X 10.8 Notification Center and Applescript

Ok.. so what am I missing here.

1.  open applescript editor
2.  file > new from template > Cocoa-AppleScript Applet
3.  paste

Applescript:

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

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

Offline

 

#13 2013-06-12 12:32:11 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6574

Re: MAc OS X 10.8 Notification Center and Applescript

From the docs:

The user notification center reserves the right to decide if a delivered user notification is presented to the user. For example, it may suppress the notification if the application is already frontmost (the delegate can override this action)


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:

Applescript:

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_


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#14 2013-06-12 12:35:13 am

slashdot
Member
Registered: 2003-11-13
Posts: 291

Re: MAc OS X 10.8 Notification Center and Applescript

that did it. thanks.

Offline

 

#15 2013-11-28 12:34:59 am

djmanas
Member
Registered: 2012-08-30
Posts: 9

Re: MAc OS X 10.8 Notification Center and Applescript

Shane Stanley wrote:

From the docs:

The user notification center reserves the right to decide if a delivered user notification is presented to the user. For example, it may suppress the notification if the application is already frontmost (the delegate can override this action)


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:

Applescript:

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_


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

Applescript:


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:

Applescript:


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,

Offline

 

#16 2013-11-28 01:10:42 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6574

Re: MAc OS X 10.8 Notification Center and Applescript

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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#17 2013-11-28 11:42:13 am

djmanas
Member
Registered: 2012-08-30
Posts: 9

Re: MAc OS X 10.8 Notification Center and Applescript

Bug report raised

Damn sad since I have begun with ASObjC I started to find bug by bug sad I think I should stick with other languages big_smile

Offline

 

#18 2013-11-28 02:45:57 pm

partron22
Member
Registered: 2011-03-18
Posts: 86

Re: MAc OS X 10.8 Notification Center and Applescript

djmanas wrote:

The OSX Mavericks's AppleScript editor throws system exception on the problem with delegate method...


If you have 10.9, this'll work:

Applescript:

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

http://macosxautomation.com/mavericks/n … ns/01.html

Online

 

#19 2013-11-29 02:11:18 am

djmanas
Member
Registered: 2012-08-30
Posts: 9

Re: MAc OS X 10.8 Notification Center and Applescript

thanks, this works like a charm smile

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)