Apple Event: May 7th at 7 am PT

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

Feb 7, 2007 10:55 PM in response to ZBurnett

Neither Safari nor Firefox have the capability to do anything like what you describe built into them. (In fact, Firefox's dictionary is almost completely empty.)

Safari does have a command which will let you execute an arbitrary line of JavaScript within a given document:

tell application "Safari"
do JavaScript "window.alert('Yay!');" in document 1 of window 1
end tell

You might be able to build JavaScript with Safari's DOM implementation which would do what you want.

Feb 11, 2007 4:07 AM in response to John Franklin1

Thanks - I'm pretty new to this... I looked up how to view dictionaries for certain aps, and that shows when an ap is NOT scriptable, that's useful too.

What do you need to write JavaScript?

Maybe going off to see what Netscape has built in might get me farther. I though Firefox had something built in, maybe they were just talking about automatically launching sites.

Feb 11, 2007 8:56 AM in response to ZBurnett

ZBurnett
One can create scripts that lets you login into certain pages, I have. Now, if you want to edit the pages, I'd be a little skeptic, but depending on what you have to do and the way thes pages are, you might.
My bet is you won't.
However, I use AS extensively to create websites and to manage them. For instance, I do not have an ftp browser, I use AS.
Do not forget, if you want to use AS or any other programming toop, you have to have something that can be automated, not something that you decide at that particlur moment.


Deivy

Feb 12, 2007 2:50 AM in response to deivy

I don't want to edit the website, just automate interacting with it. For example every week I go to our Library's website and click the exact same buttons, type in my library card number (which the browser already does an autofill after I give it the first character) then I order the list I get by Date and print the first page. Is this something AppleScript can handle?

It really seems like Automator ought to handle this but apparently not.

Feb 12, 2007 5:43 AM in response to ZBurnett

You can do what you are describing using Safari's "do JavaScript" command, but...

1. Your scripts are going to be highly customized (which means that if the web pages you have automated ever change, they will break)
2. You will need to know JavaScript
3. You will need to know Safari's DOM (Document Object Model)
4. You'll have to use some workarounds to get around some limitations in Safari's AppleScript model

I'm going to show you how to use the Do JavaScript command to do an Advanced Search in Google which will ask for search terms and a site to limit the search to, and then perform the search, all by manipulating the DOM on the Advanced Search page. (Probably not terribly effectively -- usually when I write JavaScript, I'm aiming for compatibility, not efficiency, so I may not be making use of shortcuts in Safari's DOM.)

First off: there is no easy way to tell when a page has finished loading. This is because of the way Safari's AppleScript references work. (The things you manipulate to load pages are "Document" objects, but references to Documents change when the URL loaded in the document changes, which means that when you create a new document and then load something, the reference breaks.)

In order to make a new window and keep track of it (for real), the following structure works:

tell application "Safari"
set x to make new document
repeat with y in the windows
if the document of y is x then
set x to the id of y
exit repeat
end if
end repeat
end tell

This will leave the id of the window in the variable x. Since the id does not change over time, that's what you want. Next, you need to load a page and wait for it to finish loading. (If you don't wait, then your scripts won't work.) Unfortunately, Safari has no direct AppleScript access to figure out when a page has finished loading. On the other hand, you can't get the source of a document until it has finished loading, so that makes a reasonable surrogate. (Note, however, that some pages which make heavy use of JavaScript may break this!) Using the variable x from the script above, you can use the following:

tell application "Safari"
set the URL of the document of window id x to "http://www.google.com/advanced_search?hl=en"
set still_loading to true
repeat while still_loading
try
if ((the source of document of window id x) is not "") then
set still_loading to false
else
delay 1
end if
on error
delay 1
end try
end repeat
end tell

This starts the loading process, by setting the URL of the document, and then repeatedly tries to see if the source of the document has a value. At the moment, the comparison to an empty string causes an error while the document is loading. This should not happen, so we catch the error just in case, but also handle it properly in the event Apple fixes this behavior in the future. Either way, as long as we can't get the source, we wait a second before checking again to avoid eating up all the processor time.

When the loop is done, the page has loaded, and it's time to play with it's content. Now, unfortunately for us, Google did not give any of their stuff IDs. When using JavaScript, it's easy to control a form that uses IDs: just use
( document.getElementById( 'theID' ) ).value = desiredValue;
Google decided not to use IDs. Instead they just used names. (Probably makes sense, given that names are required for forms to work and Google has to serve the same pages over and over -- by leaving IDs out, they save a few bytes on each page, and probably several GB over time. But it means we have to loop through every single piece of the form, looking for the part that has the name we want.)

Here's the final part of the AppleScript, which just stuffs a JavaScript into a string and then executes it:

set the_script to "var the_query = window.prompt('What should we search for?', 'Document Object Model');
var the_domain = window.prompt('What site should be searched?', 'developer.apple.com');
var f = (document.forms['f']).elements;
var n = f.length;
var index_number;
for (index_number = 0; index_number < n; i++)
{
if ((f[index_number].name) == 'as_q')
{
f[index_number].value = the_query;
}
if (f[index_number].name == 'as_sitesearch')
{
f[index_number].value = the_domain;
}
}
document.forms['f'].submit();"
tell application "Safari"
activate
do JavaScript the_script in (document of window id x)
end tell

Note that JavaScript can use either '' or "" to indicate a string, while AppleScript uses "". To avoid having to escape quotation marks, use '' inside the script.

(Also note that if you want to get a value from AppleScript into JavaScript, quotation marks are not the only problem. In JavaScript, \n is a new line, etc. For this reason, if you want the user to type something in, it's probably easier to use "window.prompt" inside the JavaScript section; I deliberately gave you two examples of this.)

I don't really know if there are any books on JavaScript I'd recommend wholeheartedly if you don't know the language already, given your specific needs. Most of them can be summed up with either "you only need to understand Internet Explorer for Windows so we won't teach you anything else" or else "we want to use the latest versions of DOM everywhere as well as show off showy stuff, so we're going to write massively long browser-detection scripts near the beginning of this book, and then waste a lot of pages talking about how to work around the massive incompatibilities between the ways different browsers implement their more advanced features". You just want to learn Safari, and even now most books barely mention it. I'd say to just experiment a lot, and search the web for examples. And if there an example gives alternate methods for Windows IE and Netscape/Firefox, the Firefox method is the one you probably want for Safari.

Arrgh! I forgot that the array accessor, [ arrayIndexNumber ] is also the code for italic text if you're using "i" to hold that number. Fixed that by changing the loop variable from "i" to "index_number".

Feb 12, 2007 6:34 AM in response to John Franklin1

Oops! I missed an instance of the variable "i" when I did the editing. So that last code block should really read:

set the_script to "var the_query = window.prompt('What should we search for?', 'Document Object Model');
var the_domain = window.prompt('What site should be searched?', 'developer.apple.com');
var f = (document.forms['f']).elements;
var n = f.length;
var index_number;
for (index_number = 0; index_number < n; index_number++)
{
if ((f[index_number].name) == 'as_q')
{
f[index_number].value = the_query;
}
if (f[index_number].name == 'as_sitesearch')
{
f[index_number].value = the_domain;
}
}
document.forms['f'].submit();"
tell application "Safari"
activate
do JavaScript the_script in (document of window id x)
end tell

Feb 15, 2007 8:54 PM in response to ZBurnett

ZBurnett,

I think it is feasible and I'll write the script for you.
For this forum purposes say your password is "password".
However, in the page you provided, it shows only one field for "card number,last name".
Are you typing both on the same field?
After typing your "whatever", you just click go or you do something else?
Say your userid is "userid".
Send me all you type please.


Deivy

Feb 16, 2007 3:22 AM in response to deivy

I see two fields: User ID and Last name... you don't need exactly what I'm typing because this is a script 🙂 That's all I type the rest is clicks. The ability to click on a button is what I'm interested in. How do you do that?

An alternative way to demonstrate capability would be to go into the Library Catalog, type AppleScript for a search and then click on one of the items that come up.

Feb 16, 2007 3:59 PM in response to ZBurnett

Alright, I have to press go to get to the userid page.
Well, anyway, you can script it using GUI scripting. It works on the page you gave me <http://www2.libraryweb.org/> because the tabs are all going to the "next field".
Alright, I'll give you a way to go straight to your userid account. Then it is up to you.
Say your card number is 111111111111 (twelve ones)
<script>
property target_URL : "http://www2.libraryweb.org/"
property card_ID : "111111111111"

open location target_URL -- this will open the webpage with you default browser
-- I assume it is Safari

tell application "Safari" to if name of window 1 does not contain "Loading" or name of window 1 is not "Untitled" then tell application "System Events"
delay 1.5
repeat 6 times
keystroke tab
delay 0.1
end
keystroke card_ID
delay 0.1
keystroke tab
delay 0.1
keystroke return
end tell
</script>

This script should put you in straight into your userid page.
From there on it is your call.
If you need further help let me know.

Deivy

Feb 19, 2007 12:56 AM in response to ZBurnett

Just incase you are still looking for a JavaScript solution here is one that I believe will work. I couldn't check the submit portion but it should work.

You will have to enter your own password and last name in the properties at the top of the script.

I have commented out the command to go to the Library Catalog because I didn't want both happening at the same time. It does work and just needs to be uncommented. If you are interested in adding any more navigation of the left-hand column you just need to look in the status bar of Safari when you run the mouse over each of those items. The URL for that location is given directly in the status bar so you can use a similar statement to the Library Catalog with the new URL and it should work.

Let me know if you need any further items automated.

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 LibraryCatalogURL : "http://www.rochester.lib.ny.us:2080/cgi-bin/cw cgi?5000REDIRXsetDatabase720"

set AccountInfo to CardNumber & "," & LastName
set SetMyAccountScript to "document.myform1.patronacc.value='" & AccountInfo & "'"
set SubmitMyAccountScript to "document.myform1.submit"

set LibraryCatalogScript to "document.location='" & LibraryCatalogURL & "'"

tell application "Safari"
activate

-- 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)

Feb 19, 2007 1:54 PM in response to lc55

I finally got around to testing the submit of the account info submission and there was a tiny mistake. The line should be:

set SubmitMyAccountScript to "document.myform1.submit()"

note the two brackets at the end.

Just for fun I was also able to manipulate the pull down menus i.e., "Hours & Locations" and "Quick Links" on the home page.

Advanced Search & Help can be manipulated by direct calls to the URLs that are shown in the status bar when the mouse is rolled over them.

Still looking at the Search related items (text entry field, Search button and radio buttons) at the top left of the page.


PowerBook 12" Mac OS X (10.4.8)

Mar 11, 2007 3:47 PM in response to lc55

Uh - This is interesting and quite a lot shorter than the AppleScript one... so I tried it first.

First it gave me compile errors... I think you meant to say
set "MyAccountScript" not "Set SetMyAccountScript"

Next... it launches Safari but it doesn't do anything. I edited my stuff into the two spots at the top.. I'll have to dig into it more.

Thanks!

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.