Skip to content

Instantly share code, notes, and snippets.

@thesyntaxinator
Last active February 17, 2023 17:51
Show Gist options
  • Save thesyntaxinator/a8aa60dd840e3b0f767d251dab66c605 to your computer and use it in GitHub Desktop.
Save thesyntaxinator/a8aa60dd840e3b0f767d251dab66c605 to your computer and use it in GitHub Desktop.
Safari save all tabs (tested on Mac OS Sierra 10.12.1 with Safari 10.0.1)
-- Based on code from:
-- https://gist.github.com/agoddard/5114024
-- https://gist.github.com/edenwaith/2213a764ccb091d6a03989f238efb63f
-- https://computers.tutsplus.com/tutorials/generate-a-list-of-open-safari-tabs-with-applescript--mac-30564
-- Instructions
-- 1. Open Automator -> create new Service.
-- 2. Select "no input" and "Safari"
-- 3. Drag "Run AppleScript" to the pane on the right.
-- 4. Paste the below code where it says "(* Your script goes here *)".
-- 5. Save the service and call it something like "Safari save all tabs".
-- 6. In Safari, click Safari (top left next to the Apple icon) -> Services -> your service (whatever you named it above).
tell application "Safari"
--Variables
set windowCount to number of windows
set docText to ""
--Repeat for Every Window
repeat with x from 1 to windowCount
if name of window x is not "" then
try
set tabcount to number of tabs in window x
--Repeat for Every Tab in Current Window
repeat with y from 1 to tabcount
--Get Tab Name & URL
set tabName to name of tab y of window x
set tabURL to URL of tab y of window x
set docText to docText & tabName & linefeed & tabURL & linefeed & linefeed as string
--set docText to docText & "<a href=" & "\"" & tabURL & "\">" & tabName & "</a>" & linefeed as string
end repeat
on error errmsg
-- Often getting error message like this:
-- "Safari got an error: AppleEvent handler failed."
-- log "error message: " & errmsg
end try
end if
end repeat
end tell
--Write Document Text
tell application "TextEdit"
activate
make new document
set the text of the front document to docText
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment