How can I find out if certain processes (multiple) are running?

I have found several posts regarding how you can find out if particular process is running.

How can I find out if multiple processes are running?

I want to check if couple of apps are running. If they aren’t, I want to shut down the Mac.

Thanks!

Model: Mac Mini (Early 2009)
AppleScript: 2.3 (118)
Browser: Safari 535.7
Operating System: Mac OS X (10.6)

Something like this?


set appNames to {"Finder", "httpd", "System Events", "Mail"}

set AppleScript's text item delimiters to "\\|"
set expression to appNames as string
set AppleScript's text item delimiters to ""

set runningApps to every paragraph of (do shell script "ps -acwx -o command | grep -i " & quoted form of expression & " || echo 'false'")

if runningApps is {"false"} then
	tell application "System Events" to shut down
end if

I have to give it a try when I get to the Mac!

One question though. I have to change {“Finder”, “httpd”, “System Events”, “Mail”} to right app names, correct? In this case EyeTV and Turbo.264 HD.

Many thanks! :slight_smile:

I tried the script and put some own script too.

on run
	tell application "EyeTV"
		if is_recording is false then
			tell application "EyeTV" to quit
		end if
	end tell
	delay 15
	tell application "Turbo.264 HD"
		if isEncoding is false then
			tell Application "Turbo.264 HD" to quit
		end if
	end tell
	delay 15
	set appNames to {"EyeTV", "Turbo.264 HD"}
	
	set AppleScript's text item delimiters to "\\|"
	set expression to appNames as string
	set AppleScript's text item delimiters to ""
	
	set runningApps to every paragraph of (do shell script "ps -acwx -o command | grep -i " & quoted form of expression & " || echo 'false'")
	
	if runningApps is {"false"} then
		tell application "System Events" to shut down
	end if
end run

I get this kind of log:

tell application "EyeTV"
	get is_recording
		--> false
	quit
end tell
tell Application "Turbo.264 HD"
	get isEncoding
		--> true
end tell
tell current application
	do shell script "ps -acwx -o command | grep -i 'EyeTV\\|Turbo.264 HD' || echo 'false'"
		--> "EyeTV Helper
Turbo.264 HD"
end tell

Why is EyeTV Helper showing? I can’t find the process from Activity Monitor.

Should this script work?

It’s because grep looks if the word fits in the the line instead of matching the whole line. When you give the x option as an argument for grep it will match the entire line and will result only that line that matches exactly.

And yes you should alter the list appNames to the processes you want to lookup. The list runningApps will show you the processes that are running from the appNames list. When none of processes in appNames is running it will return {“false”}.

set appNames to {"Finder", "httpd", "System Events", "Mail", "iTunes"}

set AppleScript's text item delimiters to "\\|"
set expression to appNames as string
set AppleScript's text item delimiters to ""

set runningApps to every paragraph of (do shell script "ps -acwx -o command | grep -ix " & quoted form of expression & " || echo 'false'")

The script works perfectly. Thanks!

However I’m trying to accomplish another task.

I want to open all files from certain folder with Turbo.264 HD and start encoding.

I made this:

tell application "Turbo.264 HD"
open file "disk:folder"
encode

It’s simple and it works but won’t if folder is empty. If the folder is empty Turbo.264 HD says “…” was not added because it is in an unsupported format and then nothing happens.

I tried to add

delay 10
if isEncoding is false then
tell Application "Turbo.264 HD" to quit
else
		encode
	end if

but didn’t help.

I also tried

tell application "System Events"
	keystroke return

but didn’t help either.

Any ideas?

The best way is to look if the folder is empty or not before you start encoding

set theFolder to "disk:folder"

set folderIsEmpty to count of (every paragraph of (do shell script "ls " & quoted form of POSIX path of theFolder)) = 0

if you want to check for a certain file extension you can use

set theFolder to "disk:folder" --don't end theFolder with ":"
set theExtension to "txt"

set folderIsEmpty to (count of (every paragraph of (do shell script "ls " & quoted form of POSIX path of theFolder & "/*." & theExtension & " || echo ''"))) = 0

So when folderIsEmpty is true then you won’t start the encoding.

Thank you Sir! You are really my hero! :slight_smile:

I have used the script just week or two, so it takes time to get used to! :smiley:

Could you help me one more time?

I tried the same script to open files with Name Mangler and it worked beautifully.

I just need two additions.

  1. The folder contains .ts and .mpg files. I just want to open .mpg.

  2. Any idea how to remove files which have been edited x hours ago (outdated files)?

Thanks!