I would like to click a link on a webpage that is labeled “Listen!”
It opens a real player. I have been able to write a script to open a webpage according to the date which opens the correct realplayer link.
There is only one problem, There are sometimes reruns and the date doesn’t work in that case.
A foolproof way would be to click the link on the webpage, I guess,
http://www.wpr.org/book/lastweek.html however I have no idea how to approach this problem.
Here is my previous script.
set monthdate to month of (current date) as integer
if monthdate is less than 10 then
set monthdate to "0" & monthdate
else
set monthdate to monthdate as text
end if
set yeardate to year of (current date) as integer
set yeardate to yeardate as Unicode text
set yeardate to character 3 of yeardate & character 4 of yeardate
set daydate to day of (current date) as integer
if daydate is less than 10 then
set daydate to "0" & daydate
else
set daydate to daydate as text
end if
set datestring to yeardate & "" & monthdate & "" & daydate as Unicode text
tell application "Safari" to open location "http://broadcast.uwex.edu:8080/ramgen/wpr/bok/bok" & datestring & "a.rm" as Unicode text
Hi,
this searches the site for the string in the property link_text
and displays a dialog for choosing and opening one of the both links.
It uses Safari’s javascript capabilities.
property site_url : "http://www.wpr.org/book/lastweek.html"
property link_text : "Listen!"
tell application "Safari"
activate
open location site_url
-- wait until page loaded
if my page_loaded(20) is false then return
-- get number of links
set found_links to my find_link(link_text)
if found_links is false then -- link text not found
display dialog "Link \"" & link_text & "\" not found."
else
set L to button returned of (display dialog "open links" buttons {"Cancel", "Link #1", "Link #2"})
if L is "Link #1" then
open location item 1 of found_links
else
open location item 2 of found_links
end if
end if
end tell
on find_link(link)
tell application "Safari" to 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 {}
repeat with i from 0 to c
tell application "Safari" to set this_link to do JavaScript "document.links[" & i & "].text" in document 1
if this_link is link then
tell application "Safari" to set end of f to do JavaScript "document.links[" & i & "].href" in document 1
end if
end repeat
if f is {} then set f to false
return f
end find_link
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
Thank you. The script is awesome!