<!--
{
  "documentType" : "article",
  "framework" : "ObjectiveC",
  "identifier" : "/documentation/ObjectiveC/webscripting",
  "metadataVersion" : "0.1.0",
  "role" : "collectionGroup",
  "title" : "WebScripting"
}
-->

# WebScripting

`WebScripting` is an informal protocol that defines methods that classes can implement to export their interfaces to a WebScript environment such as JavaScript.

## Discussion

Not all properties and methods are exported to JavaScript by default. The object needs to implement the class methods described below to specify the properties and methods to export. Furthermore, a method is not exported if its return type and all its parameters are not Objective-C objects or scalars.

Method argument and return types that are Objective-C objects will be converted to appropriate types for the scripting environment. For example:

- `nil` is converted to undefined.
- `NSNumber` objects will be converted to JavaScript numbers.
- `NSString` objects will be converted to JavaScript strings.
- `NSArray` objects will be mapped to special read-only arrays.
- `NSNull` will be converted to JavaScript’s `null`.
- `WebUndefined` will be converted to undefined.
- `WebScriptObject` instances will be unwrapped for the scripting environment.

Instances of all other classes will be wrapped before being passed to the script, and unwrapped as they return to Objective-C. Primitive types such as `int` and `char` are cast to a numeric in JavaScript.

Access to an object’s attributes, such as instance variables, is managed by key-value coding (KVC). The KVC methods `setValue:forKey:` and `valueForKey:` are used to access the attributes of an object from the scripting environment. Additionally, the scripting environment can attempt any number of attribute requests or method invocations that are not exported by your class. You can manage these requests by overriding the `setValue:forUndefinedKey:` and `valueForUndefinedKey:` methods from the key-value coding protocol.

Exceptions can be raised from the scripting environment by sending a <doc://com.apple.documentation/documentation/WebKit/WebScriptObject/throwException(_:)> message to the relevant `WebScriptObject` instance. The method raising the exception must be within the scope of the script invocation.

## Topics

### Getting attributes

[`webScriptName(forKey:)`](/documentation/ObjectiveC/NSObject-swift.class/webScriptName(forKey:))

Returns the scripting environment name for an attribute specified by a key.

[`webScriptName(for:)`](/documentation/ObjectiveC/NSObject-swift.class/webScriptName(for:))

Returns the scripting environment name for a selector.

[`isSelectorExcluded(fromWebScript:)`](/documentation/ObjectiveC/NSObject-swift.class/isSelectorExcluded(fromWebScript:))

Returns whether a selector should be hidden from the scripting environment.

[`isKeyExcluded(fromWebScript:)`](/documentation/ObjectiveC/NSObject-swift.class/isKeyExcluded(fromWebScript:))

Returns whether a key should be hidden from the scripting environment.

### Invoking methods

[`invokeDefaultMethod(withArguments:)`](/documentation/ObjectiveC/NSObject-swift.class/invokeDefaultMethod(withArguments:))

Executes when a script attempts to invoke a method on an exposed object directly.

[`invokeUndefinedMethod(fromWebScript:withArguments:)`](/documentation/ObjectiveC/NSObject-swift.class/invokeUndefinedMethod(fromWebScript:withArguments:))

Handles undefined method invocation from the scripting environment.

### Finalizing

[`finalizeForWebScript()`](/documentation/ObjectiveC/NSObject-swift.class/finalizeForWebScript())

Performs cleanup when the scripting environment is reset.

## See Also

  [WebKit Objective-C Programming Guide](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/DisplayWebContent/DisplayWebContent.html#//apple_ref/doc/uid/10000164i)



---

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)