PDA

View Full Version : AS to click all selected links in Safari


minton
04-09-2005, 07:39 PM
I would like to use applescript to click on all selected links in Safari. It probably would use "tell application system events" to find the links in the selection area, and just click on each one in succession. The links are all to audio files, so they would be cued in iTunes and wouldn't need to be opened in a new window or tab. This would be used at sites like amazon to listen to all audio samples on the page without needing to go back and click on each one.

cudaboy_71
04-09-2005, 10:30 PM
there are so many links on any given page. i just went to amazon and found over 130 links on a page that has the album song samples you speak of. how do you propose you'd limit it to just the sample clips?

wouldnt it just be easier to listen to the clips in the iTMS? or does it not have all the songs you're looking for?

mark hunte
04-09-2005, 11:11 PM
Me and a friend had played with some ideas like this a while back

to find the link number an address, in the front Safari page. try something like this

tell application "Safari"
set counter to 0
-- the number of links in the front document
set the_link_number to do JavaScript "document.links.length" in document 1
repeat the_link_number times
-- getting the target URL of the first link of the front document
set counter to counter + 1
do JavaScript "document.links[" & counter & "].href" in document 1

end repeat
end tell

make sure you looking at the event log in AS to see what its doing
It list the link number and its address

to go to a link number

tell application "Safari"

open location "http://www.nasa.gov/multimedia/imagegallery/"
delay 4 --
set thelink to do JavaScript "document.links[25].href" in document 1
delay 4
set the URL of document 1 to thelink
delay 4
set thelink to do JavaScript "document.links[22].href" in document 1



--my new_tab()
set the URL of document 1 to thelink
end tell
(*
on new_tab() --found this part on an osxhints search http://www.macosxhints.com/article....030414185226343
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
click menu item "New Tab" of menu "File" of menu bar 1
end tell
end tell
end new_tab*)

You get the idea, but you would need to combine the scripts and and use the link name with some sort of 'if link contains blahblah then'

you can find better explanation of how some of this Applescript Javascript works from the apple site.
http://www.apple.com/applescript/safari/jscript.01.html

This is not a solution in its self but may help point you to how to start the script

minton
04-10-2005, 01:58 AM
Thanks Mark for the code, but my needs are for something that clicks on audio links without opening a new tab. The temp audio files get sent to itunes. Also, I'm looking to work not with all the links in the safari window but just the links I have selected with the mouse (cudaboy, to answer your question as well). I've attached a picture to give you a better idea of what I mean. That's why I was thinking System Events, something like 'click on urls of selection'. They can be in rapid succesion without pause as I would have the computer muted until I loaded up all the urls I wanted to listen to, then do a sort by date added in itunes, turn the volume back up and listen to all the music I had cued. I have found an applescript that will play all tracks (http://www.dougscripts.com/itunes/scripts/ss.php?sp=itunesmsp) listed in the itms window, but I'd still like to find something for safari as there are other music sites with lots of links that I like to visit.

mark hunte
04-10-2005, 06:10 AM
Thanks Mark for the code, but my needs are for something that clicks on audio links without opening a new tab.
Did you run the scripts, The one that jumps through the links does not open in Tabs.

There is a tab sub script in it, but that is commented out, so the script ignores it.

The point of showing you these is:

to show you one way of getting info from the page and the links on it.
and then showing how to open (click a link).

AFAIK , There is no way of you to get 'system events' to 'click' an url

'open location' is what should be able to do what you want.
When you click a link in a browser, what you are doing is telling the browser to open location.

From your image you posted.

It shows that you have selected a list of music links.
What you have done, is actually selected some text on the page, not the links.

If you get the AS to:
first look at the page and gather all info on the page where links are.
Filter the links by using the text you have selected
And then open the filtered links.

But the problem is that the links them selves may not contain the text you selected, so then you would have to work on proximity to your selected text

But webpages are all different, so how do you do that for just any webpage

If there is not an app out there already to do what you want.
this could end up a lot of work to figure out, and you also would more than likely have to use some shell script.

Good luck

minton
04-10-2005, 11:35 PM
Ok, found what I was looking for. Another reason to start using Firefox: the Download Them All (https://addons.update.mozilla.org/extensions/moreinfo.php?id=201&vid=845) and Linky (http://gemal.dk/mozilla/linky.html) extensions allow opening a selection of links all at once.