Script Studio web page fun...but help!

As Im sure everyone knows, making your own web page in script studio is really easy and fun. (The "WebView" topic has been discussed several times already so I hope this wont totally bore all of you that already know how to do this.)

  1. Make new AppleScript Application project.
  2. Add WebKit frameworks to the “Other Frameworks” folder in XCode
    (This is done by selecting “Add Frameworks” from the “Project Menu” and then choosing “WebKit”.)
  3. From the GraphicsView palette, add a WebView to your window
  4. From the Text palette, add a text field to the top of your window
  5. With control key down, drag from the text field to the WebView window and choose takeStringURLFrom: from the Target/Action section of the “Connections” section of the “Info Window” (in Interface Builder)

Now run the script and any url put into the text field will bring up the web page in the WebView (After pressing return). Really Cool.

I have a simple page set up so that in the morning when I get to work and can check out the world events and mac news before getting going. (During lunch I use Safari to do my usual browsing. My web app, however, has a nice simple interface that automatically takes me through the 3 pages I want to see in the morning before starting work.)

Anyway, my question/problem is this:

-I want to use the up and down arrow keys to scroll up and down in my web page (like in Safari) but to do this you have to click the mouse once in the WebView, wait a second while the page refreshes(?) and then click the mouse in the WebView again. After that, you can use the arrow keys for scrolling with no problems. This is really a little picky interface issue but I would like to set my app up so that when I go to a particular URL, it automatically sets things up so that I can start using the up and down arrow keys without having to go through the mouse clicks.

What do you do? Is there some kind of command like (Set the view “webView” to be the active target) (sort of like a first responder type of command) that would do it? I can`t seem to find anything in the documentation regarding it.

Any help would be greatly appreciated.

Thanks in advance.

There is a command for a window to make it become ‘key’, meaning to receive any keystrokes that are pressed while the application is in focus. Maybe the same can be applied to the webview.

From the Apple developer site:

http://developer.apple.com/documentation/AppleScript/Reference/StudioReference/sr3_app_suite/chapter_3_section_18.html

Maybe you can set it with something like:


set key of webview "thewebview" of window "window" to true

and link that to the on launched handler.

Hope that helps! :slight_smile:

Dear Elljah,

Thanks for the reply. Did you actually try this out? (If not, please set up a webView page and give it a shot. It`s simple and you can use the webView (as other posts have indicated) in other programs as well (not just for viewing web pages).

Anyway, I get a Syntax error (I may have left something out though), and I think its because maybe you cant refer to webViews like you do windows (or movie views for that matter.) If you look at the names of the 4 views in the “graphic views” section of the interface elements palette in IB, you see that the other options (NSOpenGLView, NSMovieView, NSQuickDrawView) all have the “NS” notation, whereas only the webView option doesnt. (Again, I may be way off base here, but it just seems to be special somehow.) There is no mention of webView in the Script Studio Building Apps pdf or the SS Terminology pdf. You can find example webView based apps in your "Developer-Examples-Webkit" folder though, but theyre all written in the C language (Objective-C?) so perhaps it`s not used that much in a typical AppleScript Studio project.

If I can`t work it out in the program, I could always just fall back on a mouse bassed Scripting Addition to put a few timely mouse clicks in the web page itself to make it the “key” window. But that would be kinda cheating.

Again, thank you for your comments and I appreciate you trying to help. (I wish more people would feel free to just jump in and try to offer advise. I think it`s good to have fun fooling around with AppleScript Studio, and then to use the knowledge gained for more serious apps you may want to use for business etc.)

Please give it a try and if you have any other ideas let me know.

bw
Follow Your Dreams

bwDreams,
Elijah’s suggestion is definitely not what you want, but his post did contain a reference to what will… at least in some situations. There are a lot of factors involved here, some of which you have no control over.

First, some web pages contain objects such as text fields and interactive media types, like flash movies or java applets. These objects can become the current field editor for a window, which makes them steal focus from the web view itself. Many web pages use javascript to focus a text field automatically, which makes the web view surrender it’s first responder status to that text field. Try going to [url=http://mail.yahoo.com/]http://mail.yahoo.com/[/url] for an example of this. In safari, firefox, and my test project, all exhibited the same behavior… all required me to click on the page background to return the web view itself to first responder so the arrow keys would be received…

Second, and I found this to be important, is to make sure that the text field that is providing the “takeStringURLFrom:” connection is an ‘enter only’ send action field. In IB, select your text field, and make sure that “Enter Only” is selected for the “Send Action On:” option in the “Attributes” pane. The ‘End Editing’ choice does not allow the text field to surrender first responder status after the web page changes. Selecting ‘Enter Only’ will perform the same, but will allow the first responder to change after the user presses return… the importance of which will be discussed next.

Third, you must set the first responder of the window to the web view whenever you make changes to it, or whenever you feel focus must be returned to it. Below is the code I used in my test app. Because the “Enter Only” setting of the text field will not send the command to load the default page automatically when the window opens, I included a subroutine to set the url of the web view which opens a default web page when the window opens (the code in the ‘opened’ handler below assumes that your text field has a default url entered in it when the page opens).

on opened theObject --> Attached to the window
	set tmpURL to (content of text field "URL" of window "window") as string
	load_webpage(tmpURL)
	tell theObject to set first responder to view "myWebview"
end opened

on end editing theObject --> Attached to the text field
	tell window "window" to set first responder to view "myWebview"
end end editing

on load_webpage(tmpURL) --> Loads the url of "tmpURL" into your web view
	try
		set URLWithString to call method "URLWithString:" of class "NSURL" with parameter tmpURL
		set requestWithURL to call method "requestWithURL:" of class "NSURLRequest" with parameter URLWithString
		set mainFrame to call method "mainFrame" of object (view "myWebview" of window "window")
		call method "loadRequest:" of mainFrame with parameter requestWithURL
	on error the_error
		log the_error
	end try
end load_webpage

Remember that if the html page loaded into the web view contains an auto-focused text field, the web view will still become first responder, but it will by default focus that text field, rather than letting the web view itself handle the key presses. All browsers I tested had this behavior, and overriding this would be unlikely in AS alone, and would be difficult to achieve when integrating cocoa subclasses into an ASStudio project.

Good luck, hope this helps…
j

Dear Jobu,

Thanks for the info. I tried it out and have a few comments:

  1. Youre right (of course). The 'enter only' option in IB surrenders first responder status away from the text field and thus eliminates one of the clicks that was necessary to activate the 'webView'. (I never would have thought of that! Ive always used ‘end editing’.)

  2. Actually, I didnt use the subroutine you suggested as I just set the text field with the URL (I have it set up so that every time I press the right arrow key it automatically loads the next pre-assigned web page.) I then included a: tell application "System Events" to keystroke return command and it loads fine. (I did actually set up a new project with your subroutine and I have to admit your coding is much more elegant.) The problem is, I found that the following command: tell window "window" to set first responder to view "myWebview" tended to create some instability with certain web pages that dont want to lose control of their first responder status (www.apple.com was one of them) though most pages are fine. I also had to stick in a two sec. delay between loading the page and giving the webView first responder status. Now, I can right arrow to the next page and then quickly scroll down without taking my hands of the arrow keys. Thanks. You saved the day. (I had been trying to add first responder status by referring to “myWebview” as ‘webView’ and not just ‘view’.)

Just as an aside, what do you do about pages (links) that want to open up in a new page? (www.macagent.com is an example) Our standard ASS webView page doesnt do anything (as I expected) when an open-up-in-a-separate-page link is clicked. Not that this is a big issue, but is there a setting in IB somewhere that Ive missed?

Again, thank you very much.