Save and Restore Safari Session (Tabs)

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 "https://forum.keyboardmaestro.com/
https://forum.keyboardmaestro.com/t/use-of-variables-in-mathematics/15055
https://forum.keyboardmaestro.com/t/how-can-i-set-km-to-find-copy-a-string-of-numbers-in-a-document/14815/4
https://www.google.com/
https://www.macupdate.com/"

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

3 Likes