Save and Restore Safari Session (Tabs)

awesome!
wonder if the script can be modified to auto save current session every used defined X minutes to a variable/file to be able to restore after a bad crash?

also how does one restore the tabes? I can see it only currently saves the tabs?

again thx a lot, very useful for me!

Z

macOS does this as standard unless you specifically set it not to. As a test, you should be able to force quit Safari and have the same tabs and windows reopen when you next open Safari.

In fact, I use KM to set CMD+Q to kill Safari rather than quit, so each time I open it, the previous session is restored (you can have Safari do this by default, but other applications don't necessarily give the option so one macro covers any application for which I would want this behaviour).

thx @CJK
I do use that but I have had on occasion safari crash and not come back with many tabs :slight_smile: so I thought it would be great to have KM act as a robust backup in the rare cases that macOS for whatever reason dosent bring the tabs back

thx!
Z

You can certainly do that, e.g. using a periodic trigger or a “focused window” trigger. But this is not the scope of my macro.

But IMHO I’m not sure if it is worth to use a macro for this, since the functionality, as pointed out by @CJK, is already part of the OS, and the session restore via OS fails only in rare cases for you.

Is your Safari crashing often? If yes, then maybe it would be worth to investigate the reason for the crashes (extensions?, …). Currently —on my system— I don’t remember a complete Safari crash for a while. (From time to time a subprocess, likely as part of an open page, is crashing; but this doesn’t bring down the whole Safari.)

For a new version of the macro see the update note 2019-09-04 in the original post.

1 Like

Referring to this post by @CJK:

While playing around with a new version of my macro and different save/restore methods, I now experienced again the “missing value” URLs. So, they are not gone with Mojave, unfortunately.

Often these are tabs that have a name of “Untitled”, but not always.

It seems these URL-less tabs can sometimes be created when saving not yet fully loaded tabs via AS, for example tabs that have been restored with Safari’s “Reopen Last Closed Window”. In this case it will have negative impact on the usefulness of the script, as tabs will be missing upon restore.

However, they also seem to appear “out of nowhere”, while all visible tabs are saved and restored correctly. In this case they are irrelevant for the script. According to the tab list created via AS, these phantom tabs seem to belong to some kind of invisible zombie windows. I couldn’t find out why or when these are created, but it sometimes happens even shortly after a Safari (or STP) restart without any restored sessions.

My understanding was that the missing value URLs were time-dependent, as Safari unloads from memory tabs that have not been active for a certain amount of time (which would make a lot of sense, as websites, even just one, can use a lot of resources).

However, I wouldn’t think it ought to affect session data, e.g. restoring windows from previous sessions, or reopening the last tab. I think these data are stored separately, certainly session data is stored in .savedState files (or maybe it’s a bundle, I don’t recall). The most recent tab, I’m less certain about. However, I do recall that, without needed to “awaken” any dormant tabs, you can bookmark the whole lot and they will bookmark successfully.

I’m uncertain how that could be. The only way .savedState data files are erased are when Safari exits cleanly. I’m not doubting this has happened to you, just that it would make me wonder if something else is going on.

There’s a menu item to restore windows from previous session. Have you seen if that works following one of these events?

In my experiments from last week I was unable to reproduce this.

But here an example of two different — not time-dependent — types of missing value URLs:

This is the result of set tabList to tabs of windows in Safari, after having closed a window with 3 tabs (the only window) and then immediately reopened it via Safari’s “Reopen Last Closed Window”:

Tabs 1, 2 and 3 were the tabs that were open before closing the window. After “Reopen Last Closed Window” all 3 tabs were visibly restored but apparently only tab 1 (the frontmost tab) has fully loaded, hence the missing value for tabs 2 and 3. This is understandable.

Tab 4 was a tab I had closed shortly before closing the window. You see that in the tab list now it appears again, and as member of a different window (id 6959) and with missing value. Although “visible” of this tab is true, note that the parent window of this tab (and hence the tab) is not on screen (= not visible).

Tab 5 is similar to tab 4 in the sense that it also belongs to a separate (and invisible) window (id 7739). However, in contrast to tab 4, I cannot explain where it comes from. It seems like a ghost clone of tab 1.

Here the same thing on window level (set winList to windows):

“document” of the invisible windows (id 6959 and 7739, containing tabs 4 and 5) is missing value, and “visible” of these windows is false.

Thank you for going through the trouble of explaining that, and doing it so clearly. That is an interesting problem.

Thank you for this thread. It has been beneficial and educational. I came here wanting to have a reliable way of saving my tabs at all times, having tried different Safari plugins that didn't quite seem to deliver on their promises in a consistent matter.

But I also needed something more automatic, so I have redone @Tom 's macro so that Safari saves all tabs whenever the focused window title changes. Then, a separate macro only restores the current saved set of tabs in a new window.

If you start navigating to any saved tab groups, saving the tabs every time the focused window title changes is an issue. Then, the tab groups get saved instead, and these are saved anyway to be shared across Apple's ecosystem. So we're only interested in keeping the tabs in the browser window that is not part of these. So, I have incorporated an AppleScript that checks through UI scripting if any tab groups are active before trying to save the tabs. In that way, we only keep the tabs not being saved anyway by Safari.

I hope you can use or modify these to fit your workflow.

Safari Macro Group Macros.kmmacros (20.9 KB)

Cheers,
Massimo

Hi all

im trying to follow the macro but I am pretty bad with coding :slight_smile:

I have a different use case. I would like to "sync" (manually) all open tabs from one safari on a MBP to another safari on my air m3 laptop. can this be modified to do so somehow? is there another less complex way (I need to do this manual "sync" once a month or so)

thx so much!

Z

No solution at hand. But there is one, maybe.

Basically: use the very first func as shown in the topic, to get the array; then retrieve it with the safari on the other Mac. Thats the theory.

I tried that, it saves the array as expected but Safari does not pick it up.


PS: to be continued

thx @Tom !

really appreciate it!

Z

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.

SAVE-RESTORE-DELETE SESSION v1.1.kmmacros (91.6 KB)
(uploaded in disabled state)

1 Like

wow amazing @BKammer !

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 :slight_smile: any clue?

attached is the current state of the macro

SAVE-RESTORE-DELETE SESSION.kmmacros (93.3 KB)

thx so much for your epic macro!

Z

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!

SAVE-RESTORE-DELETE SESSION v1.1 (FOR ZELTAK).kmmacros (91.5 KB)
(uploaded in disabled state)

1 Like

this is so great thank you so much @BKammer !!!

ps , I assume the pinned state of tabs are impossible to get from safari right?

This is hugely appreciated!

Z

1 Like

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!

1 Like

yes it works like a charm!! thx so much again @BKammer !!

Z

1 Like