How to tell if a program is running

Within a Filemaker Pro application I need to know if the application “iTunes” is currently open. I am using applescript to find this out. I have searched here and found two basic ways:

[
if (my test_process(“iTunes”)) then
display dialog “true”
else
display dialog “false”
end if

on test_process(which_app)
–this tests if the process “iTunes” is running
if (do shell script “ps -ax | grep /Applications”) contains which_app then
set processRunning to true
return processRunning
else
set processRunning to false
return processRunning
end if
end test_processt]

This is applescript is fast, but returns “true” even if the program “iTunes” is not open. Presumably it is because other programs open a process using “iTunes” in it’s name.

the other option is:

[tell application “System Events”
if exists process “iTunes” then
set is_running to true
else
set is_running to false
end if
end tell
]

This is slower, but accurately returns “false” if the user has not opened 'iTunes".

anyone have any thoughts on an accurate and fast option to this problem

thank you

Model: macbook pro
AppleScript: 2.6.1
Browser: Safari 537.77.4
Operating System: Mac OS X (10.8)

Hi,

third way


set iTunesIsRunning to application "iTunes" is running
display dialog (iTunesIsRunning as text)

Above looks nice and short. I was going to offer:


if application "iTunes" is running then
	set is_running to true
else
	set is_running to false
end if

display dialog is_running

Looks great thank you.

One question, so I understand.

In the script below:

set iTunesIsRunning to application “iTunes” is running

what part of the system software is being called to answer the question “Is iTunes running”?

Hi Beeman,

It was added to AppleScript in Mountain Lion, I think. I think Stefan said that. Not sure though.

gl,
kel

The application itself.
Every application regardless of the availability of an AppleScript dictionary is treated as an application object.

It responds to the basic AppleScript commands launch, activate, reopen and quit and “ in Mac OS 10.5 and higher “
it supports the properties is running, frontmost, version and id.

it’s not required that the application be running to access those properties

great information
thank you very much