Count of Tabs in Safari 2.0

I tried using the “radio buttons” call to get the count of tabs in Safari but it just errors out.
All i want to do is cycle through the open tabs at 5 second intervals, but I would like it to stop when it gets to the last tab.

There is no reference to tabs in the Safari dictionary, or on Apple’s Website.

Anyone have any ideas?

This might work for you if you can stand the flashing through the tabs:

set theTabs to {}
tell application "Safari"
	activate
	repeat
		set thisTab to URL of document 1 of window 1
		if thisTab is not in theTabs then
			set end of theTabs to thisTab
			tell application "System Events" to keystroke "]" using {command down, shift down}
		else
			exit repeat
		end if
	end repeat
end tell
{Count of theTabs, theTabs}

Something like this might do the trick, ddoscher:

to countSafariTabs()
	tell application "System Events" to tell (group 1 of window 1 of process "Safari" whose class of UI elements ¬
		is class of buttons and "" is not in title of buttons) to if exists then return count buttons
	0
end countSafariTabs

set tabCount to countSafariTabs()
-- do something with tabCount...

Edit 1: script modified to fix bug (details below)
Edit 2: incorporated in handler

Sheesh, Kai;

When I run your GUI script with 3 tabs open, it returns 17. As it happens, that’s how many bookmark bar items I have. Do you have your bookmark bar?

Yeah, I tested with and without the address bar, bookmarks bar and status bar - in every possible combination - and it worked every time. I just went through the entire routine again, with the same results.

However, your experience indicated some other difference between our configurations that had clearly been overlooked - and I just figured it out. My bookmarks bar contains a few more bookmarks than can be accommodated on-screen, so a menu button is displayed to allow access to the off-screen items. That button, tiny as it is, fooled me into believing the structure of the bars differed consistently. Of course, they don’t - at least, not in the way I had assumed.

(That’s what I just love about UI scripting - the sheer uncertainty of it all.) :wink:

I appreciate the feedback, Adam. In this sort of situation, it’s not easy to cover every possible scenario - so the more input, the better.

Anyway, I’ve modified the above script to incorporate (what I believe to be) a fix. Do let me know if you find any other bugs that need attention.
:slight_smile:

Which, in a nutshell, summarizes my reticence to try anything more complex than keystroke or key code commands. You’re braver than I am.

Having said that your fix works perfectly on my machine, my Safari.