Button click action in Safari webpage.


I’ve read the article at: http://macscripter.net/viewtopic.php?id=15651 but it does not seem to work for what I want to do.

I want an applescript capable of clicking successfully in Safari on the “Play” button at this webpage: http://republicbroadcasting.org/shoutcast/shoutcast.html

I don’t know javascript and I barely know applescript but I’m technical enough to figure out what to do with a little help.

My ultimate goal behind this is to be able to schedule my Audio Hijack Pro to launch a script capable of starting Safari at the precise webpage and simulate a click action on the “Play” button (on that webpage) so I can record a radio show unattended. The Global Research News Hour is a great show! I urge you guys to listen to it!

Here’s what I have for the moment:

tell application “Safari”
open location “http://republicbroadcasting.org/shoutcast/shoutcast.html
end tell

tell application “System Events” to tell process “Safari”
click button “Play” of sheet 1 of window 1 – I know this does not work but this is what needs to be fixed.
end tell

Any help appreciated.

Thanks in advance.

AppleScript: 2.0.1
Browser: Safari 525.27.1
Operating System: Mac OS X (10.5)

I am glad you posted the link: http://macscripter.net/viewtopic.php?id=15651
Kai is so good at giving all possible information regarding UI scripting. I had never attempted to do before what you are trying to do but i could still successfully do it within 5 minutes.
The following script clicks the “Click to give” button on www.thehungersite.com

tell application "Safari"
	tell application "System Events"
		tell process "Safari" to click button 1 of group 14 of UI element 1 of scroll area 1 of group 4 of window 1
	end tell
end tell

Then i attempted to write a script for you. The problem in your case is that nothing shows up for the “play” button in prefab UI browser.

This script works fine In safari 3.2.1 (4525.27.1) osx 10.4
Don’t know about osx10.5

tell application "Safari"
	open location "http://republicbroadcasting.org/shoutcast/shoutcast.html"
end tell

tell application "System Events" to tell process "Safari"
	set frontmost to true
	keystroke tab
	repeat 3 times
		shift key down
		keystroke tab
		shift key up
	end repeat
	keystroke return
end tell

To click a normal (None flash button) I just tried this and it works.

Using the www.thehungersite.com site

tell application "Safari"
	--Click form button named=j_id78:clickToGiveButton
	do JavaScript "document.all(\"j_id78:clickToGiveButton\").click()" in document 1
end tell

To ptremblay: as you see, the play button is in flash and so the method you are trying will not work here.
To mira: i wonder, how you could click something inside a flash object in 10.4
To mark hunte: could you please tell me how to write the script that you wrote? I mean, it must be something about the button id, how do you find the button id?

There might be an option which doesn’t require using Safari or resorting to GUI scripting:

tell application "iTunes"
	open location ";stream.nsv"
end tell

Note that the stream’s URL is revealed in Safari’s activity window once the Play button on the RBN Live ONLINE page is pressed. Good luck.

I used the Inspector built into Safari. (can not remember if you need to enable the developer menu first)
But control click on the button, and select ‘Inspect Element’,

A inspector window should pop up with the HTML for the element highlighted. (you may need to scroll up or down, sometimes
it does not jump to the right place, but it will have it in highlight, so make sure you use the scroll bar and not not click on the window or you may un-highlight)

This is the example of what gets highlighted: Not the input id or the name one of them should work

<input id="j_id78:clickToGiveButton" name="j_id78:clickToGiveButton" type="image" src="http://cdn.thehungersite.com/charityusa_vitalstream_com/ctg/p3/images/interface/buttons/button-home-ths-right-2.gif?cb=1235080800000" onclick="mCC();;" alt="Click Here Daily To Click Here to Give - it's FREE. It's Free!" class="clickToGiveButton">

Thanks Mark. It will be very useful to me. Can you also tell me how to set delay in cases where java is used.
For example, look at delay set in this case, so as to make sure that the button is available to be clicked:

tell application "Safari" to open location "http://www.apple.com/"
tell application "System Events" to tell UI element "Job Opportunities" of group 10 of ¬
   UI element 1 of scroll area 1 of group -1 of window 1 of application process "Safari"
   repeat until exists
       delay 0.2
   end repeat
end tell


I would like to thank everyone on the thread! It helped me a lot. And frankly, it seems that everybody picked a little bit from everyone who contributed.


i have to complete forms manually and i would like to do this automatically.
but i don’t find how to click on the following radio button :
html-code :

i have some buttons like this in the web-page.

i think there should be something like this :

tell application “Safari”
end tell
tell application “System Events”
tell process “Safari”
click radio button (something in here, but what ?)
end tell
end tell

Hello and welcome.

“Something in there” could be a do javascript"getElmentById(ID)", followed by yourElm.SetFocus() or something clicked to true, or a call to system events after having found the value and id of the radiobutton by UI Element Inspector.

You may maybe go away from Safari and UI scripting all together, and maybe have curl click the page for you. :wink:

Seek and you shall find…

Thanks for your ideas.

But I found another solution with cliclick : http://www.bluem.net/en/mac/cliclick/

so first I place the window with apple script at a fixed place and then do mouse-click with cliclick.

Browser: Firefox 17.0
Operating System: Mac OS X (10.7)

It is kind of hard to do it any other way, as long as the page is a flash player page.

It would be interesting to have a copy of your script it can be intersting to look at. :slight_smile:

I hit cmd-shift-4 leveraging on the screen capture to show me screen coordinates, for the mouse pointer, when I am done, I hit ESC to avoid taking the picture.

here is my script :

tell application “Safari”
set bounds of window 1 to {1, 1, 1200, 1000}
end tell
do shell script “users/myusername/desktop/cliclick/cliclick c:308,521”

You can put Cliclick where ever you want

to find coordinates of a button, look with UIElementInspector (http://developer.apple.com/library/mac/samplecode/UIElementInspector/UIElementInspector.zip), this gives you the coordinates. I had to add 8 pixel to x and 8 to y (UIElementInspector gives the upper left corner of the element)

it works fine

That is nice.

Sometimes the very last resort, when everything else is tested. Regulus6633/HamSoftEngineering has also made a utility like clickclick, and I also think that AsObjC runner is capable of the same.

Sometimes the wood aren’t seen for the trees. :slight_smile:

This solution gets my vote for the following three reasons.

You don’t move the return key in the process.

Nothing else need to be installed.

You can easily restore the focus to the element that previously had it, should you wish it.