russellsayshi
russellsayshi

Reputation: 2569

How can I use AppleScriptObjC to get the current url of a web view in Xcode?

I have a webView in Xcode, and after a few seconds of browsing I want to be able to get the current URL of the web view. I've searched around but I can't find how. Any help would be appreciated!

Upvotes: 0

Views: 492

Answers (1)

NSGod
NSGod

Reputation: 22938

If you set the WebView's frameLoadDelegate outlet to your AppleScript class in the nib file, you can implement the AppleScript-Objc equivalent of the WebFrameLoadDelegate's webView:didStartProvisionalLoadForFrame: method to be informed when the WebView starts loading a new page (which comes from WebKit Objective-C Programming Guide - Loading Pages - Displaying the Current URL):

on webView_didStartProvisionalLoadForFrame_(sender, frame)
    log "webView_didStartProvisionalLoadForFrame_"

    if frame is equal to sender's mainFrame then
        set currentURLRequest to frame's provisionalDataSource()'s request
        log currentURLRequest

        set theURL to currentURLRequest's mainDocumentURL()
        log theURL

        set theURLString to theURL's absoluteString()
        log theURLString

        set textField's stringValue to theURLString

    end if

end webView_didStartProvisionalLoadForFrame_

In this sample code, I set a text field's string value to the current URL, like a browser usually does when you navigate to another page. You could always store this value in an AppleScript property if need be.

Sample Project: WebViewFinaglerAS-Objc.zip

Upvotes: 2

Related Questions