Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript Built In Progress Indicator Not Working Properly

I'm having this odd problem with the new built in progress indicator in applescript. What makes it worse, is how poorly documented everything applescript is these days. I don't know if everyone else is having problems like this too, or not? I assume I'm using the code properly but even that is just a guess.


So the main problem is that I'm trying to use "set progress additional description" to set the 'subtitle' of the progress window. When I do this, it updates but not until after the commands that are after the update are completed. It makes no sense. Here is a test script I made that illustrates it pretty well. First look at the code and see when you expect the page to be updated. Then run the script as an applet and see when it actually gets updated. Grrr!!!


on run

set progress description to "Resizing NetBoot Image Volume..."

set progress total steps to 6


set progress additional description to "Step 1"

say "Performing step 1, this may take a few seconds"

set progress completed steps to 1


set progress additional description to "Step 2"

say "Performing step 2, this may take a few seconds"

set progress completed steps to 2


set progress additional description to "Step 3"

say "Performing step 3, this may take a few seconds"

set progress completed steps to 3


set progress additional description to "Step 4"

say "Performing step 4, this may take a few seconds"

set progress completed steps to 4


set progress additional description to "Step 5"

say "Performing step 5, this may take a few seconds"

set progress completed steps to 5


set progress additional description to "Step 6"

say "Performing step 6, this may take a few seconds"

set progress completed steps to 6



set progress additional description to "Completed"

display dialog "NetBoot Image Volume Resizing Complete" buttons {"OK"} default button 1

end run

Mac mini, OS X Yosemite (10.10.5), null

Posted on Aug 22, 2015 9:34 PM

Reply
8 replies

Aug 23, 2015 2:21 PM in response to l008com

I think the main problem is that your script blocks the UI while it is speaking (pretty much any script will block the UI most of the time), so the progress text only gets updated when the run loop is given a chance to get events. AppleScriptObjC will give you a bit more control, in this case using a handler to manually update the event queue is a little bit easier than recreating the progress dialog, for example:


use framework "Foundation"
use scripting additions

on run
  set progress description to "Resizing NetBoot Image Volume..."
  set progress total steps to 6
  repeat with steps from 1 to 6
    updateProgress(steps)
  end repeat
  set progress additional description to "Completed"
  display dialog "NetBoot Image Volume Resizing Complete" buttons {"OK"} default button 1
end run

on updateProgress(step)
  set progress additional description to "Step " & step
  set progress completed steps to step
  fetchEvents()
  say "Performing step " & step & ", this may take a few seconds"
end updateProgress

on fetchEvents() -- handle user events
  repeat -- forever
    tell current application's NSApp to set theEvent to nextEventMatchingMask_untilDate_inMode_dequeue_(current application's NSUIntegerMax, missing value, current application's NSEventTrackingRunLoopMode, true)
    if theEvent is missing value then -- none left
      exit repeat
    else
      tell current application's NSApp to sendEvent:theEvent -- pass it on
    end if
  end repeat
  return
end fetchEvents

Aug 24, 2015 12:06 AM in response to red_menace

If this is true:

pretty much any script will block the UI most of the time


Then, doesn't that make the progress feature relatively pointless?

I mean, apple script is supposed to be relatively simple. You shouldn't have to do as much coding as a full fledged application, otherwise what's the point.

My real script has the structure of the one I posted, but all of the "say" commands are actually do shell script commands. And one 'step' is some native stuff. For me, it makes more sense to just leave it as is and let the progress indicator be wrong. It just seems crazy that this is how this works.

Aug 24, 2015 6:06 PM in response to l008com

Then, doesn't that make the progress feature relatively pointless?


Pretty much. I thought it was way too late be added, since there were already a few stand alone third party progress applications, and once Cocoa applets became available even more progress libraries showed up.


The new progress works fairly well in the Script Editor, but your typical AppleScript tends to be one big monolithic chunk of code, so the user interface doesn't get much of a chance to update. Putting the typical AppleScript into an Xcode project usually winds up doing the same thing - you need to pay a bit more attention to the way you write your application (regardless of the language used) if you want to keep the UI responsive.


Regular AppleScript is single threaded and not really event driven, so things like repeat loops and shell scripts (unless run in the background) will lock up the UI unless steps are taken. As another example, take a look at your application menu (move the mouse pointer into the menu) while it is running - it is constantly getting beach balls, since the application is not responding to the system.

Aug 24, 2015 8:54 PM in response to l008com

The fetchEvents() handler in my earlier post lets the UI update by getting any events that may be backed up in the queue and passing them on. What I did in my example was to set the various parameters for the progress indicator, then call the handler right before performing the task to update the UI with the new settings.


Unless you run your shell script in the background, AppleScript will wait until it is finished (blocking the UI in the mean time), although running your shell scripts in the background gets interesting figuring out when they are complete so that you can update the progress.


You are correct that AppleScript isn't really designed for this kind of thing, although you can use AppleScriptObjC to leverage Cocoa classes such as NSTask, which does handle background tasks with completion notifications.

Applescript Built In Progress Indicator Not Working Properly

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.