1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Very simple programming to browse the web

Discussion in 'Mac Programming' started by Frown, Jul 18, 2009.

  1. macrumors newbie

    #1
    I need to load a page in Safari in separate tabs/windows every 30 minutes in order to gather statistics, and I was wondering if anybody can help me out with a script for AppleScript to do this?

    Also, if this is in the wrong section, I apologize. I am dealing very basic programming.
     
  2. macrumors regular

    #2
    I dont know if this works, but its probably on the right track or at least it can help you get somewhere

    Code:
    open_safari_window("http://www.macrumors.com")
    
    on open_safari_window(my_url)
    	tell application "Safari"
    		repeat with i from 1 to 10
    			# wake up safari
                            activate
    			
    			# make a new window
    			make new document at end of documents
    			
                            # set the URL of this new window
    			tell document i
    				set URL to my_url
    			end tell
    
                            # sleep 1800 seconds (30 minutes)
    			delay 1800
    		end repeat
    	end tell
    end open_safari_window
     
  3. macrumors newbie

    #3
    Sorry, it won't let me compile it. I think your method should work though. I mean, it all makes sense.
     
  4. macrumors 6502a

    #4
    It compiles and runs okay for me although it creates a new Safari window each time rather than a new tab.

    Are you running Leopard (OS X 10.5.*) or are on an older version of OS X?

    Regards,
    Craig.
     
  5. macrumors newbie

    #5
    I'm running Tiger on a 1.83 GHz MacBook. Could that be the problem?

    EDIT: I used it on my other MacBook, and it worked fine. Thank you very much, guys!
     
  6. macrumors 6502a

    #6
    Hi,
    Glad you got it working. Yes there are differences between Tiger and Leopard but I don't know what they are to be honest. I've come across issues of scripts working in Leopard but not in Tiger which is why I asked. I'm a beginner in Applescript myself but I often find that helping other people with a specific problem helps me to learn better than just sitting down doing artificial tasks all of my own.

    I managed to get the above script working by creating new tabs rather than windows but I forgot to save it... Let me know if you want it to work with tabs rather than windows and I'll get it working again.

    Cheers,
    Craig.
     
  7. macrumors newbie

    #7
    That would be great.
     
  8. macrumors 6502a

    #8
    Okay, try this:

    Code:
    open_safari_window("http://www.macrumors.com")
    
    on open_safari_window(my_url)
    	tell application "Safari"
    		activate
    		set newDoc to (make new document with properties {URL:my_url})
    		
    		repeat with i from 2 to 10
    			# sleep 1800 seconds (30 minutes)
    			delay 1800
    			tell window 1 to set current tab to (make new tab with properties {URL:my_url})
    		end repeat
    	end tell
    end open_safari_window
    Note that "Window 1" seems to represent the current window in focus so if you were to set the script running and then open another Safari window you will find that the new tabs start appearing in that instance of Safari rather than the original instance. I'm not familiar enough with Applescript to figure out how to prevent this. Maybe another user will be able to suggest something.

    Anyway, I hope this helps.
    Craig.
    :)
     
  9. macrumors newbie

    #9
    Everything is working perfectly. Thanks, Craig!
     

Share This Page