How do you write an apple script to open a link on a specific website?

Hi,
I was trying to get an apple script to open one of my links on my website. I have tried many things, but I just can’t figure it out. Could you maybe help me out? I was trying to have it open the link called Awesome Biking.
Thanks.


set site to "http://www.zandersite.co.nr"
repeat
	open location site
	
	delay 3
	open "Awesome Biking"
	delay 10
	tell application "Safari"
		quit
	end tell
end repeat

Hi zslavitz,

Here’s an example that opens this bbs:


set site_url to "http://macscripter.net/"
set link_text to "AppleScript | OS X"
tell application "Safari"
	activate
	open location site_url
	-- wait until page loaded
	if my page_loaded(20) is false then error numner - 128
	-- get number of links
	set num_links to (do JavaScript "document.links.length" in document 1)
	-- find the link
	set c to num_links - 1
	set this_link to ""
	set f to false
	repeat with i from 0 to c
		set this_link to do JavaScript "document.links[" & i & "].text" in document 1
		if this_link is link_text then
			set f to true
			exit repeat
		end if
	end repeat
	if f then -- link text found
		-- get the url and open the link url
		set link_url to do JavaScript "document.links[" & i & "].href" in document 1
		open location link_url -- opens in new page
	else
		display dialog "Link \"" & link_text & "\" not found."
	end if
end tell
--
on page_loaded(timeout_value)
	delay 2
	repeat with i from 1 to the timeout_value
		tell application "Safari"
			if (do JavaScript "document.readyState" in document 1) is "complete" then
				return true
			else if i is the timeout_value then
				return false
			else
				delay 1
			end if
		end tell
	end repeat
	return false
end page_loaded

I’m using an old version of Safari, so it doesn’t have GetURL.

gl,

Thank You. So what do I have to change for the newer version of Safari?

I did it, and it said the link was not found. I tried it on my website and said Awesome Biking was not found.

Hi zslavitz,

It worked for this site, but not on your site? I don’t think it works if the links are in frames. What’s your url?

gl,

I even tried it on the site you gave me and it didn’t work? My URL is zandersite.co.nr

Oh, shucks… just do this:


open location "http://www.zandersite.co.nr"

… or this:


tell application "Safari"
	activate
	
	try
		set _exists to document 1
		tell front document
			set URL to "http://www.zandersite.co.nr"
		end tell
	on error
		make new document
		tell front document
			set URL to "http://www.zandersite.co.nr"
		end tell
	end try
	
end tell

Have fun.

Peter B.


How does that open a link? It just opens my website?

It was simply an example…

Since your ‘Awesome’ page has a unique URL, you can do this:


open location "http://zslavitz.googlepages.com/biking"

Perhaps you can modify kel’s script to click on ‘Awesome’ instead, if so desired.

Peter B.


How would you make it so kels script would just open awesome. Would I have to change my link?

Hi zslavitz,

kel’s script can’t work: Javascript doesn’t find any links,
because you’re using frames or something else, which “hides” the links

Hi,

You can’t get the url from the link text “,Awesome Biking,” because it’s not on the page. “,Awesome Biking,” is in your html document for the frame. You would have to go to the html document for the frame. The google one. You would need to search every frame’s document.

I wonder why open location doesn’t work in the new versions of Safari anymore. It must be erroring or something. See Peter’s ‘set url’ of front document method instead of open location in the Safari tell block.

gl,

Hi kel,

of course open location works in the newest version of Safari :wink:

So I am still confused on what to do? Could you post the new apple script?

if you use the URL of the main frame : [i]http://zslavitz.googlepages.com/[/i]
and the value of the variable link_text “,Awesome Biking,”, then kel’s script works

set site_url to "http://zslavitz.googlepages.com/"
set link_text to ",Awesome Biking,"
tell application "Safari"
...