Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if an application exists using AppleScript

Tags:

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".)

like image 954
David Foster Avatar asked Jan 12 '13 20:01

David Foster


3 Answers

@regulus6633 is right to point out you’re doing two separate things in your examples, and also his advice about bundle identifiers is spot on.

My preferred way to check if an application is installed is the following:

try
    tell application "Finder" to get application file id "bundle.id.here"
    set appExists to true
on error
    set appExists to false
end try

This avoids the “Where is application x?” dialog and assigns a boolean value to appExists. You could also display alert in the on error block (or anything you desire).

For your second example, you could write:

tell application "System Events"
    set processIsRunning to ((bundle identifier of processes) ¬
    contains "com.bundle.id.here")
end tell

It does almost exactly what @regulus6633’s code does, but grabs the list of processes and checks it in a single line. You also don’t have to worry about initialising processIsRunning.

If you're using application names just swap bundle identifier for name.

like image 75
robjwells Avatar answered Nov 10 '22 15:11

robjwells


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
try
    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
like image 30
regulus6633 Avatar answered Nov 10 '22 16:11

regulus6633


This is an old question but the answer may still be helpful. The following shell script doesn't launch the application, avoids the "Where is application x?" dialog if the application doesn't exist, accepts either the application name or bundle id as input, and returns the application's bundle id if it exists or an empty string if it doesn't:

set appBundleId to do shell script "osascript -e " & ("id of application \"" & appRef & "\"")'s quoted form & " || osascript -e " & ("id of application id \"" & appRef & "\"")'s quoted form & " || :"
set doesExist to (appBundleId ≠ "")

where appRef is either the application name or bundle id.

like image 20
scolfax Avatar answered Nov 10 '22 15:11

scolfax