I had previously posted about one of my oldest but still frequently used scripts which gathers all tabs from a window into a text list to be pasted in an email or something like that.
I have recently adapted that script to do something new. This new script will gather the tabs in the frontmost Safari window and create a note in the Notes app with all the links (then, presumably, you would want to clean out and close most of the tabs)
property defaultAccountName : "iCloud" | |
property defaultFolderName : "Saved Safari Windows" | |
global html | |
global processedURLs | |
on appendLineWithDoc(theDoc) | |
tell application "Safari" | |
tell theDoc | |
try | |
my appendHTML(" <li>") | |
my appendHTML("<a href=\"" & URL & "\">") | |
my appendHTML(name) | |
my appendHTML("</a></li>" & return) | |
set processedURLs to processedURLs & (URL as string) | |
end try | |
end tell | |
end tell | |
end appendLineWithDoc | |
on appendHTML(htmlString) | |
set html to html & htmlString | |
end appendHTML | |
on defaultFolder() | |
tell application "Notes" | |
if not (exists account defaultAccountName) then | |
display dialog "Cound not find account '" & defaultAccountName & "'!" | |
tell me to quit | |
end if | |
if exists folder defaultFolderName of account defaultAccountName then | |
return folder defaultFolderName of account defaultAccountName | |
end if | |
make new folder at account defaultAccountName with properties {name:defaultFolderName} | |
end tell | |
end defaultFolder | |
on run | |
set datestamp to "Saved on " & (short date string) of (current date) & " at " & (time string) of (current date) & return | |
set html to "<h1>" & datestamp & "</h1>" & return | |
my appendHTML("<ul>" & return) | |
set processedURLs to {} | |
tell application "Safari" | |
activate | |
set w to window 1 | |
set n to 0 | |
try -- this will fail for the downloads window | |
set n to count tabs of w | |
end try | |
if n > 1 then | |
repeat with t in every tab of w | |
my appendLineWithDoc(t) | |
end repeat | |
else if n = 1 then | |
my appendLineWithDoc(document of w) | |
end if | |
end tell | |
my appendHTML("</ul>" & return) | |
if (count of processedURLs) > 0 then | |
set f to my defaultFolder() | |
tell application "Notes" | |
make new note at f with properties {body:html, name:datestamp} | |
activate | |
end tell | |
end if | |
end run |
Also I learned that the Safari tabs from all your Macs and iOS devices are stored in the `~/Library/SyncedPreferences/com.apple.Safari.plist` and extended the script to read that instead of grabbing them directly from Safari:
property defaultAccountName : "iCloud" | |
property defaultFolderName : "Saved iCloud Tabs" | |
global html | |
global processedURLs | |
on appendLineWithURLItem(urlItem) | |
my appendHTML(" <li>") | |
my appendHTML("<a href=\"" & |url| of urlItem & "\">") | |
my appendHTML(|Title| of urlItem) | |
my appendHTML("</a></li>" & return) | |
set processedURLs to processedURLs & (|url| of urlItem) | |
end appendLineWithURLItem | |
on appendHTML(htmlString) | |
set html to html & htmlString | |
end appendHTML | |
on defaultFolder() | |
tell application "Notes" | |
if not (exists account defaultAccountName) then | |
display dialog "Cound not find account '" & defaultAccountName & "'!" | |
tell me to quit | |
end if | |
if exists folder defaultFolderName of account defaultAccountName then | |
return folder defaultFolderName of account defaultAccountName | |
end if | |
make new folder at account defaultAccountName with properties {name:defaultFolderName} | |
end tell | |
end defaultFolder | |
on run | |
set datestamp to "Saved on " & (short date string) of (current date) & " at " & (time string) of (current date) & return | |
set html to "<h1>" & datestamp & "</h1>" & return | |
set processedURLs to {} | |
tell application "System Events" | |
set SafariPrefsPlist to property list file "~/Library/SyncedPreferences/com.apple.Safari.plist" | |
repeat with x in every property list item of property list item "values" of SafariPrefsPlist | |
set v to property list item "value" of x | |
if exists property list item "Tabs" of v then | |
set deviceName to value of property list item "DeviceName" of v | |
my appendHTML("<h3>" & deviceName & "</h3>" & return) | |
my appendHTML("<ul>" & return) | |
set tabsPlist to value of property list item "Tabs" of v | |
repeat with i in tabsPlist | |
my appendLineWithURLItem(i) | |
end repeat | |
end if | |
my appendHTML("</ul>" & return) | |
end repeat | |
end tell | |
if (count of processedURLs) > 0 then | |
set f to my defaultFolder() | |
tell application "Notes" | |
make new note at f with properties {body:html, name:datestamp} | |
activate | |
end tell | |
end if | |
end run |
To use it is probably easiest to enable the Script menu (in Script Editor > Preferences) and drop the scripts in the `~/Library/Scripts/Applications/Safari` folder.
Thank you very much for sharing this, it’s exactly the script I needed.
What would I need to do to adapt this script to paste into any application like Mail or OneNote as opposed to opening Notes and creating a new folder?
I’d like to use TextExpander to run the Applescript and just pull the tabs as links and paste them wherever I need.
try this script: https://scriptingosx.com/2013/01/copy-link-list-from-frontmost-safari-window/
Thanks for replying. I came across that post as well but need the formatting of the one that creates a new Notes folder.
Titles with hyperlinks instead of URL pasted below the title.
I need the same function (pasting the list) from the other post but the formatting for output of this one.
I changed the script above to pipe the html into textutil and then pbcopy. It works for me in Mail. I don’t use OneNote but it is worth a try:
https://gist.github.com/scriptingosx/52ebbb2e3d030b53556f48c90df0a5e2
Where do I paste the script please?
Also will this work with my iPhone?
Thanks
Unfortunately this script has been defunct since 10.12