Problems with Safari, tabs, radio buttons, etc.

My goal is (as you probably guessed) to have a script that unite all open windows/tabs in the frontmost window. I know at least of two theoretical ways to do this, but neither works for reasons I don't understand:

1. GUI scripting. Use Safari's menus to navigate through tabs. For some reason this works somewhat randomly, sometimes getting all tabs and sometimes one tab per window. I can get it working in an awkward way that removes all windows before opening a new one: not exactly what I want to do.

2. I have found this one browsing for solutions on the web, and it uses the System Events app. It seems that Safari tabs are "radio buttons". However, the number of radio buttons returned is always 0. Also, reading the URL with the line

value of text field 1 of group 1 of splitter group 1 of window 1

does not work (returns NSReceiverEvaluationScriptError: 4).

So, does anyone have any ideas on what goes wrong? (or even better, does anyone have a script that works?)

Mac mini Core Duo Mac OS X (10.4.7)

Posted on Nov 8, 2006 12:44 PM

Reply
6 replies

Nov 8, 2006 5:11 PM in response to Neithan

To get the url of the website that safari is currently showing use

tell application "Safari" to set Locurl to URL of front document

If you want to cycle through safari tabs then i think you have to use this as far as i can see safari doesnt have applescript support for tabs

tell application "System Events"
tell application "Safari" to activate
key code 30 using {command down, shift down}
end tell


Not totally sure what you are doing but if you want to cycle through all your tabs and get the URLs out of them then something like this will do it

set tablist to {}
set exitflag to false
repeat until exitflag is true

tell application "System Events"
tell application "Safari" to activate
key code 30 using {command down, shift down}
tell application "Safari" to set Locurl to URL of front document
if Locurl is in tablist then
set exitflag to true
else
copy Locurl to end of tablist
end if
end tell
end repeat

tablist

Hope this helps

Nov 9, 2006 9:41 AM in response to Richard Moore

That's what I had in mind with GUI scripting. I've just got one script to work (without closing all windows), but there are still unavoidable disavantadges to this method. For example if you have two tabs at the same URL it will stop the process of going through tabs before it should. And it is not what we can call "good programming" anyway.

Well, I can live with that solution, but it should be possible to manage the tabs with System Events. If anyone has ideas for this, that would be great!

Nov 9, 2006 1:13 PM in response to Neithan

Safari tabs are buttons, not radio buttons. Safari window contains multiple groups Tab bar group number change depend on whether you have Address Bar, Bookmarks Bar etc...

Try this:

tell application "System Events"
tell process "Safari"
set tab_group to 0
repeat with i from 1 to count of groups of window 1
if buttons of group i of window 1 is not {} and title of button 1 of group i of window 1 is not "" then -- Tab bar's first button has title
set tab_group to i
exit repeat
end if
end repeat
set tab_list to {}
repeat with i from 1 to count of buttons of group tab_group of window 1
click button i of group tab_group of window 1
tell application "Safari" to set _url to URL of document 1
if tab_list does not contain _url then copy _url to end of tab_list
end repeat
end tell
end tell

tab_list

Nov 10, 2006 11:48 AM in response to Cyclosaurus

Thanks Cyclosaurus, this seems to work!

There is still one problem: when getting the URL of a window, if it is empty it will throw an "undefined variable" error. How can I avoid this? (I cannot even check if the url is empty because it is undefined.)

Here is the script I have so far, if anyone is interested:

tell application "System Events"
tell process "Safari"
try
-- finding the group
set tab_group to 0
repeat with g from 1 to count of groups of window 1
if buttons of group g of window 1 is not {} and title of button 1 of group g of window 1 is not "" then
set tab_group to g
exit repeat
end if
end repeat

set URL_list to {}
set N to count of windows
repeat with w from N to 2 by -1 -- going through windows
set T to count of buttons of group tab_group of window w
repeat with b from 1 to count of buttons of group tab_group of window w -- going through tabs
click button b of group tab_group of window w
tell application "Safari" to set this_URL to URL of document w
if URL_list does not contain this_URL then copy this_URL to end of URL_list
end repeat
tell application "Safari" to close window w
end repeat
set C to count of URL_list
repeat with i from 1 to C
my new_tab()
tell application "Safari" to set URL of document 1 to item i of URL_list
end repeat
on error the error_message number the error_number
display dialog the error_message buttons {"OK"} default button 1
end try
end tell
end tell

on new_tab()
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

Mac mini Core Duo Mac OS X (10.4.8)

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Problems with Safari, tabs, radio buttons, etc.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.