Identifying / Counting "Hidden" processes

I have a media center setup at home that automatically downloads and passes files to handbrakeCLI. I have a batch automator workflow i downloaded that will batch encode files if they are all in the folder when the script runs. In this case, they will be converted one at a time. Otherwise, i use another workflow that will parse the files one at a time but if there are multiple files they will all start-up separate instances of handbrakeCLI.

This is fine but I do not want to run more than 3 instances at one time. I am trying to devise a script that will count the number of handbrakeCLI instances running and not start the automator script to encode the file until another instance finishes.

One of the problems with HandbrakeCLI is that it is not reported by system events as a process so i was using the following shell command from applescript to list all the running processes.

do shell script "/bin/ps -ax

Then i found that i could combine this with grep and do something like this:

set processCount to do shell script "/bin/ps -ax | grep -c HandBrakeCLI"

if processCount = >3 then
   activate application "Encode_Files"
   error number -128
end if

But this has not worked reliably. If i just run the

do shell script "/bin/ps -ax | grep -c HandBrakeCLI"

it returns values of 2 with no handbrakeCLI running. For some reason its identifying 2 instances. Not sure why this is. But then if i just assume this to be the case and change my process count to 5 (2 + 3 instances) it does not work reliably either. Sometimes the script will work and only 3 instances start other times 4 instances start or even 5. Also, my handbrake automator script lables the files in green when it starts processing them so as not to try and process them.

Here is my entire script which I run as an embedded applescript inside Hazel (and hence the references to “theFile” which is how Hazel identifies the file to act on).

	set processCount to do shell script "/bin/ps -ax | grep -c HandBrakeCLI"
	set origFilepath to quoted form of POSIX path of (theFile as alias)
	set shellCommand to "automator -i " & origFilepath & " '/Users/bmorgan/Library/Services/Batch Rip ¢ Batch Encode (Finder)(TV).workflow' ;"
	delay 5
	tell application "Finder"
		if label index of theFile = 6 then
			error number -128
		else if processCount > "3" then
			error number -128
			do shell script shellCommand
		end if
	end tell
end try

Again script runs fine just doesnt obey the process arguments. Anyone have any idea why this is happening above and how to better fix it?

Much thanks.

If I run this without the “-c” parameter, the process returned is the shell script itself. Sometimes it’s the entire script, sometimes it’s the entire script followed by the “grep” part again.

Somewhere I ran across this method to stop the grep from matching itself

do shell script "/bin/ps -ax | grep -c [H]andBrakeCLI"
-- The regular expression (the H inside the square brackets) matches a character set which contains a single character (upper case H in this case) and the literal string "andBrakeCLI." So this grep can't self-match the original regular expression string, as it also includes the square brackets. 
--end try
do shell script "ps axc | egrep -ic handbrakecli"