AS delay until URL done loading???

Just wondering if there was a way to make AS wait until a Page was done loading in Safari before continuing with the rest of the script.

I am attempting to login to multiple webmail accounts with each account in a different tab. Once the page is done loading my user & pw are filled via keychain. Since by default I can hit enter to sign-in I’m using “keystroke” to do so since I couldn’t get the UI elements coding for the button to work right.

Right now I have “delay x” (where x is # of seconds) but this is sometimes “missed” because of the delay in the page loading for a variety of reasons (i.e. connection slow, network delay, time of day, etc.) so the next action is taken and the script sometimes bombs because the user/pw info hasn’t been populated yet. :confused:


tell application "Safari"
	activate
end tell
tell application "System Events"
	tell process "Safari"
		tell menu bar 1
			click menu item "New Window" of menu "File"
		end tell
		tell menu bar 1
			click menu item "New Tab" of menu "File"
			open location "http://webmail.west.cox.net/"
			delay 5
			keystroke return
		end tell
		tell menu bar 1
			click menu item "New Tab" of menu "File"
			open location "http://www.mac.com/WebObjects/Webmail.woa/wa/default?aff=consumer&cty=US&lang=en&identifier=NDUohWFuFt1LS8mb&src=wl"
			delay 7
			keystroke return
		end tell
		tell menu bar 1
			click menu item "New Tab" of menu "File"
			open location "http://gmail.google.com/
			delay 8
			keystroke return
		end tell
	end tell
end tell


TIA,
Chip

AppleScript: 2.1
Browser: Safari 412.2
Operating System: Mac OS X (10.4)

I can’t promise this works, but…
Safari shouldn’t allow you to “View Source” until a page is fully loaded. I can’t test this because I don’t have a slow enough modem :smiley: but give this a try and let us know how it works:


repeat
	--Initiate a variable
	set HTMLdata to ""
	--Attempt to set the variable to the html source. If it fails, it repeats until it succeeds. Once the page fully loads and the source is available, the script exits.
	try
		tell application "Safari" to set HTMLdata to source of document 1
	end try
	--Exit the loop once the source is available
	if HTMLdata is not "" then exit repeat
end repeat

SC

This does :smiley:


repeat
	tell application "Safari" to set LoadStatus to name of window 1
	
	if LoadStatus does not contain "Loading" then exit repeat
end repeat

After I made the first post I downloaded a 45 mb QT movie into a window. I noticed “Loading” appeared in the window name while the file loaded. I checked the name property of the window and it had “Loading” too. Bingo.
I decided not to edit the above post to avoid confusion. (On my part)
SC

SC,

So far so good, will keep testing but it hasn’t bombed yet!!

Sweet!! THX!! :D:D:D

I am afraid I have to retract that statement…

with this addition the page should fully load (and there are no redirects so the URL is for the page I want) then the delay should kick in, execute the keystroke and then open new tab…

But what is happening is the page starts to load and and the delay kicks in, I know this because I can see the button being activated before the page is done loading sometimes, so it doesn’t look like the name of the window is set to loading in this case.

I will have to try the other method mentioned first to see if that works.

This method errors:

“The variable HTMLdata is not defined.”

I placed right after the “open location” string

could it be that the document reference is incorrect since I want it to apply to the open tab? Does Safari by default always call the frontmost tab in any window document1?

The key is letting the “page load” be its own event, inside its own tell block. That way, nothing will be executed until that block is done. Then watch the page: the name of the window will change to the frontmost tab, and will include “Loading” while the page is loading. Here is the basic structure with the tell blocks lowlighted so you can see what I mean. It should work as is below:


tell application "Safari"
	activate
end tell

tell application "System Events"
	tell process "Safari"
		-- tell menu bar 1 
		--    click menu item "New Window" of menu "File" 
		--  end tell 
		-- tell menu bar 1 
		--    click menu item "New Tab" of menu "File" 
		--   open location "http://webmail.west.cox.net/" 
		-- end tell 
	end tell
end tell

delay 2 --Allow name of new tab to register

repeat
	tell application "Safari" to set LoadStatus to name of window 1
	
	if LoadStatus does not contain "Loading" then exit repeat
end repeat

delay 1 --Wait a second after page is loaded for good measure


keystroke return

tell application "System Events"
	tell process "Safari"
		--tell menu bar 1 
		-- click menu item "New Tab" of menu "File" 
		-- open location "http://www.mac.com/WebObjects/Webmail.woa/wa/default?aff=consumer&cty=US?=en&identifier=NDUohWFuFt1LS8mb&src=wl" 
		--end tell 
	end tell
end tell

delay 2 --Allow name of new tab to register

repeat
	tell application "Safari" to set LoadStatus to name of window 1
	
	if LoadStatus does not contain "Loading" then exit repeat
end repeat

delay 1 --Wait a second after page is loaded for good measure


keystroke return

tell application "System Events"
	tell process "Safari"
		--tell menu bar 1 
		-- click menu item "New Tab" of menu "File" 
		--open location "http://gmail.google.com/ 
		-- end tell 
	end tell
end tell

delay 2 --Allow name of new tab to register

repeat
	tell application "Safari" to set LoadStatus to name of window 1
	
	if LoadStatus does not contain "Loading" then exit repeat
end repeat

delay 1 --Wait a second after page is loaded for good measure


keystroke return

SC

It might be worth trying a couple of delays: the first, to make sure the URL is loaded - and the second, to allow sufficient time for the page elements and user name & password to load. While the former can be monitored and adjusted dynamically, the latter probably still needs to be fixed (at least for a general-purpose script).

From the few tests I’ve tried so far, this seems to work quite well:

tell application "Safari" to activate
repeat with u in {"http://webmail.west.cox.net/", "http://www.mac.com/WebObjects/Webmail.woa/wa/default?aff=consumer&cty=US·=en&identifier=NDUohWFuFt1LS8mb&src=wl", "http://gmail.google.com/"}
	tell application "System Events" to keystroke "t" using command down
	tell application "Safari" to tell front document
		set URL to u
		repeat while (count URL) is 0 (* wait for URL to load *)
			delay 1
		end repeat
		delay 3 (* wait for page elements and user info to load: modify as required *)
		if URL is u's contents then tell application "System Events" to keystroke return
	end tell
end repeat

Kai,

I like that script very compact and neat/easy to follow. I am having an issue with the last page loaded however. It seems that the last return is not being passed & the page remains at the login. Your post seems to indicate that you did not have that same issue…any ideas?? BTW I copy/pasted your script so not a typo on my part :smiley:

SC,
Thought I tried that method of ending the Tell completely and making the repeat it’s own event but I can’t seem to find it in my “versions” so I will have to give it an edit and see.

Thank you both for you help…I am almost there I can feel it. :cool:

My script is the “safe” route. It is long and clunky, but it allows each piece to go through. It was meant to be a framework, not the finished script. I did think about a script that used a repeat loop similar to kai’s, but using the “Loading” status method. I don’t think it will be much faster either way, but speed isn’t probably what your looking for, just effectiveness…
SC

SC,

Your are correct I would rather have one that works 100% of the time no matter how long it is or “ugly” it looks or fast it is…didn’t mean any offense. :slight_smile:

Chip

Once you get a script working its less frustrating to go back and make it more efficient when you have a working copy. I always appreciate Kai’s compact techniques- I’m learning too. It looks like the only thing I left out was

should be

tell application "System Events" to keystroke return

SC

I see what you mean, Chip. I tested the routine with some of my own login URLs, rather than yours - and they all worked as intended. However, the final location of some login pages (including the Google one) is not necessarily the same as that originally specified. The simplest fix for this would be to just change this line:

…to this:

That should make the routine pretty much as “safe” as any other. :slight_smile:

Your revised script must have appeared while I was typing my original message, sitcom - since I didn’t see it until after I’d posted. However, our scripts are remarkably similar in approach, in that they both specify a target location, wait for the page to load, add a further delay to allow page elements and user name/password to load - then finally hit the return key.

We used slightly different (but I think equally valid) methods to monitor page loading, and I threw in a repeat loop to tighten things up a bit - but that’s essentially it. Hopefully, either method (with their respective tweaks) should now work reasonably well for Chip. (Fingers crossed.) :wink: