Adding Bookmarks for multiple tabs in Safari with a keyboard shortcut

bookmarkssafaritabs

In Safari, there is the Add Bookmarks for These N Tabs… function. Unfortunately, you cannot create a Keyboard shortcut for that because those need to match the name of the menu item.

Best Answer

You can use this Applescript; it creates a shell script containing the links for all the tabs in the current Safari window:

-- Set the default folder to Home -> bookmarks
set bookmarkFolder to "/bookmarks"
set username to do shell script "whoami"
set defaultFolder to POSIX file ("/Users/" & username & bookmarkFolder)

-- Initialize the text ot the script.
set cmd to "#!/bin/bash" & linefeed & linefeed

-- Add commands to open all the tabs.
tell application "Safari"
    set n to count of tabs in front window
    repeat with i from 1 to n
        set cmd to cmd & "open -g " & URL of tab i of front window & linefeed
    end repeat
end tell

-- Open/create a file and save the script.

tell me
    activate
    set scriptAlias to choose file name default name "tabset" default location (defaultFolder as alias)
end tell
set scriptPath to quoted form of 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

The original version is by Dr. Drang.

I have changed two things:

  • the script allows for whitespace in the file name
  • the default folder is ~/bookmarks

To run it, you can either use a launcher (like Quicksilver or Butler) or wrap it in a System Service with Automator:

  • create the service
    • add a Run Applescript item
    • select Service receives no input in Safari
    • paste the Applescript
  • create a shortcut in System Preferences > Keyboard > Shortcuts > Services
    • ⌘⌥D does not work if you use it to toggle the visibility of the Dock

enter image description here