`display notification` works from AppleScript Editor but not when exported as Application

applescriptnotifications

I've got some short AppleScript code designed to display notifications at a specified interval. The code works perfectly from the Script Editor, but not when exported as an Application. Instead, notifications never show up in the top left though they do appear in Notification Center. However, instead of happening every interval (say once/minute) they just trigger continuously.

Here's the code:

set lasttime to current date
set min to 5 as number
set interval to 1 as number

repeat
    display notification "Notification" with title "Title"
    delay (interval * 60)
    if (((current date) - lasttime) / minutes) ≥ min then exit repeat
end repeat

Any ideas what I'm doing wrong here?

Best Answer

When you check the notification center you will see that the messages still appear there but the center won't notify the user when the app sending the message is in the foreground.

Workaround:

Before firing the notification, activate Finder:

tell application "Finder" to activate

set lasttime to current date
set min to 5 as number
set interval to 1 as number

repeat
    display notification "Notification" with title "Title"
    delay (interval * 60)
    if (((current date) - lasttime) / minutes) ≥ min then exit repeat
end repeat

Additionally, we can do this: Run an apple script saved as run only so icon doesnt show in dock - but doing so, one must use Activity Monitor to terminate the application. Still, we need to "de-frontmost" our application because when it launches it gets to be the active application.