Skip to content

Instantly share code, notes, and snippets.

@tshu-w
Last active February 3, 2023 03:24
Show Gist options
  • Save tshu-w/146efa449176c37fb1e3d75ddda6cf37 to your computer and use it in GitHub Desktop.
Save tshu-w/146efa449176c37fb1e3d75ddda6cf37 to your computer and use it in GitHub Desktop.
Save all safari tabs in a single file and reopen them with a double click.
-- Detect if the Safari window exists
tell application "System Events"
if not (exists (front window of process "Safari")) then
return
end if
end tell
-- Assume the frontmost Finder window (or the Desktop)
-- is where we want to store the script.
try
tell application "Finder" to set defaultFolder to the folder of the front window
on error
set defaultFolder to (path to desktop)
end try
-- Initialize the text ot the script.
set cmd to "#!/bin/bash" & linefeed & linefeed
-- Add commands to open all the tabs.
set cmd to cmd & "nohup open \\" & linefeed
tell application "Safari"
set n to count of tabs in front window
repeat with i from 1 to n
if (URL of tab i of front window is not equal to "topsites://") then
set cmd to cmd & " \"" & URL of tab i of front window & "\" \\" & linefeed
end if
end repeat
end tell
set cmd to cmd & ">/dev/null 2>&1 &" & linefeed
-- Add command to quit terminal
set cmd to cmd & linefeed & "osascript -e 'tell application \"Terminal\" to quit'"
-- Open/create a file and save the script.
tell application "Safari"
activate
set scriptAlias to choose file name default name "tabset" default location (defaultFolder as alias)
close (every tab of front window)
end tell
set scriptPath to POSIX path of scriptAlias
set scriptFile to open for access scriptAlias with write permission
set eof scriptFile to 0
write cmd to scriptFile starting at eof
close access scriptFile
-- Change the file attributes to make it double-clickable.
do shell script "chmod 777 \"" & scriptPath & "\""
do shell script "xattr -wx com.apple.FinderInfo '00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00' \"" & scriptPath & "\""
return
@swestenb
Copy link

swestenb commented Jul 4, 2018

very cool! super useful script, many thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment