get html source code from a web view

I have a AppleScript Studio project that I am working on which is eventually going to help me post blog entries to my website from my desktop.
What I have so far is a XML-RPC call that searches my remote MySQL database, creates a new document, or gets the contents of an existing document, and takes the HTML source of that document and sets it to an editable instance of a web view and (for editing the raw html) an ordinary text view in a second (popup) window.

The actual HTML content is initially set in a variable called docContent. To assign it in its default, freshly downloaded sate to both the web view and the text view, I put this in my script.


	set rawHTML to text view "rawHtmlEdit" of scroll view "rawHtmlScroll" of window "rawHtmlWindow"
	set content of rawHTML to docContent
		
	set richText to view "webView" of window "editWindow"
	call method "setEditable:" of (richText) with parameters {true}
	call method "loadHTMLString:baseURL:" of (call method "mainFrame" of object richText) with parameter (docContent)

This works brilliantly. The web view shows the content laid out exactly as the html should be rendered, and I can add and delete words, etc…
The text area (which pops open in a new window on button click) shows the raw html of docContent. Perfect!

Now, when I change the raw html in the text view and I click “update”, this is executed:


	set rawHTML to text view "rawHtmlEdit" of scroll view "rawHtmlScroll" of window "rawHtmlWindow"
	set richText to view "webView" of window "editWindow"
	call method "setEditable:" of (richText) with parameters {true}
	call method "loadHTMLString:baseURL:" of (call method "mainFrame" of object richText) with parameter (content of rawHTML)
		
	close window of theObject

which basically repeats the initialization of the web view again, but this time loads it with the current content of the text view, so now the web view reloads and reflects the changes I made in the raw html. it also closes the rawHTML window when the “update” button is clicked.

So my question is when I change the content of the editable web view and I click the button to open the raw html text view, how can I get the source code of the newly edited web view so I can pass it to the text view, and of course eventually send it back to the server to be inserted into the database? Is it possible to get the source of the current web view?

-sD-
Dr. Scotty Delicious, Scientist.

Hi Scotty,

me again :wink:
Here’s how to access the edited source of a web view:

set mf to (call method "mainFrame" of (view "browser" of window "main"))
set dom to (call method "DOMDocument" of mf)
set ele to (call method "documentElement" of dom)
set theSource to (call method "outerHTML" of ele)

theSource contains the edited html as string …

regards,

Dominik

Thanks Dominik. I was trying to use call method (selectedDOMRange), but it wasn’t working. I am going to have to list you as the main author of this script soon!

-sD-
Dr. Scotty Delicious, Scientist.

I am impressed with how readable the apple documentation is. This particular page really helped me, although not as much as you have Dominik :wink:

-sD-
Dr. Scotty Delicious, Scientist

I think I am stuck again. I was trying to do this in ObjC, but I feel I am not even close. So far I have:

HTMLSource.h

[code]/* HTMLSource */

#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>

@interface HTMLSource : NSObject
{
NSString *_source;
}

  • (NSString*) sourceOf: (id)aWebView;
  • (void) setSourceOf: (id)aWebView withContent:(NSString*)contentString;

@end[/code]
HTMLSource.m

[code]#import “HTMLSource.h”
@class WebView;

@implementation HTMLSource

  • (NSString*) sourceOf: (id)aWebView
    {
    return [(DOMHTMLElement *)[[[aWebView mainFrame] DOMDocument] documentElement] outerHTML];
    }
  • (void) setSourceOf: (id)aWebView withContent:(NSString*)contentString
    {
    //code to set source of web view.
    }
    @end[/code]
    I have a feeling it is because I am asking for the parameter “aWebView” (which is of course the web view I want to get the source from) as (id) when it should be… I have no idea what it should be?

-sD-
Dr. Scotty Delicious, Scientist.

Hi Scotty,

I read your code, but I am not really sure about what exactly you want…
Is it still the WebView and NSTextView have the same content project?
If so, I suggest to create a controller, setting it as delegate of WebView and NSTextView and use some of their delegate methods to implement the updating among each other views. For example there are:

for the WebView:

[code]- (void)webViewDidChange:(NSNotification *)notification{
// and maybe

  • (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame{[/code]
    for the NSTextView (see documentation of NSText for this…):

[code]- (void)textDidChange:(NSNotification *)aNotification{
// or

  • (void)textDidEndEditing:(NSNotification *)aNotification{[/code]
    in the Controller you then have (at least) two IBOutlets - one for each view - let’s say they are called wv and tv:

IBOutlet WebView *wv; IBOutlet NSTextView *tv;
then for example this should update the text view tv on every change you make in the web view:

- (void)webViewDidChange:(NSNotification *)notification{ NSString *source = [(DOMHTMLElement *)[[[[notification object] mainFrame] DOMDocument] documentElement] outerHTML]; [tv setString:source]; }
Hope that helps … if not, feel free to pm me ( discussing ‘pure’ Objective-C here might be considered as off topic?? )

Dominik

Dominik,

Wow, thanks for posting this! You just saved me endless hours of hunting through the web kit docs!