<!--
{
  "availability" : [
    "macOS: 10.8.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSUserAppleScriptTask/execute(withAppleEvent:completionHandler:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSUserAppleScriptTask(im)executeWithAppleEvent:completionHandler:"
  },
  "title" : "execute(withAppleEvent:completionHandler:)"
}
-->

# execute(withAppleEvent:completionHandler:)

Execute the AppleScript script by sending it the specified Apple event.

```
func execute(withAppleEvent event: NSAppleEventDescriptor?, completionHandler handler: NSUserAppleScriptTask.CompletionHandler? = nil)
```

## Parameters

`event`

The Apple event.

`handler`

The completion handler Block that returns the result or an error. See [`NSUserAppleScriptTask.CompletionHandler`](/documentation/Foundation/NSUserAppleScriptTask/CompletionHandler).

## Discussion

Pass `nil` as `event` to execute the script’s default “run” handler.

This method should be invoked no more than once for a given instance of the class.

If the script completed normally, the completion handler’s `error` parameter will be `nil`.

## See Also

[`init(url:)`](/documentation/Foundation/NSUserScriptTask/init(url:)-2qgls)

Return a user script task instance given a URL for a script file.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)