15

I am using

osascript -e 'display notification "Lorem ipsum dolor sit amet" with title "Title"'

to display notifications in Mac. However, on clicking the notification, I am getting redirected to the applescript editor. Is it possible for me to redirect the user to a url or open up a directory on clicking the notification which is generated?

Pradeep Vairamani
  • 4,004
  • 3
  • 36
  • 59

2 Answers2

5

The run handler will only get called if the script is saved as an app, preferably a stay-open app. In any case, the app has to be still running when someone clicks the notification. You won't get this behavior from a simple osascript string.

You could get osascript to run a compiled script file (which can store properties persistently), but you will still need to distinguish between the run event that happens when you run the script, and the run event that gets called when someone clicks the notification.

I can suggest a couple of solutions here.

  • Use a python library to fire off notifications and forget about appleScript/OSA. You can find some information, and various solutions at this stackoverflow link: Python post osx notification

  • Set up a stay-open appleScript app as a kind of 'notification server' and send a message to that (possibly with OSAscript, unless you can send a raw apple event to the 'server' from python) when you want to set up some notification intercourse. This is tricky, and seems overcomplex, compared to my first suggestion. In particular, you may still need to mess about with the privacy settings (especially if on Mavericks or later) to allow OSAscript access to system events.

Here are a couple of links which may guide you with the latter approach, but I really thing the first suggestion will get you further, with fewer tears:

http://jacobsalmela.com/bash-script-enable-access-assistive-devices-programmatically-os-x-mavericks-10-9-x-simulate-keystrokes/

http://support.apple.com/kb/HT6026?viewlocale=en_US&locale=en_US

Community
  • 1
  • 1
brennanyoung
  • 6,243
  • 3
  • 26
  • 44
0

so yes there is a way to do what you would like

here is a tutorial here

this is a simplified version that does what you like, however you must save it as an application and drag a file on it.

on open theItems
    display notification "Open stackoverflow ?" with title "Stackoverflow"
    delay 2
end open

on run

    tell application "Safari"
        tell window 1
            set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})
        end tell
    end tell
end run
mcgrailm
  • 17,469
  • 22
  • 83
  • 129
  • I wouldn't use this simplified version. Having to drag a file onto a droplet in order to activate the notification will surely not accomplish what he wants. I can't think of any situation that would be of use. You simply want a run handler that puts up a notification unless it detects it was called by Notification selection, and if so then launch a directory or URL. – jweaks Jul 07 '14 at 15:28
  • I'm not sure how the initial notifications is to be prompted. However, this answer does show how to use the click event of a display notification – mcgrailm Jul 07 '14 at 15:37
  • Well, except that it doesn't "show how to use the click event of a display not", unless you initiate the notification by dragging a file onto a droplet. The OP want's to programmatically initiate, as he indicated with the osascript call. But, he can modify the one from macosxautomation to do so, but everything will need to be in the run handler. – jweaks Jul 07 '14 at 20:40
  • My requirement is pretty simple. I need to trigger a notification through a python program. On clicking the notification, I need to open an url which also needs to be programatically set from python. – Pradeep Vairamani Jul 14 '14 at 07:15
  • according to http://macosxautomation.com/mavericks/notifications/01A.html the run handler is called when the user clicks on the notification. However, this doesn't seem to work for me. – Pradeep Vairamani Jul 14 '14 at 08:36