(* This code takes all open windows in Safari, closes them, and reopens them in tabs in a single window. Safari does not have tab support enabled in their Applescript dictionary. As a workaround, the script iterates through tabs, closing them, and checking to see if the close tabs menu item is enabled. If it is not enabled, there are no more tabs in the window. Writen for Safari 2.0.4 Mac OS 10.4.8 20070204 *) if UIscript_check() is not false then tell application "Safari" try set docCount to the number of documents set URLlist to {} repeat with iDoc from 1 to docCount - 1 set noMoreTabs to false repeat until noMoreTabs is true my lastTab() set noMoreTabs to result set the end of URLlist to the URL of document 1 if noMoreTabs is false then my CloseTab() end repeat close window 1 end repeat repeat with i from the (count of URLlist) to 1 by -1 my new_tab() set the URL of document 1 to item i of URLlist 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 if ---Handlers---------------------------------------------------------------- on new_tab() 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 on CloseTab() tell application "System Events" tell process "Safari" click menu item "Close Tab" of menu "File" of menu bar item "File" of menu bar 1 end tell end tell end CloseTab on lastTab() local multipleTabs tell application "System Events" tell process "Safari" set frontmost to true set multipleTabs to enabled of menu item "Close Tab" of menu "File" of menu bar item "File" of menu bar 1 end tell end tell return not multipleTabs end lastTab ----Apple User Interface check---------------------------------------- on UIscript_check() -- get the system version set the hexData to system attribute "sysv" set hexString to {} repeat 4 times set hexString to ((hexData mod 16) as string) & hexString set hexData to hexData div 16 end repeat set the OS_version to the hexString as string if the OS_version is less than "1030" then display dialog "This script requires the installation of " & ¬ "Mac OS X 10.3 or higher." buttons {"Cancel"} ¬ default button 1 with icon 2 return false end if -- check to see if assistive devices is enabled tell application "System Events" set UI_enabled to UI elements enabled end tell if UI_enabled is false then tell application "System Preferences" activate set current pane to ¬ pane "com.apple.preference.universalaccess" set the dialog_message to "This script utilizes " & ¬ "the built-in Graphic User Interface Scripting " & ¬ "architecture of Mac OS X " & ¬ "which is currently disabled." & return & return & ¬ "You can activate GUI Scripting by selecting the " & ¬ "checkbox ?Enable access for assistive devices? " & ¬ "in the Universal Access preference pane." display dialog dialog_message buttons {"Cancel"} ¬ default button 1 with icon 1 end tell return false end if return true end UIscript_check