Safari AS Questions

I tried diggin through as many topics as I could to track down the answer to my questions but I don’t think I know exactly how to phrase my problem so I thought I’d ask for some help specifically.

First just so you know my AS abilities, I’ll list what i’ve accomplished so far

Ok, it’s short list.

moving on. Currently I am trying to put together a script to automagically logs on to a number of web based email accounts. At this point, I’m stuck trying to figure out how to tell AS to check the name of the foremost Safari window against a list of expected names. What I mean is, once the page has loaded, is the title of page “Log In” or “Inbox”? And depending on the answer, take the next appropriate step, log in or create a new tab and open the next webpage.

I’ve been trying to accomplish this with a simple:


tell application "System Events" to tell process "Safari" 
    set frontmost to true 
    set windowName to name of window 
end tell 

if windowName is {"Log In"} then 
    tell application "System Events" to tell process "Safari" 
        set frontmost to true 
              keystroke tab 
        keystroke "screen name" 
              keystroke tab 
              keystroke "password" 
              keystroke return 
        keystroke "t" using {command down} 
    end tell 
end if 

if windowName contains {"inbox"} then 
    tell application "System Events" to tell process "Safari" 
        set frontmost to true 
        keystroke "t" using {command down} 
    end tell 
end if 

Unfortunately every time I run this I receive an applescript error “Can’t get name of window”

My brain can’t seem to get past this and I’m in need of some assistance. Any thoughts, code, etc to push me past this? I really appreciate any help you might be able to provide.

Hi,

When you get a property of an object, you use a reference to the object. Looking in the Safari dictionary:

window by name, by numeric index, before/after another element, as a range of elements, satisfying a test, by ID

Here you don’t know the window’s name, but you can use it’s index. For front window:

tell app “Safari”
name of front window
end tell

or:

tell app “Safari”
name of window 1
end tell

both are the same. The indices go from front to back ordering. Range of elements would be something like:

tell app “Safari”
name of window 1 thru 3
end tell

Satisfying a test would be something like:

tell application “Safari”
name of every window whose name contains “macscripter bbs”
end tell

I didn’t look at the rest of the script yet, but hope it works.

gl,

Thanks Kel, I was pretty sure it was something simple and obvious, and that tip took care of my problem.

My next question is a matter of cleaning up my script.

Currently I am forcing my script to delay 30 seconds or so to fully load the pages before continuing the checks. Is there any way to have AS make a check to see if a page is fully loaded or not so that it doesn’t sit there waiting for a delay to complete?

Thanks again for the first tip.

Diq

Jacques, thanks for the tip on how to check for Stop/Reload status in the toolbar.

I actually ended up having to ajust it a bit to get it to work:


repeat
     if value of attribute "axdescription" of button "Stop/Reload" of group 2 of tool bar of window1 as string is {"reload"} then exit repeat -- fully load page
end repeat

Thanks again, works like a charm

Something’s up; I’m getting “System Events NSAS Receiver Evaluation Error: 4”

when I run either:

       repeat
           if value of attribute "axdescription" of button "Stop/Reload" of group 2 of tool bar of window1 as string is {"reload"} then exit repeat -- fully load page
       end repeat

or the first poster’s:

       repeat
           if value of attribute "axdescription" of button "Stop/Reload" of group 2 of tool bar of window ¬
               "Log In" as string is "reload" then exit repeat -- fully load page
       end repeat

The script works fine without the repeat loop, but I need to wait til the page loads.

Is there a reason that the repeat loop code wouldn’t be able to work with Firefox as the browser?