WebScriptObject Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/WebKit.framework
Availability
Available in OS X v10.3 and later.
Declared in
WebScriptObject.h
Companion guides
Related sample code

Overview

A WebScriptObject object is an Objective-C wrapper for a scripting object passed to your application from the scripting environment.

You can not create a WebScriptObject object directly. You get a window WebScriptObject object by sending windowScriptObject to your WebView object.

You can use key-value coding methods—for example, setValue:forKey: and valueForKey:—to get and set properties of a WebScriptObject object. You can also access properties by index using the setWebScriptValueAtIndex:value: and webScriptValueAtIndex: methods. Use the removeWebScriptKey: method to remove a scripting object property.

Not all properties and methods of a class are exported. Use the setValue:forUndefinedKey: and valueForUndefinedKey: methods to intercept access to properties that are not exported. Similarly, use the invokeUndefinedMethodFromWebScript:withArguments: method to intercept method invocations that are not exported.

If you want access to properties and methods defined in your own classes, use the methods in the WebScripting informal protocol to specify the properties and methods the class should export to WebKit’s JavaScript environment.

Use the callWebScriptMethod:withArguments: and evaluateWebScript: methods to execute scripts in the scripting environment.

Tasks

Getting and Setting Properties

Executing Scripts

Raising Exceptions

Getting a String Representation

Class Methods

throwException:

Raises an exception in the current script execution context.

+ (BOOL)throwException:(NSString *)exceptionMessage
Parameters
exceptionMessage

The exception message.

Return Value

YES if successful, NO otherwise.

Availability
  • Available in OS X v10.3 and later.
Declared In
WebScriptObject.h

Instance Methods

callWebScriptMethod:withArguments:

Returns the result of executing a method in the scripting environment.

- (id)callWebScriptMethod:(NSString *)name withArguments:(NSArray *)args
Parameters
name

The name of the method to invoke.

args

The values to pass to the method.

Return Value

The return value of the method. Returns WebUndefined if an exception is thrown in the JavaScript environment or the method has no return value.

Availability
  • Available in OS X v10.3 and later.
Related Sample Code
Declared In
WebScriptObject.h

evaluateWebScript:

Returns the result of evaluating a script in the scripting environment.

- (id)evaluateWebScript:(NSString *)script
Parameters
script

The script to evaluate.

Return Value

The scripting object. The format of the script is dependent on the target scripting environment. Returns WebUndefined if an exception is thrown in the JavaScript environment or there is no return value.

Availability
  • Available in OS X v10.3 and later.
Related Sample Code
Declared In
WebScriptObject.h

JSObject

Returns the JavaScript object corresponding to the receiver.

- (JSObjectRef)JSObject
Return Value

The JavaScript object corresponding to the receiver in the JavaScriptCore C API.

Availability
  • Available in OS X v10.5 and later.
Declared In
WebScriptObject.h

removeWebScriptKey:

Removes a property from a scripting environment.

- (void)removeWebScriptKey:(NSString *)name
Parameters
name

Property to remove.

Availability
  • Available in OS X v10.3 and later.
Declared In
WebScriptObject.h

setException:

Raises a scripting environment exception in the context of the current object.

- (void)setException:(NSString *)description
Parameters
description

Description of the exception.

Availability
  • Available in OS X v10.3 and later.
Declared In
WebScriptObject.h

setWebScriptValueAtIndex:value:

Sets the value of a property at the specified index.

- (void)setWebScriptValueAtIndex:(unsigned)index value:(id)value
Parameters
index

The index of the property.

value

The value of the property.

Availability
  • Available in OS X v10.3 and later.
Declared In
WebScriptObject.h

stringRepresentation

Returns a string representation of the receiver.

- (NSString *)stringRepresentation
Return Value

The string representation of the receiver.

Discussion

The coercion of nonstring objects is dependent on the scripting environment.

Availability
  • Available in OS X v10.3 and later.
Declared In
WebScriptObject.h

webScriptValueAtIndex:

Returns the value of a property at the specified index.

- (id)webScriptValueAtIndex:(unsigned)index
Parameters
index

The index of the property.

Return Value

The value of a property at index. Returns WebUndefined if an exception is thrown in the JavaScript environment.

Discussion

Accessing property values by index is dependent on the scripting environment.

Availability
  • Available in OS X v10.3 and later.
Declared In
WebScriptObject.h