Save and Restore Safari Session (Tabs)

This macro was initially an answer to this question:

The Macro

[--> For a newer version of the macro scroll a bit down in the post.]

Goal is to quickly save or restore a Safari session (all open tabs).

Here an approach in a very basic form:

[test] Save and Reopen Safari Session.kmmacros (5.0 KB)

Usage & 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

2019-08-28: Working on a new version…

  • almost completed…

2019-09-04: New Version (v2)

Since I consider this a beta version and I didn’t have too much time for testing, I leave the original version posted above.

Basically it works the same way as the old version:

  • 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 restore the URLs to tabs and delete the saved URLs


I adopted the improved AppleScripts by @ccstone for scraping the URLs and – optionally – for restoring the tabs.

The macro now has a little user prompt:

  • Restore method:

    • Same window (no duplicate tabs): Virtually the same as in the original script: tabs are created via open location in the current window (if one is open)
    • New window (possibly duplicate tabs): Chris’ AppleScript: restores the saved tabs to a new window, non-regarding any already open URLs. Is likely to be faster.
  • Safari Technology Preview: Use STP instead of Safari (can also be used to transfer tabs from one app to the other).

  • Clear current save: Force-clear the currently saved session (so that the next macro run will be a new save instead of a restore)

  • Restore Previous Save Now: Restores the tabs that were used for the previous restore, discarding the currently saved tabs. (Sort of a backup if you have accidentaly closed your restored tabs, or just to repeatedly restore the same session.)

  • The user prompt opens when the is down at macro launch: Either in combination with the hotkey trigger (default F1, with prompt: F1, or by holding down while selecting the macro from the palette/menu.

  • The macro should also be operable without ever opening the prompt. (With its basic toggling save/restore functionality.)

The macro now has some mechanism to detect and eliminate “missing values”, including a warning when this happens.

The Macro (v2b)

Safari: Save and Restore Session (v2.0b).kmmacros (30.5 KB)

Some already existing discussion related to this macro is located at the original topic.

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


This has been super helpful. Thanks again, Tom.

For anyone wondering, I use this when I want to take a break from work, continue using the same computer for fun, and then resume work later. Very helpful!

1 Like

Thanks for the feedback.

You can tick the Solution check box at the bottom of the post that was most helpful (in your original topic), if you want.

Hey Tom,

These AppleScripts will probably be a bit speedier:

Scrape all URLs from all tabs in all windows:

set AppleScript's text item delimiters to linefeed
tell application "Safari"
   set urlList to (URL of tabs of windows) as text
end tell
return urlList

Example of restoring all of them to one new window:

set urlList to ""

set urlList to paragraphs of urlList

tell application "Safari"
   set newDoc to make new document with properties {URL:item 1 of urlList}
   set urlList to rest of urlList
   tell front window
      repeat with theURL in urlList
         make new tab at end of tabs with properties {URL:theURL as text}
      end repeat
   end tell
end tell



Chris, many thanks for your optimization! Your scraping part is a no-brainer, I’m undecided on the restoring part (new window & duplicate tabs vs current window & no duplicate tabs).

I think I will make this an option (with a GUI prompt), along with other options I had in mind, e.g. restoring a backup session. Working on that.

BTW, many thanks for reminding me that “URLList” is a very ugly (and potentially misleading) way of naming a variable. :slightly_smiling_face:

All so interesting! I don't fully understand, but I'll be watching...

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!


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


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)


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!


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!