Saving and restoring tabs in Safari

Sep 16, '03 09:55:00AM

Contributed by: mazatty

So, one day I needed to quit Safari for some reason. The problem was that I had a window with something like 10 tabs open and I needed those pages still. I really find it a pain that I have to bookmark each tab separately, otherwise I would just bookmark all the tabs at once and load them back up when I restarted Safari, but Safari doesn't do this.

I did a little searching and found some scripts on John Gruber's site that saved and restored windows in Safari. This looked good, but it didn't save/restore tabs in those windows. So starting with John's scripts, I added support for tabs to the save script. After learning about GUI Scripting I came up with the following -- read the rest of the hint for the scripts.

The script to save the open windows...


set url_list to {}

tell application "System Events"
  tell process "Safari"
        
    -- count the windows
    set winCount to count of windows
        
    -- loop through the windows
    repeat with i from 1 to winCount
            
      -- count the tabs
      set tabCount to count of radio buttons of window i
            
      -- loop through the tabs
      repeat with j from 1 to tabCount
        -- activate the tab 
        click radio button j of window i
                
        -- url of window
        set theUrl to value of text field 1 of group 1 
        of splitter group 1 of window i
                
        -- save the url
        set url_list to url_list & theUrl
      end repeat
            
      -- empty line to seprate windows
      set url_list to url_list & ""
    end repeat
  end tell
end tell

-- convert url list  to text
set old_delim to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set url_list to url_list as text
set AppleScript's text item delimiters to old_delim

-- get path to prefs file where URLs will be stored
set prefs_folder to path to preferences folder as string
set prefs_file to prefs_folder & "Safari Saved URLs"

try
  set open_file to 
  open for access file prefs_file with write permission
  -- erase current contents of file:
  set eof of open_file to 0
  write url_list to open_file starting at eof
  close access open_file
  on error
  try
    close access file prefs_file
  end try
end try

-- let the user know we are done.
tell application "Safari"
  activate
  display dialog "All Done" buttons {"OK"} default button 1
end tell

Now that i have the tabs saved to a text file in my Preferences folder, I need to restore them. To do this, I needed to be able to open new tabs. MacOSXHints to the rescue. I found a hint that showed how to do just that. So, again starting with John's restore script and the hint on opening tabs, I came up with the following.


tell application "Safari" to activate

-- get path to prefs file where URLs are stored
set prefs_folder to path to preferences folder as string
set prefs_file to prefs_folder & "Safari Saved URLs"

try
  -- read the saved urls
  set open_file to 
  open for access file prefs_file without write permission
  set url_list to read open_file using delimiter return
  close access open_file
    
  set tmpList to {}
  set newUrl_list to {}
    
  -- make urls from file into a list of lists
  -- each window is a seperate list in the big list
  repeat with aUrl in url_list
    if aUrl & "x" is "x" then
      set newUrl_list to newUrl_list & {tmpList}
      set tmpList to {}
    else
      set tmpList to tmpList & aUrl
    end if
  end repeat
    
  -- don't forget the last list, or maybe the only
  set newUrl_list to newUrl_list & {tmpList} 
    
  tell application "Safari"
        
    -- loop through the list of windows
    repeat with urls in newUrl_list
            
      my new_window()
      set emptyWindow to true
            
      -- loop through the list of tabs
      repeat with aUrl in urls
        if emptyWindow then
          set emptyWindow to false
        else
          my new_tab()
        end if
                
        -- open the url
        set URL of document 1 to aUrl
                
      end repeat
            
    end repeat
  end tell
  on error
  try
    close access file prefs_file
  end try
end try

-- let the user know we are done.
tell application "Safari"
  activate
  display dialog "All Done" buttons {"OK"} default button 1
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

on new_window()
  tell application "Safari" to activate
  tell application "System Events"
    tell process "Safari"
      click menu item "New Window" of menu "File" of menu bar 1
    end tell
  end tell
end new_window

For this all to work you need to have GUI Scripting, from Apple, installed. Also, the "Enable access for assistive devices" check box must be checked in the Universal Access preference pane.

Save these scripts to your script folder so you can access them from your Script Menu, and you're all set.

Comments (52)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20030913153245341