Skip to content

Instantly share code, notes, and snippets.

@yyolk
Forked from kshiteesh/urlsafari
Last active March 8, 2024 22:12
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 yyolk/5ee36b12271d89371fe859630aee80a4 to your computer and use it in GitHub Desktop.
Save yyolk/5ee36b12271d89371fe859630aee80a4 to your computer and use it in GitHub Desktop.
This AppleScript saves all the tabs open in all Safari windows to a Markdown file.
(*
Export All Safari Tabs in All Open Windows to a Markdown File
March 8th, 2024
// SCRIPT PAGE
https://gist.github.com/yyolk/5ee36b12271d89371fe859630aee80a4
// ORIGINAL SCRIPT ON WHICH THIS SCRIPT WAS ORIGINALLY BUILT
http://veritrope.com/code/export-all-safari-tabs-to-a-text-file
// CHANGES COMPARED TO THE ORIGINAL SCRIPT THAT THIS SCRIPT WAS FORKED FROM
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/
// CHANGES IN THIS FORK
1. Filename includes a date prefix as `YYYYMMDD-HHMM report_Title` as default when saving without using script calls (no permission required).
2. Uses LF over CR
3. Writes file with UTF-8 encoded text
// 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"
-- PREFIX REPORT TITLE WITH DATETIME STAMP OF YYYYMMDD-HHMM
tell ((current date) as «class isot» as string) to set report_Title to text 1 thru 4 & text 6 thru 7 & text 9 thru 10 & "-" & text 12 thru 13 & text 15 thru 16 & " " & report_Title
-- 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 & ":" & linefeed) 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 & linefeed)
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 linefeed
set url_list to (NoteTitle & linefeed & linefeed & linefeed & 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 as «class utf8»
end try
close access save_File
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment