Notifications during "do shell script" not showing

In an AppleScript app, I'm downloading a file using curl in a "do shell script" command. The curl starts and AppleScript immediately moves into a loop displaying a Notification until the curl has finished.


The notification displays properly during the curl, when running from ScriptEditor. It stays visible on the Desktop until shortly after the curl has finished. But when running the app (i.e. opened in Finder), the Notification doesn't display at all until well after the curl has finished.


Here is a summary of the code:


set process_id to do shell script "curl -L https://www.downloads.com/latest/downloadthisfile-o /usr/local/bin/downloadthisfile &> /dev/null & echo $!" with administrator privileges

set process_name to "downloadthisfile"

send_notification(process_id, process_name)


-- Handler for sending notification to user while files are downloading

on send_notification(process_id, process_name)

repeat

do shell script "ps ax | grep " & process_id & " | grep -v grep | awk '{ print $1 }'"

if result is "" then exit repeat

display notification process_name & " is downloading. Please Wait." with title "Downloading " & downloadthisfile

delay 3

end repeat

end send_notification


Why would this work well when running from ScriptEditor but not when running the app itself ?



Garry

I don't have a background task to test with, but one possibility is that notifications normally don't occur if the application is frontmost (that can be overridden with the userNotificationCenter:shouldPresentNotification: delegate method). Another would be that even though you are running the shell script in the background, you are still keeping everything else in a tight repeat loop, which is blocking the user interface (for example, when running your script without the shell script part and just the delay, menu items such as quit are blocked). Alternatives would be using NSTask or an indeterminite progress indicator.

Many thanks. At present I'm only using ScriptEditor to create this so I don't have easy access to NSTask and the userNotificationCenter:shouldPresentNotification: delegate method.


I have recently found there was a bug reported in AppleScript that was supposed to have been fixed in 10.11 (http://macscripter.net/viewtopic.php?id=45363 andhttps://developer.apple.com/library/content/releasenotes/AppleScript/RN-AppleScript/RN-10_11/RN-10_11.html#//apple_ref/doc/uid/TP40000982-CH111-DontLinkElementID_12http://macscripter.net/viewtopic.php?id=45363).


Maybe it's still an issue or has returned. Why would the notifications work when running in ScriptEditor but not when running as a separate app ?


I'll test what happens when I push the app into background or force the notification. But, normally, it would be at the front as the user goes through its process. I'll also test various ways of doing the delay.


Cheers.

I've used NSUserNotificationCenter with no problems - when I ran your posted script (minus the shell script), the notifications did get posted, the alert just wasn't displayed. AppleScriptObjC has been available in regular scripts since OS X 10.10 Yosemite, and the Script Editor can also create a Cocoa-AppleScript applet, complete with an application delegate file in the bundle.


The Script Editor runs scripts in separate processes so it can hook itself into them (logs, errors and alerts, etc), so stand-alone applications can run a bit differently, especially if there are user interface items.

Thanks. I'll try to get AppleScriptObjC working. I've had a bit of a go but the test run I did crashed constantly. I've a lot of learning to do.


I've found as you thought that the Notifications only display if the applet is hidden or inactive. So, as a kludge, the applet is hidden while the notification handler is running and is activated at the finish. It works except that a notificatin is always displayed when I close the applet later on. Timing of different processes seems to be an issue.


Cheers.

Notifications during "do shell script" not showing
 
 
Q