Skip to content

Instantly share code, notes, and snippets.

@pch
Created April 30, 2025 17:41
Show Gist options
  • Save pch/3ab7041d687ea1d01845b40f517e46c3 to your computer and use it in GitHub Desktop.
Save pch/3ab7041d687ea1d01845b40f517e46c3 to your computer and use it in GitHub Desktop.
tabs
tell application "Safari"
set tabData to ""
set windowList to every window
repeat with theWindow in windowList
repeat with theTab in (every tab of theWindow)
set tabData to tabData & (name of theTab) & linefeed & (URL of theTab) & linefeed & linefeed
end repeat
end repeat
set targetFile to ((path to desktop folder as text) & "tabs.txt")
try
set fileRef to open for access targetFile with write permission
write tabData to fileRef
close access fileRef
on error
try
close access fileRef
end try
end try
end tell
-- Read the file content
set targetFile to ((path to desktop folder as text) & "tabs.txt")
set fileContent to read file targetFile as text
set urlList to {}
-- Split the content by line breaks and extract URLs
set AppleScript's text item delimiters to linefeed
set contentLines to every text item of fileContent
set AppleScript's text item delimiters to ""
-- Extract URLs (every other non-empty line)
repeat with i from 1 to count of contentLines
set currentLine to item i of contentLines
if currentLine starts with "http" then
set end of urlList to currentLine
end if
end repeat
-- Open URLs in Safari
tell application "Safari"
-- Make a new window
make new document
-- Get the reference to the new window
set newWindow to window 1
-- Set the first URL
if (count of urlList) > 0 then
set URL of current tab of newWindow to (item 1 of urlList)
end if
-- Open remaining URLs in new tabs
repeat with i from 2 to count of urlList
tell newWindow
make new tab at end with properties {URL:(item i of urlList)}
end tell
end repeat
-- Activate Safari
activate
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment