JMichaelTX
1/13/2016 - 8:52 PM

[PROGRESS] How to Display Progress Bar Using ASObjC Runner [JXA] JavaScript for Automation

[PROGRESS] How to Display Progress Bar Using ASObjC Runner [JXA] JavaScript for Automation

/*
==============================================================================
PURPOSE:  Show how to display real Progress Bar using ASObjC Runner app in JXA
					• Redesigned to use FUNCTIONS

VER:	2.0		DATE:  Tue, Jan 12, 2016

AUTHOR: JMichaelTX 
        Please PM me with any bugs/issues/questions
				
	  • All of the credit for this functionality goes to Shane Stanley,
		  who wrote the ASObjC Runner app, and many other incredible ASObjC apps
			and libraries
		• My only real contribution is in putting this in functions, and
		  converting to JXA
			
REF:  	http://www.macosxautomation.com/applescript/apps/runner_vanilla.html

SHARED ONLINE:
	1.  AppleScript version was posted to MacScripter on Mon, May 18, 2015
		http://macscripter.net/viewtopic.php?pid=180778#p180778

REQUIRES:
	1.  Mac OS X Mavericks, or later
	2.  ASObjC Runner app (see free download below)
	2.  ProgressBar Functions (included below)
		1.  initProgressBar(psTitle, psMsg, piMax, piCurrent)
		2.  updateProgressBar(piCurrent, piMax)
		3.  closeProgressBar()

The free ASObjC Runner app may be downloaded from:
http://www.macosxautomation.com/applescript/apps/ASObjC_Runner.zip

Previously, there was no good way to show a standard progress bar from AppleScript.  
Yosemite adds a Progress Bar to AppleScript & JXA, but it is very poor, IMO.
This ASObjC Progress Bar is much, much better.
Also, it provides a solutoin for Mavericks.

This script provides a demo of the ASObjC Runner Progress Bar.  I adapted the 
demo code from MacOSXAutomation.com to use functions, and to convert to JXA.
===============================================================================
*/
var app = Application.currentApplication()
app.includeStandardAdditions = true

var sPBStatus = "TBD" // Will be set to "Cancel" if User Cancels.  Otherwise set to "OK"

//--- VARIABLES TO BE SET IN YOUR SCRIPT ---
var sPBTitle 	= "ASObjC Runner Progress Bar" 					//-- Window Title
var sPBMsg 		= "DEMO Progress Bar by ASObjC Runner" 	//-- Msg ABOVE progress bar

//--- FOR DEMO ONLY ---
var iStart = 1
var iEnd = 50
//---------------------------------------------

// *** INITIALIZE PROGRESS BAR ***
initProgressBar(sPBTitle, sPBMsg, iEnd, 0)

//--- REPLACE with your own LOOP ---

for (var iCurrent = iStart; iCurrent <= iEnd; iCurrent++) {
		
	//	-- *** UPDATE PROGRESS BAR JUST BEFORE YOUR PROCESSING ***
		var sPBStatus = updateProgressBar(iCurrent, iEnd)
		
	//	-- HANDLE CANCEL BY USER --
	
		if (sPBStatus === "Cancel") {
			break;
		} //––– end if –––
		
	//	--- INSERT YOUR PROCESSING HERE --
		delay(0.1)		// for demo only, not needed in production use
		
} //––– END for –––

//-- *** CLOSE PROGRESS BAR ***
closeProgressBar()

//--- HANDLE ANY CLEANUP IF USER CANCELED ---

if (sPBStatus === "Cancel") {

	app.displayNotification ("User CANCELED process", {title: sPBTitle})
	
	//	-- YOUR CODE HERE --

} else {

	app.displayNotification ("Process SUCCESSFULLY Completed", {title: sPBTitle})
	
} //––– end if User Canceled –––


//===================== END OF MAIN SCRIPT ===============

//–––––––––––––––––––––––––––––––––––––––––––––––––––––––––
//		FUNCTIONS
//–––––––––––––––––––––––––––––––––––––––––––––––––––––––––

/*
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
ASObjC Runner FUNCTIONS

	AUTHOR: JMichaelTX 
	REF:  	http://www.macosxautomation.com/applescript/apps/runner_vanilla.html
	
	CREDIT:
	  • All of the credit for this functionality goes to Shane Stanley,
		  who wrote the ASObjC Runner app, and many other incredible ASObjC apps
			and libraries
		• My only real contribution is in putting this in functions, and
		  converting to JXA
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
*/

//—————————————————————————————————————————————————————————————
function initProgressBar(psTitle, psMsg, piMax, piCurrent) {
//—————————————————————————————————————————————————————————————
	/*
	PURPOSE:  Initialize the ASObjC Runner Progress Bar	
	VER:  		2.0			DATE:  Tue, Jan 12, 2016
	
	-- NOTE:  The "name" and "message" properties need to be set only here.
	--			They will continue to show when the UPDATE is called 
	--			without being included in the properties there.
	*/
	//––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
	
	var ASORun = Application("ASObjC Runner")
	
	ASORun.resetProgress()
	
	ASORun.progressWindow.properties = {
			buttonTitle:			"Cancel"
			, buttonVisible:	true
			, name:						psTitle
			, message:				psMsg
			, detail:					""
			, indeterminate:	false
			, maxValue:				piMax
			, currentValue:		piCurrent
		}
	
	ASORun.showProgress()
	
} //––––––––– END function initProgressBar –––––––––––––––––––

//—————————————————————————————————————————————————————————————
function updateProgressBar(piCurrent, piMax) {
//—————————————————————————————————————————————————————————————
	/*
	PURPOSE:  UPDATE the ASObjC Runner Progress Bar	
	VER:  		2.0			DATE:  Tue, Jan 12, 2016
	*/
	//––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
	
	var ASORun = Application("ASObjC Runner")
	
	ASORun.progressWindow.properties = {
			detail:("This is number " + piCurrent + " of " + piMax)
			, currentValue:piCurrent
		}
				
		if (ASORun.progressWindow.buttonWasPressed()) {
			return "Cancel";
		} else {
			return "OK";
		}
		

} //––––––––  END function updateProgressBar –––––––––––––

//—————————————————————————————————————————————————————————————
function closeProgressBar() {
//—————————————————————————————————————————————————————————————
	/*
	PURPOSE:  HIDE the ASObjC Runner Progress Bar	
	VER:  		2.0			DATE:  Tue, Jan 12, 2016
	*/
	//––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
	
	var ASORun = Application("ASObjC Runner")
	ASORun.hideProgress()

} //––––––––––––  END function closeProgressBar –––––––––––––––