-
-
Save kshiteesh/b72e93d31d65008fcd11 to your computer and use it in GitHub Desktop.
(* | |
Export All Safari Tabs in All Open Windows to a Markdown File | |
July 13, 2015 | |
// SCRIPT PAGE | |
http://hegde.me/urlsafari | |
// ORIGINAL SCRIPT ON WHICH THIS SCRIPT IS BUILT | |
http://veritrope.com/code/export-all-safari-tabs-to-a-text-file | |
// CHANGES COMPARED TO THE ORIGINAL SCRIPT | |
1. Save URLs to .md instead of .txt | |
2. Save URLs from all windows instead of just the first window | |
3. Save the file to Downloads/ instead of Desktop/ | |
// DISCLAIMER | |
I have made some minor changes to the orginal script which can be found at veritrope.com (full link given above). I'm not affiliated with veritrope.com. | |
// TERMS OF USE: | |
This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. | |
To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA. | |
*) | |
---------------------------------------------------------------------------------------- | |
-- NAME OF REPORT TITLE | |
property report_Title : "URL List from Safari" | |
-- PREPARE THE LIST | |
set url_list to {} | |
set the date_stamp to ((the current date) as string) | |
set NoteTitle to "# " & the date_stamp | |
-- GET TABS FROM SAFARI | |
set window_count to 1 | |
tell application "Safari" | |
activate | |
set safariWindow to windows | |
repeat with w in safariWindow | |
try | |
if (tabs of w) is not {} then | |
copy ("## Window " & window_count & ":" & return) to the end of url_list | |
end if | |
repeat with t in (tabs of w) | |
set TabTitle to ("1. [" & name of t & "]") | |
set TabURL to ("(" & URL of t & ")") | |
set TabInfo to (TabTitle & TabURL & return) | |
copy TabInfo to the end of url_list | |
end repeat | |
end try | |
set window_count to window_count + 1 | |
end repeat | |
end tell | |
-- CONVERT URL_LIST TO TEXT | |
set old_delim to AppleScript's text item delimiters | |
set AppleScript's text item delimiters to return | |
set url_list to (NoteTitle & return & return & return & url_list) as text | |
set AppleScript's text item delimiters to old_delim | |
-- CHOOSE FILE NAME FOR EXPORT | |
tell application "Finder" | |
activate | |
set save_File to choose file name with prompt "Name this file:" default name report_Title default location (path to downloads folder) | |
end tell | |
--WRITE THE FILE | |
tell application "System Events" | |
set save_File to open for access (save_File & ".md" as string) with write permission | |
try | |
write url_list to save_File | |
end try | |
close access save_File | |
end tell |
replace all the 'return' with 'linefeed' to make it look nice in terminal (unix/terminal only uses linefeed, not carriage return)
I'm surprised this works in 2020 for Safari 14. But I have tab titles containing Chinese and Japanese characters so I changed line 70 to write url_list to save_File as «class utf8»
to make it compatible.
It still works in macOS 10.15 with Safari 15.1.
I'd love it if someone wrote a script that can restore all the tabs in the same window groups! It's outside my capabilities and would be a secure alternative to the Safari extensions that do this but have access to everything on every page you have open…
Thanks for the script! Works like a charm!
August 2023 - still works!!!
Thank you!
for append to an existing file add this:
"write content to myFile starting at eof"
ps: let ScriptEditor itself write to a file with something like this (no need for System Events)
set F to (path to downloads folder as text) & "myTabs.txt"
set myFile to open for access F with write permission
write theContent to myFile starting at eof
close access myFile
tell application "Finder" to reveal F as alias
get all safari tabs with a single line, set delimiter to return to get output in paragraphs
tell application "Safari" to set allURLs to URL of tabs of windows
set AppleScript's text item delimiters to return
set mytabs to items of allURLs as string
set AppleScript's text item delimiters to ""
--return mytabs
Nice!