MacOS – How to save list of open Safari Tabs in a text file

macossafaritabs

I open a lot of tabs which are important for me. Isn't there an easier way to install something that creates a TextEdit file on my desktop with all URLs open at the time?

In the past I used Eelee's SafariTabsListDs but this doesn't seem work anymore.

Can you help me with something that I can use to reopen the tabs I want after closing them?

Best Answer

The following example AppleScript code will write the URL of every tab of every window of Safari to a file named MyURLs.txt on your Desktop:

set myURLs to {}
tell application "Safari" to set myURLs to the URL of every tab of every window
set text item delimiters to linefeed
set myURLs to myURLs as text
set text item delimiters to {}
if not myURLs is equal to "" then
    do shell script "echo " & myURLs's quoted form & "> $HOME/Desktop/MyURLs.txt"
end if

To open the URLs in, e.g., MyURLs.txt you can use, e.g.,:

set myURLs to {}
try
    set myURLs to paragraphs of (do shell script "cat $HOME/Desktop/MyURLs.txt")
end try
if not myURLs is equal to {} then
    repeat with thisURL in myURLs
        open location thisURL
    end repeat
end if
  • The example AppleScript code above assumes Safari is your default web browser.

Note: The example AppleScript code is just that and does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

Related Question