PDA

View Full Version : Very simple programming to browse the web




Frown
Jul 18, 2009, 02:46 PM
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.



wafl iron
Jul 18, 2009, 03:19 PM
I dont know if this works, but its probably on the right track or at least it can help you get somewhere


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

Frown
Jul 18, 2009, 05:42 PM
I dont know if this works, but its probably on the right track or at least it can help you get somewhere


Sorry, it won't let me compile it. I think your method should work though. I mean, it all makes sense.

craig1410
Jul 19, 2009, 07:27 AM
Sorry, it won't let me compile it. I think your method should work though. I mean, it all makes sense.

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.

Frown
Jul 19, 2009, 09:48 AM
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.

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!

craig1410
Jul 19, 2009, 10:17 AM
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!

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.

Frown
Jul 19, 2009, 02:32 PM
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.

That would be great.

craig1410
Jul 19, 2009, 05:05 PM
That would be great.

Okay, try this:

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

Frown
Jul 19, 2009, 07:37 PM
Everything is working perfectly. Thanks, Craig!