title: Apple Script fails when run from within a macOS Swift-based application
body: <p><strong>SOLUTION:</strong>
  I needed to do two things to solve my problem:</p>
  
  <p>1) Add the following to my app's entitlements file:</p>
  
  <pre><code>&lt;key&gt;com.apple.security.temporary-exception.apple-events&lt;/key&gt;
  &lt;array&gt;
    &lt;string&gt;com.apple.Mail&lt;/string&gt;
  &lt;/array&gt;
  </code></pre>
  
  <p>2) Add the following to my app's plist:</p>
  
  <pre><code>&lt;key&gt;NSAppleEventsUsageDescription&lt;/key&gt;
  &lt;string&gt;Please allow "your message here"&lt;/string&gt;
  </code></pre>
  
  <p>This works on Mojave 10.14 (18A389)</p>
  
  <hr>
  
  <p>I have a basic Swift-based macOS app. I'd like to use Apple Script to determine the number of unread email messages in Mail.app. I created a small Apple Script and tested it in Script Debugger (it works fine).</p>
  
  <p>When I run the same script from within my app though it fails stating that the Mail application isn't running (even though it is):</p>
  
  <pre><code>let unreadEmailsScript = """
                           tell application "Mail"
                             get unread count of inbox
                           end tell
                           """
   if let scriptObject = NSAppleScript(source: unreadEmailsScript) {
     var errorDict: NSDictionary? = nil
     scriptObject.executeAndReturnError(&amp;errorDict)
  
     if let error = errorDict {
       print(error)
     }
   }
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>{
      NSAppleScriptErrorAppName = Mail;
      NSAppleScriptErrorBriefMessage = "Application isn't running.";
      NSAppleScriptErrorMessage = "Mail got an error: Application isn't running.";
      NSAppleScriptErrorNumber = "-600";
      NSAppleScriptErrorRange = "NSRange: {33, 12}";
  }
  </code></pre>
  
  <p>I haven't executed Apple Script from within an app before. Is this error due to a permissions problem?</p>