Below is my code for getting music links from a web page in Safari, I used Apple’s example script (get thumbnail images) to help me.
It works, but I believe there must be a cleaner way to do it.
--couldn't get this list to work
--property extension_list : {".mp3", ".wma", ".aac"}
tell application "Safari"
activate
try
-- the number of links in the front document
set the link_count to (do JavaScript "document.links.length" in document 1)
if the link_count is "0" then error "No links in this document."
-- get the url of the directory containing the current page
set the document_URL to (do JavaScript "document.URL" in document 1)
set AppleScript's text item delimiters to "/"
set the parent_URL to ((text items 1 thru -2 of the document_URL) as string) & "/"
set AppleScript's text item delimiters to ""
set the list_of_links to {}
repeat with i from 0 to (the link_count - 1)
-- get the text of the anchor link
set this_text to (do JavaScript "document.links[" & (i as string) & "].text" in document 1)
-- if the text of the anchor link is an image, add the link to the list
set this_url to (do JavaScript "document.links[" & (i as string) & "].href" in document 1)
-- would be better to use some kondo of property list here
if (the this_url ends with ".mp3") or (the this_url ends with ".wma") or (the this_url ends with ".aac") then
set this_host to (do JavaScript "document.links[" & (i as string) & "].host" in document 1)
if this_host is "" then
set this_url to the parent_URL & this_url
end if
set the end of the list_of_links to this_url & return
end if
end repeat
if the list_of_links is {} then
error "This page contains no valid links."
end if
-- make a new doc in textedit with the links
tell application "TextEdit"
set the_urls to list_of_links as string
make new document at beginning with properties {text:the_urls}
activate
end tell
on error the error_message number the error_number
if the error_number is not -128 then
display dialog the error_message buttons {"OK"} default button 1
end if
end try
end tell