The NSAppleScript class provides the ability to load, compile, and execute scripts.


This class provides applications with the ability to

  • load a script from a URL or from a text string

  • compile or execute a script or an individual Apple event

  • obtain an NSAppleEventDescriptor containing the reply from an executed script or event

  • obtain an attributed string for a compiled script, suitable for display in a script editor

  • obtain various kinds of information about any errors that may occur

When you create an instance of NSAppleScript object, you can use a URL to specify a script that can be in either text or compiled form, or you can supply the script as a string. Should an error occur when compiling or executing the script, several of the methods return a dictionary containing error information. The keys for obtaining error information, such as errorMessage, are described in the Constants section.

See also NSAppleScript Additions Reference in the Application Kit framework, which defines a method that returns the syntax-highlighted source code for a script.


Initializing a Script

init?(contentsOf: URL, error: AutoreleasingUnsafeMutablePointer<NSDictionary?>?)

Initializes a newly allocated script instance from the source identified by the passed URL.

init?(source: String)

Initializes a newly allocated script instance from the passed source.

Getting Information About a Script

var isCompiled: Bool

A Boolean value that indicates whether the receiver's script has been compiled.

var source: String?

The script source for the receiver.

Compiling and Executing a Script

func compileAndReturnError(AutoreleasingUnsafeMutablePointer<NSDictionary?>?)

Compiles the receiver, if it is not already compiled.

func executeAndReturnError(AutoreleasingUnsafeMutablePointer<NSDictionary?>?)

Executes the receiver, compiling it first if it is not already compiled.

func executeAppleEvent(NSAppleEventDescriptor, error: AutoreleasingUnsafeMutablePointer<NSDictionary?>?)

Executes an Apple event in the context of the receiver, as a means of allowing the application to invoke a handler in the script.


Error Dictionary Keys

If the result of init(contentsOf:error:), compileAndReturnError(_:), executeAndReturnError(_:), or executeAppleEvent(_:error:), signals failure (nil, false, nil, or nil, respectively), a pointer to an autoreleased dictionary is put at the location pointed to by the error parameter. The error info dictionary may contain entries that use any combination of the following keys, including no entries at all.

Instance Properties

var richTextSource: NSAttributedString?

Returns the syntax-highlighted source code of the receiver if the receiver has been compiled and its source code is available.


Inherits From