How to check in AppleScript if an app is running, without launching it - via osascript utility

Consider the following AppleScript:

on is_running(appName)
    tell application "System Events" to (name of processes) contains appName
end is_running

set safRunning to is_running("Safari")
if safRunning then
    tell application "Safari"
      -- Stuff I only want executed if Safari is running goes here.
    end tell
    return "Running"
else
    return "Not running"
end if

The problem: when I run this via the osascript command line utility, if Safari is not running, it gets launched and the script reports "Running". This is not the behaviour I desire or would expect. Note that it works as desired/expected when run within AppleScript Editor.

Is this an osascript bug / known issue? Or is it somehow intended behaviour for reasons I'm missing? Can anyone get it to work as desired? (BTW I'm running OSX 10.7.5; I can't see how to get osascript to report a version number).

If you comment out the tell / end tell lines, it behaves as I'd expect: if Safari is not running, it doesn't launch it, and prints "Not running". So it seems to me like the tell is what's causing Safari to be launched, but it doesn't need to be actually executed, just present in the script...? For a while I wondered if maybe this was just how tell is supposed to work, but since it doesn't work like this in AppleScript Editor, I guess not...

In fact, here's another, madder, version with similar behaviour:

on is_running(appName)
    tell application "System Events" to (name of processes) contains appName
end is_running

set safRunning to is_running("Safari")
return safRunning
if false then
    tell application "Safari"
    end tell
end if

This still always launches Safari, even though tell is inside an if false block after the return statement! (But again, this is fine in AppleScript Editor.)

BTW, this behaviour isn't limited to Safari, but it also isn't universal:

  • Affected apps include: Safari, TextEdit, iPhoto, AppleScript Editor, iTerm, ...
  • Non-affected apps include: Google Chrome, iTunes, Preview, Mail, Terminal, Address Book, Echofon, ...

So, does anyone have any ideas about how I might fix or route around this? Is it an osascript bug? Or am I missing something about AppleScript's semantics?

For context: I'm trying to write a script (to be embedded/called from some python) which queries open browsers for the URLs of any tabs they have open; I've got it all working fine except that it always launches Safari, whether it's open or not. I've boiled down that undesirable behaviour to the simple test case shown above. I'm not aware of any way to run this script from python without using osascript, other than appscript, which I don't want to use because it's no longer developed/supported/recommended.

Many thanks for all inputs / insights!


Solution 1:

I suspect the reason you are getting this is because each time you call the script from the command line with osascript the script is being compiled.

The act of compiling on a tell application will afaik make the app launch.

Calling the script from the command line with osascript from a pre-compiled file i.e .scpt does not cause this behaviour because the is no compiling to be done.

But calling it from a plain text (.txt,.sh ) file will so the app will launch.

If you do not want to use a .scpt file and want to use a plain text file then you could try the trick of putting a run script command in the applescript.

on is_running(appName)
    tell application "System Events" to (name of processes) contains appName
end is_running

set safRunning to is_running("Safari")
if safRunning then
    run script "tell application \"Safari\" 
    
open location \"http://google.com\"
     
    end tell"
    return "Running"
else
    return "Not running"
end if

The script in the run script is only compiled when needed. You will need to escape any characters like quotes as in my example.

It will be easier if you write the script in a normal applescript document first and compiled it to check for errors.

Then copy it to the plain text file.


UPDATE **

The method I used above was from a old script I had used to solved this issue a while before I answered here.

The answer works and is not trying to be elegant. ;-)

I actually like user1804762 method below. As it does work but feel the Answer is not clear enough so I will give an example on using it.

set appName to "Safari"

if application appName is running then
    
    tell application id (id of application appName)
        
        open location "http://google.com"
    end tell
    return "Running"
else
    return "Not running"
end if

This script can be run from the command line with osascript

example:

osascript /Users/USERNAME/Desktop/foo.scpt

Notice that the script is saved as a compiled script. This will work ok and you can also save and use it as a plain text script.

i.e.

osascript /Users/USERNAME/Desktop/foo.applescript

Solution 2:

Some Info:

"Enhanced Application Object Model":

tell application "iTunes"
    if it is running then
      pause
    end if
end tell

You can also do it that way:

if application "iTunes" is running then
    tell application "iTunes" to quit
end if

You can also do this:

get name of application "iTunes"
get version of application "iTunes"

And to complete the journey:

get id of application "TextEdit" --> "com.apple.TextEdit"
tell application id "com.apple.TextEdit"
    make new document
end tell

That was the "Enhanced Application Object Model". If an app still launches (for example, the first time you compile & execute the script) I assume it is because AS has to get some info from the app which it did not found in the dictionary (or something like that...?).

Solution 3:

OK, I know this question is really old, but I stumbled on it looking for a different issue and had to pipe in considering how complicated some of these responses are.

The simple code to achieve what you want(ed) is:

tell application "System Events"
  if application process "Safari" exists then
    -- do stuff you want to do only if Safari exists
  end if
end tell

On older systems, the syntax used to be:

tell application "System Events"
  if exists of application process "Safari" is true then
    -- do stuff you want to do only if Safari exists
  end if
end tell

One of these should definitely work for you, intrepid searcher of Applescript solutions for action only when an app is running.


Oh! Bonus tip: And if you're not sure what the application process name is exactly (it is usually but not always the app name), before coding your final script run…

tell application "System Events"
   get every application process
end tell

And find your app process name in the results.

Here's a screen grab of running that command. (Note the zillions of Google Chrome Helper instances. Thanks Google!)

enter image description here

HTH!