Mac Developer Library

Developer

WebKit Framework Reference WebScriptObject Class Reference

Options
Deployment Target:

On This Page
Language:

WebScriptObject

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.

Inheritance


Conforms To


Import Statement


Swift

import WebKit

Objective-C

@import WebKit;

Availability


Available in OS X v10.4 and later.
  • Returns the JavaScript object corresponding to the receiver.

    Declaration

    Swift

    func JSObject() -> Unmanaged<JSObject>!

    Objective-C

    - (JSObjectRef)JSObject

    Return Value

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

    Import Statement

    Objective-C

    @import WebKit;

    Swift

    import WebKit

    Availability

    Available in OS X v10.5 and later.

  • Removes a property from a scripting environment.

    Declaration

    Swift

    func removeWebScriptKey(_ name: String!)

    Objective-C

    - (void)removeWebScriptKey:(NSString *)name

    Parameters

    name

    Property to remove.

    Import Statement

    Objective-C

    @import WebKit;

    Swift

    import WebKit

    Availability

    Available in OS X v10.3 and later.

  • Returns the value of a property at the specified index.

    Declaration

    Swift

    func webScriptValueAtIndex(_ index: UInt32) -> AnyObject!

    Objective-C

    - (id)webScriptValueAtIndex:(unsigned int)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.

    Import Statement

    Objective-C

    @import WebKit;

    Swift

    import WebKit

    Availability

    Available in OS X v10.3 and later.

  • Sets the value of a property at the specified index.

    Declaration

    Swift

    func setWebScriptValueAtIndex(_ index: UInt32, value value: AnyObject!)

    Objective-C

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

    Parameters

    index

    The index of the property.

    value

    The value of the property.

    Import Statement

    Objective-C

    @import WebKit;

    Swift

    import WebKit

    Availability

    Available in OS X v10.3 and later.

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

    Declaration

    Swift

    func callWebScriptMethod(_ name: String!, withArguments args: [AnyObject]!) -> AnyObject!

    Objective-C

    - (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.

    Import Statement

    Objective-C

    @import WebKit;

    Swift

    import WebKit

    Availability

    Available in OS X v10.3 and later.

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

    Declaration

    Swift

    func evaluateWebScript(_ script: String!) -> AnyObject!

    Objective-C

    - (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.

    Import Statement

    Objective-C

    @import WebKit;

    Swift

    import WebKit

    Availability

    Available in OS X v10.3 and later.

  • Raises an exception in the current script execution context.

    Declaration

    Swift

    class func throwException(_ exceptionMessage: String!) -> Bool

    Objective-C

    + (BOOL)throwException:(NSString *)exceptionMessage

    Parameters

    exceptionMessage

    The exception message.

    Return Value

    YEStrue if successful, NOfalse otherwise.

    Import Statement

    Objective-C

    @import WebKit;

    Swift

    import WebKit

    Availability

    Available in OS X v10.3 and later.

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

    Declaration

    Swift

    func setException(_ description: String!)

    Objective-C

    - (void)setException:(NSString *)description

    Parameters

    description

    Description of the exception.

    Import Statement

    Objective-C

    @import WebKit;

    Swift

    import WebKit

    Availability

    Available in OS X v10.3 and later.

  • Returns a string representation of the receiver.

    Declaration

    Swift

    func stringRepresentation() -> String!

    Objective-C

    - (NSString *)stringRepresentation

    Return Value

    The string representation of the receiver.

    Discussion

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

    Import Statement

    Objective-C

    @import WebKit;

    Swift

    import WebKit

    Availability

    Available in OS X v10.3 and later.