Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to check in AppleScript if an app is running, without launching it - via osascript utility
    primarykey
    data
    text
    <p>Consider the following AppleScript:</p> <pre><code>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 </code></pre> <p>The problem: when I run this via the <code>osascript</code> 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.</p> <p>Is this an <code>osascript</code> 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 <code>osascript</code> to report a version number).</p> <p>If you comment out the <code>tell</code> / <code>end tell</code> lines, it behaves as I'd expect: if Safari is not running, it doesn't launch it, and prints "Not running". So it <em>seems</em> to me like the <code>tell</code> 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 <code>tell</code> is supposed to work, but since it <em>doesn't</em> work like this in AppleScript Editor, I guess not...</p> <p>In fact, here's another, madder, version with similar behaviour:</p> <pre><code>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 </code></pre> <p>This still always launches Safari, even though <code>tell</code> is inside an <code>if false</code> block after the return statement! (But again, this is fine in AppleScript Editor.)</p> <p>BTW, this behaviour isn't limited to Safari, but it also isn't universal:</p> <ul> <li>Affected apps include: Safari, TextEdit, iPhoto, AppleScript Editor, iTerm, ...</li> <li>Non-affected apps include: Google Chrome, iTunes, Preview, Mail, Terminal, Address Book, Echofon, ...</li> </ul> <p>So, does anyone have any ideas about how I might fix or route around this? Is it an <code>osascript</code> bug? Or am I missing something about AppleScript's semantics?</p> <p>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 <em>except</em> 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 <code>osascript</code>, other than <a href="http://appscript.sourceforge.net/">appscript</a>, which I don't want to use because <a href="http://appscript.sourceforge.net/status.html">it's no longer developed/supported/recommended</a>.</p> <p>Many thanks for all inputs / insights!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload