Apple Script: Opening a URL after page has fully loaded

Discussion in 'Mac Programming' started by omgzsrsly, Aug 4, 2008.

  1. omgzsrsly macrumors newbie

    Joined:
    Aug 4, 2008
    #1
    Hey everyone, I'm new to this forum, and have been working on an apple script that simply opens a couple URLs to document 1 of safari. Well right now I am using delay, to ensure that all these pages fully load before jumping to the next, but this changes depending on how fast my internet connection is, and it would be much more convenient to have some way of saying for example:

    if document 1 has fully loaded then

    set URL of document 1 to "http://example.com"

    end if

    but i have no idea of how to do this. I have tried getting the source as a way of telling if the page has fully loaded but can not get it to work with an if statement. so... any ideas?

    here's the script:

    Code:
    tell application "Safari"
    	activate
    	repeat
    		set URL of document 1 to "http://example.com"
    		delay 4
    		set URL of document 1 to "http://example2.com"
    		delay 4
    		set URL of document 1 to "http://example3.com"
    		delay 4
    		set URL of document 1 to "hhttp://example4.com"
    		delay 4
    		set URL of document 1 to "http://example5.com"
    		delay 4
    		set URL of document 1 to "http://example6.com"
    		delay 5
    		tell application "Safari"
    			activate
    			tell application "System Events" to key code 48
    			tell application "System Events" to key code 36
    			delay 5
    		end tell
    	end repeat
    end tell
    
     
  2. omgzsrsly thread starter macrumors newbie

    Joined:
    Aug 4, 2008
    #2
    update

    I got the view source to work, but It actually gets the SOURCE... :| which causes a apple script error. :\ still looking for a solution.
     
  3. tedsmith3rd macrumors member

    Joined:
    Aug 30, 2006
    Location:
    US
    #3
    Try something like this:

    Code:
    set myURLs to {"http://www.example.com/1", "http://www.example.com/2", "http://www.example.com/3", "http://www.example.com/4", "http://www.example.com/5", "http://www.example.com/6"}
    
    tell application "Safari"
        activate
        make new document
        
        -- This will repeat with every url in the list above
        repeat with thisURL in myURLs
            set URL of document 1 to thisURL
            repeat
                try
                    if ((URL of document 1 is not "") and (do JavaScript "document.readyState" in document 1) is "complete") then
                        exit repeat
                    end if
                end try
                -- If we're not ready yet we delay a quarter second
                delay 0.25
            end repeat
            -- At this point the page is completely loaded. Do we need to do anything here?
        end repeat
        
        -- This happens once after all the pages have been loaded.
        tell application "System Events"
            key code 48 -- tab
            key code 36 -- return
        end tell
        
    end tell
    
     

Share This Page