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 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).
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?
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 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.
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.