Alright, alright, I'll admit: I'm that type that literally leaves hundreds of tabs open.
I use this extension called Tab Suspender for Safari which I highly recommend. Basically, it allows you to suspend tabs with just the click of a button, significantly saving your RAM. Perhaps you don't need it if you have an M1 MacBook, but with a 2015 MacBook Air like mine, I certainly depend on it everyday.
The frustration comes from the number of times I lost all my tabs due to a) computer crashing, b) computer dying and c) Safari app crashing/force quitting due to this popup that I'm sure some of you can relate to.
My point is, I was desperately in need of a way to save tabs. And yes, I use an extension called Replicate Tabs that allows me to save the tabs, but unfortunately, this extension is not compatible with my other extension, Suspender. This means that the suspended tabs - which make up more than 90% of my tabs - do not get saved at all. This is why I turned to AppleScript, as any programmer should!
If you're like me, perhaps you have wondered how many tabs you actually have open. Checking the tab count is not possible from the browser, so that's why I wrote a script for it!
Well actually, I found this code from here, so I didn't write it, but here it is in case you want it as well:
tell application "Safari" --Variables set winlist to every window set totaltabcount to 0 -- Loop through each window to count the number of open tabs repeat with win in winlist try set tabcount to number of tabs in win set totaltabcount to totaltabcount + tabcount -- log "tab count: " & tabcount & " totaltabcount: " & totaltabcount on error errmsg -- Often getting error message like this: -- "Safari got an error: AppleEvent handler failed." -- log "error message: " & errmsg end try end repeat log "There are " & totaltabcount & " Safari tabs open." end tell
To execute this from the Terminal, simply type osascript filename.scpt
It's cool knowing the number of open tabs, but that's useless unless we can actually save them. Again, I managed to find this code online from this site, so I did not write it.
tell application "Safari" set windowCount to number of windows set docText to "" repeat with x from 1 to windowCount set tabCount to number of tabs in window x repeat with y from 1 to tabCount set tabName to name of tab y of window x set tabURL to URL of tab y of window x set docText to docText & "<a href=" & "\"" & tabURL & "\">" & tabName & "</a>" & linefeed as string end repeat end repeat tell application "TextEdit" activate make new document set the text of the front document to docText end tell end tell
docText
to an empty string so that we can add to it as we loop through the tabs.tabName
and tabURL
, then add it to docText
. This contains the URL and name of each tab in HTML format (using the anchor tag), so we must escape the " character by using a backslash, \. linefeed
, a line break (similar to \n from the other languages).docText
) which contains a list of all the tab names and links.Isn't it cool how we can use AppleScript to automate many of the tasks that would be too mundane for us? I don't know the language well, but I will continue to experiment with it to optimise my workflow and experience on the computer.