Pradeep Vairamani
Pradeep Vairamani

Reputation: 4322

Redirected to applescript editor on clicking apple notification

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?

Upvotes: 15

Views: 5234

Answers (2)

brennanyoung
brennanyoung

Reputation: 6524

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

Upvotes: 5

mcgrailm
mcgrailm

Reputation: 17638

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

Upvotes: 0

Related Questions