Skip to content

Instantly share code, notes, and snippets.

@Joilence
Forked from tshu-w/save_safari_tabs.scpt
Created July 5, 2019 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Joilence/3046b9e84e1c249796ec5d6eaa1f25ba to your computer and use it in GitHub Desktop.
Save Joilence/3046b9e84e1c249796ec5d6eaa1f25ba to your computer and use it in GitHub Desktop.
Save all safari tabs in a single file and reopen them with a single 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
-- Create custom icons for the scipts
-- command line tool needed: http://www.hamsoftengineering.com/codeSharing/SetFileIcon/SetFileIcon.html
set exePath to (path to home folder as text) & ".scripts:SetFileIcon"
set iconPath to (path to home folder as text) & "Pictures:bookmark.png"
try
do shell script quoted form of POSIX path of exePath & " -image " & quoted form of POSIX path of iconPath & " -file " & quoted form of scriptPath
on error errorMessage number errorNumber
-- display notification errorMessage with title "Save Safari Tabs" sound name "Frog"
end try
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment