I agree with you on the good vehicle for learning as this forum along with others have been invaluable to me in sorting out issues that are often not described in other documentation.
The reason I was asking to go offline was for two reasons:
1) Not to have your library details published online;
2) To keep the forum listing short as the HTML source is quite long.
You are correct in the GUI AppleScript version will also likely break if the site changes and in some cases is more fragile even if the site does not change. GUI scripting is more susceptible to timing errors as AppleScript is in most cases much faster than the GUI response time. CPU loading has an influence on timing as well. Another issue with GUI scripting, but not in this case as tabbing is being used, is if GUI elements are not readily seen by System Events then they must be automated using screen locations which can easily be thrown off by the user changing the size or location of the window.
You should try the AppleScript version and play around with the delay time, which is in seconds, to get a better feel for some of what I am talking about above. Try setting the delay time to 0 and see whether an error is thrown. If you set the delay time to something high, say 5, you will notice that there won't be errors thrown if you just leave the machine alone but the user will be waiting around for a response, and may even do something, e.g. close the window or move to another application, that will throw off the rest of the script.
The GUI scripting is actually a side-effect of legislation that was brought in by the US goverment to reduce the barriers to physically impaired/challenged users operating computers. Assistive (which is a word) refers to hardware and/or software devices that enable these users to more effectively operate the GUI.
If you place the tab generation script into a function then it can be called before opening any new webpage. Here is an example that uses the past script to open the home page of the library, opens a new tab, and then loads the library catalog page.
click here to
open this script in your editor
<pre style="font-family: 'Monaco', 'Courier New', Courier, monospace; overflow:auto; color: #222; background: #DDD; padding: 0.2em; font-size: 10px; width:400px">property CardNumber : "1234567" -- Library Card Number
property LastName : "Smith" -- Patron's last name
property LibraryHomePage : "http://www2.libraryweb.org/"
property LibraryCatalogURL : "http://www.rochester.lib.ny.us:2080/cgi-bin/cw
cgi?5000REDIRXsetDatabase720"
set AccountInfo to CardNumber & "," & LastName
set LibraryScript to "document.location='" & LibraryHomePage & "'"
set MyAccountScript to "document.myform1.patronacc.value='" & AccountInfo & "'"
set SubmitMyAccountScript to "document.myform1.submit()"
set LibraryCatalogScript to "document.location='" & LibraryCatalogURL & "'"
tell application "Safari"
activate
make new document
do JavaScript LibraryScript in document 1
delay 2 -- use this to allow Safari to download page or the more complete solution already provided which tests status of page loading
-- Enter the Account info and submit the form
--do JavaScript MyAccountScript in document 1
--do JavaScript SubmitMyAccountScript in document 1
-- Go to the Library Catalog
my OpenNewSafariTab()
do JavaScript LibraryCatalogScript in document 1
end tell
to OpenNewSafariTab()
tell application "System Events"
if UI elements enabled then
tell process "Safari"
set frontmost to true
keystroke "t" using (command down)
delay 2
end tell
end if
end tell
end OpenNewSafariTab</pre>
Now to the JavaScript questions.
No you generally won't find those values in the webpages because they refer to the Document Object Model (DOM) of Safari which was mentioned in the discussion by John Franklin1. The DOM models the structure of the webpage as objects that contain other objects. So for example the document object contains form objects which in turn contain text entry fields or buttons.
You can either use routines like John provided that pull out the names of the objects, in this case forms, out of the HTML or you can look at the source HTML of the webpage. Here are the key lines in the source HTMl for the home webpage of the library for entering the account name and number:
<form method="post" name="myform1" tharget="newpage" action="index.asp?action=checkMyAccount">
<input id="patronacc" name="patronacc" type="text" class="searchArea" value="card number,last name" size="20">
The first line gives me the name of the form i.e. "myform1" and the second line gives me the name and default contents of the value property for the text entry field. So to set the patronacc text field to a new value I needed to set up the DOM path for that object, i.e. document.myform1.patronacc.value, and use the = operator to set that object to the new contents of the value property.
There is another way which is using the DOM more strictly by using the order of the elements of arrays of objects so in that case to set the same value the JavaScript reference would be document.forms[1].elements[4].value. The arrays in JavaScript start at 0 so myform1 is the second form on the page and patronacc text field is the 5 element of that form. Generally it is easier to work with named items.
PowerBook 12" Mac OS X (10.4.9)