MacOS – I need a keyboard shortcut for Adding Bookmarks for multiple tabs in Safari

bookmarkskeyboardmacossafarishortcut

There was an older post about this question (link below) from a few years back but I'm wondering if that process no longer works due to iCloud bookmarks, Safari 12, new MacOS etc.

What I would like to do is create a keyboard shortcut for the "Add Bookmarks for These (XX) Tabs…" in a current window of Safari. The System Preferences > Keyboard > Shortcuts doesn't work since the menu label has to be exact and the tab number variable needs to change to match the number of tabs each time. I did do a test with 5 in there and that works for an instance with 5 tabs. Not sure if there is a variable that can be written in there that knows it needs to change.

If not through system settings, assuming it would need to be something like an Apple Script which would work as I'm trying to use Stream Deck to set a button to fire the script.

Adding Bookmarks for multiple tabs in Safari with a keyboard shortcut

Best Answer

OK, so the AppleScript required to click that menu item is quite simple:

click menu item 8 of menu 1 of menu bar item "Bookmarks" of menu bar 1

To come up with this particular incantation I used UI Browser.app (there's a free Trial). In UI Browser.app choose your Target app, use the column browse to find the menu item you are interested in, then use the AppleScript dropdown to copy the required snippet to your clipboard (or press Alt+Cmd+T).

UI Browser hierarchy view

I made a small edit to the snippet to reference only the Bookmarks menu item by name.

The AppleScript snippet needs to be dressed up a little more to be useful:

activate application "Safari"
tell application "System Events"
    tell process "Safari"
        click menu item 8 of menu 1 of menu bar item "Bookmarks" of menu bar 1
    end tell
end tell

The works for both Safari and Safari Technology Preview, which is nice.

Running this in, say, macOS Script Editor.app will remotely click the referenced menu item and display the expected conformation dialog.

Safari Add bookmarks dialog

Note: you could add additional AppleScript to manipulate and/or dismiss the confirmation dialog; I'll leave such changes as an exercise for the reader.

Finally, we need to add a shortcut key to access the menu. There are a number of different ways to do this and choice will depend on personal preference and/or software at hand:

  1. Alfred: workflow with NSAppleScript Action + Hotkey Trigger
  2. Keyboard Maestro: AppleScript Action + hot key trigger
  3. Automator: Service + Shortcut key via macOS System Preferences (see this answer)
  4. FastScripts Lite (free for up to 10 shortcut keys): shortcut

Note: it is no longer possible to assign a shortcut key to an item in the macOS Script menu, so I've not included that method of calling the script.

The last of the options above, FastScripts Lite, is the one that comes for free, and also the easiest method, so I'll use it in this answer.

Enter the above "activate...end tell" snippet into macOS Script Editor.app and save the resulting script to ~/Library/Scripts/Applications/Safari/Save Bookmarks for Current Window Tabs.scpt.

In FastScripts Lite.app you'll now have a menu item that is effectively an alias for Safari's "Add Bookmarks for These X Tabs..." menu item, and that can be assigned a shortcut key using FastScripts Lite.app > Preferences > Script Shortcuts.

FastScripts Lite script shortcuts preferences