This is fun stuff. Here's a solution that cribs from the Applescript work in previous responses to the original question; requires user to save tab session as opposed to continuously monitoring session tabs; allows user to nickname saved session; allows user to restore most recent saved session or choose previously saved session from list; restores in current or new window.
If user chooses to restore in new window, macro checks for duplicate tabs in current window and, if detected, offers to open saved session in current window, or alternatively to close current window and open session in new window.
This macro requires user to create a directory as a repository for the text files containing the tab lists for the saved sessions, and to insert the path to that directory in various actions within the macro. The macro also allows user to delete one or more of the text files from the repository.
UPDATE: User @zeltak in his reply (below) alerted me to bugs in the initial version of this macro, which I believe I have addressed in the updated macro, v1.1.
I tried it now and edited all places where it said (edit file path here) but when I try and run the macro all it does is open the current path I edited any clue?
Hey, so sorry it didn't work. I went through it with a fine-tooth comb and did identify some issues in a couple of the scripts, and a variable typo, all of which were causing it to fail. I have fixed those issues and tested this updated version and it should function properly. I updated my post above to include the corrected macro, and I'm uploading into this reply a version of the corrected macro that contains your unique Safari sessions folder file path. So you should be able to download this, enable it, and run it directly on your machine without having to re-enter the file paths inside the macro. Let me know how it goes!
I suspect there is a way to detect pinned state of tabs, but that is beyond my ability. The way I figure these things out is by Googling and then tinkering with whatever is out there until it works. Sometimes it doesn't!
So, did the macro finally work as described? I'm curious!