Using AppleScript to record the Chrome browser windows and tabs

I often have a number of browser windows open with many tabs open in the windows and wanted a way of producing a list of the open windows and the tabs within each with the title and URL for each tab. I had a simple AppleScript script that will display Firefox windows titles, but that just lists the active tab in each window whereas I wanted a list of every tab's title and URL, so I created a new script for Chrome that will create a text file containing that information. That script, chrome_tabs.scpt appears below:

tell application "Google Chrome"
	
	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 title 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 set myFile to open for access (choose file name) with write permission line prompts the user to select a location and file name for the output file.

Choose file name

If the file you choose already exists, you will be prompted as to whether you wish to replace it.

The script then writes a line "----- Window Number" followed by the window number to the chosen file for window number 1. Then for each tab in that window it writes a line that contains the title number for the window followed by a line containing the URL for that window with two newlines at the end to put a blank line between the title and URL for each tab. In the script I used set tabURL to URL of aTab & "/n/n" to append the two newlines, but when you run a script, the AppleScript editor, which can be found in /Applications/Utilities where you will see Script Editor.app, will leave the opening double quote on the line where you placed it and then put a line break in, moving the ending double quote to a line below.

After writing the information for each tab in the window to the chosen file, the script will write a line that records the window number, e.g., 1, 2., 3, etc. followed by "Number of tabs: " and then the number of tabs that were open in that window. The chrome_tabs.txt file is an example of what the data in the output file will look like.

To run the script, you can open the Script Editor application found in /Applications/Utilities and click on File and then Open and then select the script file chrome_tabs.scpt.

chrome_tabs.scpt window

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 chrome_tabs.scpt
$

The above script only works for Chrome; simply substituting "Safari" or "Firefox" for "Google Chrome" in the tell application line won't work. Though, if you also change the "set tabTitle to title of aTab & " by substituting "to name", instead of "to title", i.e., "set tabTitle to name of aTab & ", it will work for Safari. Firefox doesn't have a tab property in its "dictionary", whereas both Chrome and Safari do. However, where Chrome uses "title" for the title of the webpage displayed in a tab, Safari uses "name". I've posted further information on the script that can be used for Safari at Using AppleScript to record the Safari browser windows and tabs.

If you wish to see what items can be queried and set for Google Chrome using AppleScript scripts, you can open the dictionary for the application within the Script Editor by clicking on File then selecting Open Dictionary. Scroll through the list of available dictionaries until you see "Google Chrome.app", then double-click on it to open it. Then entry for tab in the application's dictionary is shown below.

Chrome dictionary tab entry

References:

  1. Displaying Firefox Windows Titles With AppleScript
    Date: January 12, 2016
    MoonPoint Support

 

TechRabbit ad 300x250 newegg.com

Justdeals Daily Electronics Deals1x1 px