Using AppleScript to record the Safari browser windows and tabs

Sometimes I'd like to be able to save a list of what web pages are open in the Safari browser's tabs. Using Apple's AppleScript scripting language it is possible to record that information to a text file. The following script will create a text file that lists each Safari browser window that is open and for each tab within a window, the title for the webpage and the URL. The script will prompt for the location and name for the file where you wish to store that information (example output file).

tell application "Safari"
	
	set myFile to open for access (choose file name) with write permission
	set windowNumber to 1
	repeat the number of windows times
		set myTabs to every tab of window windowNumber
		write "----- Window Number " & windowNumber & " -----

" to myFile
		set tabNumber to 0
		repeat with aTab in myTabs
			
			set tabTitle to name of aTab & "
"
			write tabTitle to myFile
			set tabURL to URL of aTab & "

"
			write tabURL to myFile
			set tabNumber to tabNumber + 1
			
		end repeat
		
		write "Window Number: " & windowNumber & " Number of tabs: " & tabNumber & "

" to myFile
		set windowNumber to windowNumber + 1
	end repeat
	close access myFile
	
end tell

The script, safari_tabs.scpt can be run using the Script Editor application found in /Applications/Utilities. Within the Script Editor click on File then Open and select the script file.

Script Editor - safari_tabs.scpt

Then click on the the "run the script" icon, which is a rightward pointing arrow head. Or you can run the script from a command line interface, i.e., a Terminal window using osascript chrome_tabs.scpt.
$ osascript safari_tabs.scpt
$

The above script only works for Safari. For one that works with the Google Chrome browser, see Using AppleScript to record the Chrome browser windows and tabs. Firefox does not have a tab item in its "dictionary." Applications have a "dictionary" that can be consulted to determine what properties can be queried or altered. If you have the applications installed on an OS X system, you can see the dictionaries for Chrome, Firefox, and Safari, by clicking on File then selecting Open Dictionary in the Script Editor utility. Scroll through the list until you find the relevant dictionary. The properties for the tab item in Safari's dictionary is shown below:

Safari dictionary - tab

 

TechRabbit ad 300x250 newegg.com

Justdeals Daily Electronics Deals1x1 px