Web View ... Again

Hi there,

I am still struggling with displaying a web site in a web view. I have checked here and in other forums but couldn’t find a reference to my specific problem.

Everything works fine if I have the web view in a window. I am getting a reference to the web view in the “awake from nib” handler and the web site gets displayed fine.

However my web view is located in a tab view, meaning that when the app loads and the initial tab is not the one with the web view in it, the “awake from nib” handler will not be called for the web view and I don’t get the reference to it.

The question I have is: Is there a way to get that reference to the web view at a later stage in the execution of the app?

Any suggestions appreciated.

It is common to access things in applescript studio with nested references. You don’t have to do this every time you access the object, in fact you’re better not to. But doing it once and only once is a reasonable, thought not optimal, alternative to having it passed to a known handler.

At the top of the script file doing work with the web view you can put something like

property myWebView : main window's tab view 1's tab view item 2's ....

and then in the rest of your code refer to it as just myWebView

Thanks mooresan … I think I didn’t use the expression “reference” correctly in my previous post. I will try to make it clearer by giving an example. I used the following code to display a web site in a web view and had no problem with it:

property theUrl : "http://www.apple.com"

on awake from nib theObject
   if name of theObject is "webView" -- web view called webView in IB
	set theMethod to "stringByEvaluatingJavaScriptFromString:"
	set theScript to "location.href='" & theUrl & "'"
	call method theMethod of theObject with parameter theScript
    end if 
end awake from nib

. alternatively I could have said:

on awake from nib theObject
   if name of theObject is "webView" -- web view called "webView" in IB
      set theWebView to theObject
   end if 
end awake from nib

Now … the problem is that in my table view scenario if the app comes up with a tab view OTHER than “webView”, the “set theWebView to theObject” will never get called and theWebView will be “missing value” or “null” and hence useless.

I did also try something static like

set theWebView to web view "webView" of tab view item 1 of tab view 1 of window "main"

but I guess it didn’t like “web view” in there :frowning:

Sorry, I hope that explains the problem better…

Well I see two possible solutions but as always, I may not have understood the problem and YMMV.

  1. you could set theWebView as I showed as a property
  2. you could set it up in an awake from nib handler from a super view like
on awake from nib theTabView
    set theWebView to the view "webView" of the tab view item "webber" of theTabView
end 

note that although web view may or may not be a class as far as applescript studio is defined (and I suspect not, though I may be mistaken ^^) even if it is; it is also (by inheritance) the more generic “view” (just as most things in the nib are) so if it’s just the “web view x” part that’s giving you the problem you might try getting rid of the “web” bit� if that makes sense… yawn… maybe I should go beddy bye�""

Hmmm … this raises two follow up questions:

  1. What is the purpose of this notation ?
property myWebView : main window's tab view 1's tab view item 2's .... 

is it not the same as

property myWebView : view "webView" of tab view item "web" of tab view "tabs" of window "main"

  1. Is …
on awake from nib theTabView 
� � set theWebView to the view "webView" of the tab view item "webber" of theTabView 
end

the same as …

on awake from nib theObject
� � set theWebView to the view "webView" of the tab view item "webber" of theObject
end 

… and if not, when would you use one or the other? I have so far exclusively used the theObject form and have wondered as to why you would rename theObject :shock:

Thank you so much for your help so far.

  1. yes these are the same… it’s just a matter of how you want to do it…

I often use the first form when I am attempting to discover the object, adding one more item at a time. It feels natural for me to read this path going from general to specific having programmed other languages for quite some time. The “of” form is fine too… in fact in final code I prefer it b/c having the single quote follow a double quote in the case of named elements can be difficult to see and ensure

  1. Yes these are the same :slight_smile:

If you know the handler is only going to receive actions from a single object, might as well rename theObject to reflect the object being passed. If, say your awake from nib only reacts to your main menu (for example… most menus don’t need one, this is contrived), why not rename “theObject” to “mainMenu”? This is even one of apple’s reccomendations: I know it’s somewhere in those studio docs, but can’t find it right now ^_^; oh well…

@mooresan
thanks for these clarifications :slight_smile:

still …

property myWebView : view "webView" of tab view item "web" of tab view "tabs" of window "main"

… is getting rejected by Xcode and I am back to square one.

This …

on awake from nib theObject 
� �if name of theObject is "webView" -- web view called "webView" in IB 
� � � set theWebView to theObject 
� �end if 
end awake from nib 

… still works fine as long as the app starts up with the tab that includes the web view. (I am programatically switching to the tab that was selected when the app was closed the last time.)

Any more help is highly apreciated …

Rather than have the web view itself awake from nib, why not have a higher level object (say the tab view, or window) do the initialization… sorry if I didn’t make that clear that was what I was suggesting in the example above where I changed “theObject” to “theTabView”… If visibility is the only reason awake from nib isn’t being called (which seems fishy to me… it should be being called, I would double/triple check that nib checkbox… ) then let a visible object do the initialization for it.

Well, if I use a higher level object like “tabView” then theWebView will be referencing “tabView” as oposed to “webView”.

And … even if all “awake from nib” handlers are selected for every tab view item, this won’t work.

I am starting to believe I am totally off on how this works :rolleyes:.
Do I need to create an instance of the web view in IB??? I simply added a WebKit framework in Xcode.

Thanks again mooresan … I’ll keep on trying anyway.
Should anyone ever succeed with a web view in a tab view, lemme know.

Sorry, supiter, I know this is your thread, but… :oops:
This is my code:

call method "stringByEvaluatingJavaScriptFromString:" of webView with parameter "location.href='" & x & "'"

Where “webView” is the web view and “x” is the URL (eg, “http://www.google.com/”). It works fine.

But when I load a .txt file in the web view, it doesn’t accept javascript anymore (obviouslly). So, I need a different way to load a different URL. I tried these two and none of these work, while I think it should… (or shouldn’t???)

set mainframe to (call method "mainFrame" of webView)
call method "loadRequest:" of mainframe with parameter x --> ERROR "-[NSCFString _webDataRequestExternalRequest]: selector not recognized"
call method "loadHTMLString:baseURL:" of mainframe with parameters {x, ""} --> ERROR "-[NSCFString _web_hostString]: selector not recognized"

Do you know what could be the issue?

jj, with a webview in my main window, I simply give it an AS name of “web” and then use this code to load URLs (including local files using the “file://” protocol):

on load_webpage(this_URL)
	try
		set URLWithString to call method "URLWithString:" of class "NSURL" with parameter this_URL
		set requestWithURL to call method "requestWithURL:" of class "NSURLRequest" with parameter URLWithString
		set mainFrame to call method "mainFrame" of object (view "web" of window "main")
		call method "loadRequest:" of mainFrame with parameter requestWithURL
	on error the_error
		log the_error
	end try
end load_webpage

Jon

Oh, I see… My code was not working because it was the wrong code… :lol:
Thanks, Jon!
Just in case, a little confirmation questions… Apps with a web-view are Jaguar compatible? If answer is YES, then I have something wrong in my app… Cheers!

Hi Folks!

I am trying to build my first WebView, and seem to be making a very simple error…

I get this on debug window:

2004-08-24 11:47:53.403 teste2[24594] An uncaught exception was raised
2004-08-24 11:47:53.438 teste2[24594] *** class error for ‘WebView’: class not loaded
2004-08-24 11:47:53.449 teste2[24594] *** Uncaught exception: *** class error for ‘WebView’: class not loaded

teste2 has exited due to signal 5 (SIGTRAP).

This is my code:

on awake from nib theObject
	(*Add your script here.*)
	my load_webpage("http://www.apple.com")
end awake from nib
on load_webpage(this_URL)
	try
		set URLWithString to call method "URLWithString:" of class "NSURL" with parameter this_URL
		set requestWithURL to call method "requestWithURL:" of class "NSURLRequest" with parameter URLWithString
		set mainFrame to call method "mainFrame" of object (view "webView" of window "main")
		call method "loadRequest:" of mainFrame with parameter requestWithURL
	on error the_error
		log the_error
	end try
end load_webpage

Would you be so kind to tell me where is my basic mistake?

Thanks to you all

Bernardo Höhl

Rio de Janeiro - Brazil

It looks like you didn’t link against the webkit framework. In XCode go to Project > add frameworks… > /System/Library/Frameworks/Webkit.framework, and then re-build the application

Thanks mooresan!

That’s the missing detail.

It works.

It looks very obvious once you know it. Doesn’t it?

Hey…this thread has helped a good bit in teaching me about the web view. I’ve managed to get them working, at least. Here is the problem, though:

I have my web view in a drawer, but anytime I click a link in it my app segfaults. I tried making another web view on the main window (exactly the same, different applescript name) and applied the same lines of code to that. That one works fine.

Is there anything specific about the drawer that could be causing the glitch? Any other pointers?

THANKS!

As far as I can tell, the web view just won’t work in a drawer. I’ve tried creating new nib files and new projects, but they just don’t work. I can have a web view working fine in windows, but not in drawers…they load, but links cause a segfault.

In particular:

Exception: EXC_BAD_ACCESS (0x0001)
Codes: KERN_INVALID_ADDRESS (0x0001) at 0x041f0a3b

I don’t knw what memory the kernel is after, or why (if anyone is an ace at backtracing threaded crash reports, I can give them to you), but I’m totally stuck.

Do I just give up on this idea and use a separate window? It seems that the code should be modular enough to avoid such silliness. Or am I missing something else simple?

As you probably know, a drawer is a special kind of ‘view’ and is not a window. There are invariably going to be bugs, errors, conflicts…whatever you want to call them…with doing things that are “exceptionally creative” or just plain wacky. Applescript is a means to SOME ends, not all ends, and you may be pushing it’s limits here. AS does not have full, direct access to everything the cocoa framework offers, so unless you want to troubleshoot the gritty details and write some serious obj-c, you may be out of luck. As web views are only recently supported, and not technically “supported” at all by pure AS, you’re already getting a pretty big handout.

Perhaps a web view in a panel might be a better bet? Keep trying, but don’t let it hold your project and your enthusiasm back. Unfortunately it doesn’t seem that there are many people working with web views in AS yet, so you may have already seen all that folks here have to offer. :cry:

Keep on trucking…
j

Does anyone know how to get the source from a web view?

This is something I would really like to see as well.
I found some code in ObjC, but did not get it to work…

Erik