David Foster David Foster - 1 year ago 248
AppleScript Question

Check if an application exists using AppleScript

This code does not work for apps that do not exist because it prompts the user to look for "FooApp" (and I don't want to interact with the user):

get exists application "FooApp"

This code only works for apps whose process name matches its application name, which covers most but not all applications:

tell application "System Events"
get exists application process "FooApp"
end tell

(For example on my machine "OmniGraffle Professional" is a process name but the corresponding application name is "OmniGraffle Professional 4".)

Answer Source

Notice that your 2 scripts do different things. The first one checks if it is on the computer. The second one checks if it is currently running. So here's how to do the first thing.

set doesExist to false
    do shell script "osascript -e 'exists application \"foo\"'"
    set doesExist to true
end try

return doesExist

And note that as you point out some applications have a variety of names. In those cases you can use the bundle id of the app instead of it's name. Here's how to get the id of Safari and use it...

set appID to id of application "Safari"
exists application id appID

And if you wanted to see if it is running, like in your second script, you could do this...

set processIsRunning to true
tell application "System Events"
    set runningProcesses to processes whose bundle identifier is appID
end tell
if runningProcesses is {} then set processIsRunning to false
return processIsRunning
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download