AppleScript: Closing Specific Browser Tabs

25th February 2020 | Programming

As I've admitted in the past, I have a bad habit of having a large number of tabs open in my web browser. Bad enough that I've written scripts to count how many tabs are open in either Safari or Chrome.

While these scripts are useful to let me know how egregious my tab opening habits have been recently, they do not help in curating the massive number of tabs to a more manageable amount. So I wrote another script to help keep the tab population under control.

At my job at Acme Co. (the fine purveyor of generic products used everywhere and by everyone, including wily coyotes), we make use of Atlassian products such as JIRA and Confluence for tracking tickets and documentation. I often find that I have far too many tabs open, and after my session has expired, I am forced to relogin to access these pages anyway, so I might as well close them. The following script CloseAcmeTabs.scpt is the example I use to iterate through all of Chrome's open tabs, find any open JIRA or Confluence tabs, and then close these tabs. To make use of this script for your own uses, change acme.com to the appropriate domain name.

To simplify running this script, I added an alias into my .bash_profile file:

alias closetabs='osascript ~/Projects/Scripts/CloseAcmeTabs.scpt'

Once the file has been updated, refresh the bash shell environment: source ~/.bash_profile

Now to run the script, type closetabs in the Terminal, and the AppleScript will start, close any JIRA and Confluence web pages, and then print out the total of tabs which were closed.

While this does not entirely curtail my habit of opening hundreds of tabs, it does help automate at least one process by removing the tabs I know will have expired by the next day, so this script does help by doing some regular browser maintenance when I call for it.