Create a text file of safari tabs?

I'm looking to create two macros:

  1. A macro that will create a txt file with a list URLs from all the open tabs in safari.
  2. A second macro that will take that list of URLs and reopen those links as tabs in safari.

This could also be macros that writes a set of bookmarks and then opens and deletes the bookmarks in the 2nd macro.

(basically, I want to "save my work" when I'm done working for the day, and then "open up" to the same tabs, even though I will use safari for personal use in between!)

I think that if any one has any directional suggestions, I should be able to figure it out myself. I'd just love a nudge in the right direction. Thanks!

Here an approach in a very basic form:

Untitled-pty-fs8

Notes:

  • The hotkey works as toggle:

    • If there are no saved URLs, the macro will save all tab URLs
    • If the macro finds saved URLs, it will load the URLs and delete the variable that contains the URLs
    • If you don’t like the toggle character, split the macro into two; or add a user prompt that decides whether URLs should be saved or reloaded.
  • All tabs are restored to a single window, even if they were originally distributed over several windows


Note 2019-08-26:

This post will no longer be updated.

As suggested by some users I have created my own topic with this macro in the Macro Library section.

You find any new versions of the macro as well as the download link in the new topic.

Please post any new comments/suggestions/issues related to the macro in the new topic.

You might take a look at the Safari Extension Tab Lister .

It is very similar to OneTab for Chrome, which I use very often.

Tom - oh my god, this is amazing. Works like a charm. I'm trying to go through all the scripts, line by line to understand how it works. One question, since I'm planning on turning my computer off in between running the macro, I am guessing the system variable will get deleted? Or does it save those somewhere, even when the machine is shut down and the restarted?

The tab URLs are saved to a global KM variable (SafariSessionURLs), so it is persistent.

1 Like

Not very complex:

The macro checks if there exists a KM variable “SafariSessionURLs”.

If not, the first script is run:

The script itinerates through all tabs of all Safari windows appends the URL of each tab to a list (“URLList”). This list is exported (saved) to a KM variable called “SafariSessionURLs”.

If yes, the second script is run:

The script reads the URLs from the “SafariSessionURLs” KM variable back into a list (“URLList”). Then it itinerates through the list and creates a tab from each URL of that list in the current Safari window (or a new one, if there isn’t any).[1]
After the script has run, KM deletes the “SafariSessionURLs” KM variable.


As said, the macro and the scripts are very basic. No error checking, nothing. It is meant as a base where you can build on to create a macro that suits your needs.


1: If the script creates a new window for each URL, check your Safari Preferences > Tabs.

Bear in mind that Safari tabs that are left dormant (not brought into view) for a certain period won't have their URLs returned by AppleScript, which will return missing value in those instances. If this is an issue, you might have better results having the script save the tabs to a designated bookmark folder, which can be reserved for storing and re-storing groups of tabs as you please.

Thanks for the heads-up. I remember now remotly that I once had issues with that (or some similar tab abnormality).

Is this still happening with the current OS and Safari?


Edit/PS:

Just did a quick test with Safari Technology Preview (not Safari): I had 4 windows open with a total of 21 tabs, some of them no more touched (activated) since this morning (>12h). The macro restored 21 tabs.

I know, this does not prove anything.

I can only confirm it is still a phenomenon in Safari on High Sierra, but cannot give any feedback for macOS Mojave or later. I know my resistance to upgrading will eventually have to concede, but I'm holding out for as long as I possibly can, or until I create a USB bootable thumb drive.

OK, did another test, this time with Safari Technology Preview and Safari: Both of them had numerous tabs that were dormant (not brought to foreground) since yesterday evening. For both apps I could now (~18h later) save and restore all tabs with the macro.
The 18h include the system sleep time over night. Safari was hidden all the time, STP not.

So, there are chances that this really has been fixed (I’m on OS 10.14.6), which would be astonishing though. Or it needs still more dormant time to trigger the issue, or other factors (e.g. low on memory). Don’t know.

1 Like

Tom - this is great. You should publish this separately as your own macro. This is a great addition, for my ability to “context switch” between Work and Fun on the same machine!

Happy that it’s useful for you!

But keep an eye open for the possible issue @CJK has mentioned above.

If you want, you could change the title of your topic to something like “Save and Restore Safari Session (tabs)” and add the appropriate tags to the topic. This way people will probably find it more easily with the forum search.

Tom, I agree with @richtack, you should publish in the Macro Library, so that it can be found using your forum name.

OK. Then I will create a new topic with post#2 as OP.

Could you then move the follow-up discussion to the new topic? (It is important because of the issue @CJK mentioned.)
Or should I just refer to the discussion in this topic?


Edit:

@JMichaelTX, I have created a new topic here.

Yep, that works.