Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript Web capabilities...

I'm curious how much capability I have with AppleScript. Can this tool control an existing website?

I think what I want to do is go to a website, get to the screen where the password is required, enter it, and then do some other formatting operations on that website, then print the first page of the results.

Does this have to be done with Safari or can another browser like Firefox accomplish this?

iMac G5, Mac OS X (10.4.8), 1G, stock otherwise

Posted on Feb 7, 2007 7:25 PM

Reply
20 replies

Mar 11, 2007 7:31 PM in response to ZBurnett

Sorry about that. I was working with a reduced script and the library home page already displayed. You were right about the other error as well.

You will find at the bottom what it should have been.

By the way I have worked on manipulating more of the pages that are available to a non-subscriber and was able to manipulate the following using JavaScript:

1) Entering text in the search field, modifying the radio buttons associated with the search field, clicking on the advanced search link, sending off the search.

2) Entering your account information, sending it off, getting Help on the account information;

3) Manipulating the pop up menus (Hours & Locations, Quick Links);

4) Clicking any of the menu items on the left-hand side;

5) Clicking on any of the links in the body of the page.

Essentially the whole home page can be controlled. Let me know if you have a specific request and I will work something up for you to try out.

I can't really do much else with the site without further guidance and probably getting user access.



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
--do JavaScript LibraryCatalogScript in document 1

end tell</pre>


PowerBook 12" Mac OS X (10.4.8)

Mar 13, 2007 5:19 PM in response to lc55

Wow! this is amazing... I'm a little confused as to where the JavaScript actually IS. Or maybe even what javaScript is.

This works great... the next things I'd want I'm not sure you can do for me without me giving you access... That is I want to list the stuff I have checked out in Date order and then print the first page (actually I want to set the date as to which I print up to... but that might be getting greedy) Still what is here is way faster than the non manual process I'm using every week.

the part you might be able to help me with is to open another TAB in Safari and do the identical thing you have done here, with another login setup. Beyond creating the tab it looks like I'd have to sort out the variables for the second one. The last name is the same so that variable could stay the same. Maybe I could use Automator to call two distinct scripts and stuff the print command after the call to the script!?

Thanks!

Mar 13, 2007 8:05 PM in response to ZBurnett

To give you an idea of what the JavaScript would look like if you were using it directly instead of sending it via AppleScript commands.

JavaScript to go to library homepage
**********************************
document.location='http://www2.libraryweb.org/'

JavaScript required for login
***************************
document.myform1.patronacc.value='1234567,Smith'
document.myform1.submit()

JavaScript required for going to Library catalog
**********************************************
document.location='http://www.rochester.lib.ny.us:2080/cgi-bin/cw cgi?5000REDIRXsetDatabase720'

These commands are sent as strings to the Safari JavaScript engine. I broke these strings up into pieces, inserted AppleScript variables to make it more flexible and then (concatenated) the pieces back together using the "&" and finally sent it off via the do JavaScript command in AppleScript.

I needed to look at the HTML source of the webpage to determine the name of the forms (myform1) and other variables that were used in the webpage.

If you don't mind communicating with me off the forum then you could send me the html source for the other pages you are interested in.

Here is the AppleScript required to open a new tab in Safari:

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">--Enable access for assistive devices must be checked under Universal Access in System Preferences

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
</pre>

PowerBook 12" Mac OS X (10.4.8)

Mar 22, 2007 3:48 AM in response to lc55

I guess I'd rather not jump out of this forum...it seems like a good vehicle for learning here. I've now read all the posts better and I'm wondering if you could comment on the disadvantages posted up there (the first long post) The JavaScript version seems way more compact... and I would think if the site changes an AppleScript version is going to break too. Still I haven't tried typing in the AppleScript version yet... but it's on my list

Anyway at first, I didn't notice your note about "assistive (is that a word?) devices" But after I clicked that on I have a script that logs in on one window and then opens a second tab. So can I duplicate all these "property" and "set" commands and duplicate the statements after the second tab is open?

Actually I think I want to ask first how you figure out the variables to pass via JavaScript. When I look at the site's page I don't find document.myform1.patronacc.value so I don't see how you knew to write that. That doesn't seem like it would be canned JavaScript so maybe you went somewhere else?

Mar 22, 2007 10:19 AM in response to ZBurnett

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)

AppleScript Web capabilities...

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.