Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>The act of compiling on a <strong>tell application</strong> will afaik make the app launch.</p> <p>Calling the script from the command line with osascript from a pre-compiled file i.e .<strong>scpt</strong> does not cause this behaviour because the is no compiling to be done.</p> <p>But calling it from a plain text (.txt,.sh ) file will so the app will launch.</p> <p>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 <strong>run script</strong> command in the 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 run script "tell application \"Safari\" open location \"http://google.com\" end tell" return "Running" else return "Not running" end if </code></pre> <p>The script in the <strong>run script</strong> is only compiled when needed. You will need to escape any characters like quotes as in my example.</p> <p>It will be easier if you write the script in a normal applescript document first and compiled it to check for errors.</p> <p>Then copy it to the plain text file.</p> <hr> <p><strong>UPDATE</strong> **</p> <p>The method I used above was from a old script I had used to solved this issue a while before I answered here. </p> <p>The answer works and is not trying to be elegant. ;-)</p> <p>I actually like <strong>user1804762</strong> method below. As it does work but feel the Answer is not clear enough so I will give an example on using it.</p> <pre><code>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 </code></pre> <p>This script can be run from the command line with <strong>osascript</strong></p> <p>example:</p> <p><code>osascript /Users/USERNAME/Desktop/foo.scpt</code></p> <p>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.</p> <p>i.e.</p> <p><code>osascript /Users/USERNAME/Desktop/foo.applescript</code></p>
 

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