Skip to content

Instantly share code, notes, and snippets.

@thesyntaxinator
Last active November 28, 2016 21:06
Show Gist options
  • Save thesyntaxinator/07685a3bca9aed3831b1636b26e4e4d6 to your computer and use it in GitHub Desktop.
Save thesyntaxinator/07685a3bca9aed3831b1636b26e4e4d6 to your computer and use it in GitHub Desktop.
Safari save tabs in current window (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 tabs in current window".
-- 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 docText to ""
set x to 1 -- current safari window
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