Example of "Application enhanced" progress bar

For anybody who is interested this is a example of a script that will run the “Application (enhanced)” “progress bar.” It only has 2 things in the script that are not needed for the progress bar, the the code for the “repeat with” and the “delay” command. These were added in so the example would actually do something. This minimal adding of code makes it easier to see what would have to be added to a script to use the progress bar.

It is the responsibility of the script to determine the “total number of steps” and what step it is currently on. “progress additional description” and “progress description” are customizable text descriptions that allows the scripter to put whatever text that is desired as long as the text can fit in the box.

To create the “Application enhanced” progress bar as an Applet do a “save as” using the format “Application (enhanced).” The “Application (Apple)” format will not be able to create an enhanced progress bar.

The progress bar calculates the time remaining, percent done and says which item number it is currently working on. You can implement part, all or none of the extra stuff I have included but the example does show were the basic things that always need to be in the script are and how they are done.

I have included in this post the “Application (enhanced)” Applet and a compiled bundle version. They both have the same code in them. The “Application (enhanced)” will not work when running it from the compiled bundle version. That only works as a “Application (enhanced)” applet. But the compiled bundle version does allow people to see the code if the user can’t access the code from the applet.

Here is the actual code for the script. Anybody is free to use this in whatever way they desire.

-- Rather than stopping the script if an error occurs in the SetSignificantFractionalDigits
-- handler this script just hides the value that would be displayed in the progress bar
-- and allows the script to keep running.  If you want to stop the script the output from
-- SetSignificantFractionalDigits can be for error checking and terminate the script if desired.
-- The comments for how this would work are in the comments at the beginnging of the
-- SetSignificantFractionalDigits handler.

on SetSignificantFractionalDigits(TheNumber, MaxNumOfDigits)
	-- This handler returns a string.  It does not stop the processing when an error occured
	-- since an error in this handler is not necessarly an error in the main body of the script. 
	-- But the errors are returned as special letter strings so the calling code can handle the
	-- error if needed.
	-- MaxNumOfDigits must be greater then or equal to zero. Specifing a maximum number
	-- of digits for MaxNumOfDigits is used becauase a common problem is specifing
	-- the maximum number of digits that can fit in the space in the the progress bar.
	-- This handler returns "D" then an invalid MaxNumOfDigits is sent in
	-- This handler returns "?" when an error occured other then an invalid number in MaxNumOfDigits
	try
		if (MaxNumOfDigits < 0) then
			return "D" -- Return error indicator
		else if (MaxNumOfDigits = 0) then
			-- Returns an integer with the fractional part of the real number removed
			return (TheNumber div 1) as string
		else
			-- Returns a real number with the specified number of digits
			return (((TheNumber * (10 ^ MaxNumOfDigits)) div 1) / (10 ^ MaxNumOfDigits)) as string
		end if
	on error
		return "?"
	end try
end SetSignificantFractionalDigits

set TotalItems to 70

set progress description to ""
set progress additional description to "calculating" -- The first few time estimates a bit wild so just display calcualting
set progress total steps to TotalItems
set progress completed steps to 0

set StartTime to current date
repeat with NumberProccesed from 1 to TotalItems
	
	
	set AmountProcessed to NumberProccesed / TotalItems
	-- Multiply by 100 to convert the decimal number to a percentage
	set PrecentProcessedStr to SetSignificantFractionalDigits(AmountProcessed * 100, 2)
	if (PrecentProcessedStr = "D") or (PrecentProcessedStr = "?") then
		-- Don't show this information on progress bar if an error occured in SetSignificantFractionalDigits
		set progress description to "Processing item " & NumberProccesed & " of " & TotalItems
	else
		set progress description to "Processing item " & NumberProccesed & " of " & TotalItems & ", " & PrecentProcessedStr & "% processed"
	end if
	
	set AmountLeftToProcess to 1 - AmountProcessed
	set ProcessingTimeSoFar to (current date) - StartTime
	
	set RuntimeSeconds to (current date) - StartTime
	-- Have to make sure the following lines are not executed when RuntimeSeconds = zero or a divide by zero will occur
	if (RuntimeSeconds ≠ 0) then
		if (NumberProccesed > 3) then -- After the first 3 passes the estimates are pretty close so start displaying them
			set FilesPerSecond to NumberProccesed / RuntimeSeconds
			
			set AmountOfFilesLeft to TotalItems - NumberProccesed
			set SecondsToCompletion to FilesPerSecond * AmountOfFilesLeft
			
			set SecondsLeft to SetSignificantFractionalDigits(SecondsToCompletion, 0)
			-- Don't show this information on progress bar if an error occured in SetSignificantFractionalDigits
			if (PrecentProcessedStr ≠ "D") and (PrecentProcessedStr ≠ "?") then
				-- Update the progress information
				set progress additional description to SecondsLeft & " seconds remaining"
			end if
		end if
	end if
	-- Update the progress bar
	set progress completed steps to NumberProccesed
	
	-- Create a delay to the actual progess movement can be seen
	delay 1
end repeat


Bill

“Application enhanced” progress bar.zip (1.7 MB)

3 Likes