Is Application Busy Function...

This may not be the best way to do it, and if you have a better way please do share, but this is what I came up with. Some of the code is probably from other people on the site so thanks to them. I also didn’t combine too many commands, just in case someone is new and trying to figure some of the syntax out.

my isAppBusy("Adobe Photoshop")

on isAppBusy(theApp)
	set theApp to text 1 thru 10 of theApp
	set placementOffset to my CountItem(theApp, " ") as integer
	set thePlace to placementOffset + 3 as string
	set theCheck to do shell script "top -R -s 2 -l 2 | awk '/" & theApp & "*/ {busy= $" & thePlace & "}; END {print busy}'"
	set theCheck to my replace_chars(theCheck, "%", "")
	if (theCheck = "") then
		set theCheck to 0
	else
		set theCheck to theCheck as real
	end if
	
	-- If the app is using less than 2% of the processor it is probably not really busy
	if (theCheck is less than 2) then
		return false
	else
		return true
	end if
	
end isAppBusy

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

on CountItem(txt, Itm)
	set text item delimiters of AppleScript to Itm
	set Nbr to (count (text items of txt)) - 1
	set text item delimiters of AppleScript to ""
	return Nbr
end CountItem