---
title: "execute(withAppleEvent:completionHandler:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nsuserapplescripttask/execute(withappleevent:completionhandler:)"
---

# execute(withAppleEvent:completionHandler:)

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

## Declaration

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

```swift
func execute(withAppleEvent event: NSAppleEventDescriptor?) async throws -> NSAppleEventDescriptor
```

## Parameters

- `event`: The Apple event.
- `handler`: The completion handler Block that returns the result or an error. See doc://com.apple.foundation/documentation/Foundation/NSUserAppleScriptTask/CompletionHandler.

## Discussion

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

### Related Documentation

- [init(url:)](foundation/nsuserscripttask/init(url:)-2qgls.md)
