Select menu item by position

I'm switching my default browser from Safari to Chrome for boring reasons.

One feature I miss from Safari: I can type Command-1 to open the first bookmark in my bookmarks toolbar, and the same goes for other single-digit numbers. Chrome doesn't have a way to do this, so I want to duplicate the functionality with KM.

Of course, I could create macros for each individual bookmark — but sometimes I add a new bookmark to a high-level position, and I want the keyboard shortcuts to update automatically. So I don't want to hard-code 10 specific URLs to keyboard shortcuts in KM. And I use two different Chrome profiles, with different bookmarks in each, so the first/second/third bookmark might be different depending on circumstances.

Chrome has a Bookmarks menu that lists the same bookmarks that are in the bookmarks toolbar, in the same order (and it changes appropriately when I switch profiles). So a KM action like "open the fourth item in the Bookmarks menu" would work. I can't find a KM-native way to do that, but I would imagine it's possible with AppleScript. Any help appreciated!

If you have to look at AppleScripting the UI rather than interacting directly with the app though its AS dictionary commands you are often better off driving things with KM actions. In this case you could select the "Bookmarks" menu item, down-arrow the correct number of times, then hit Return.

The only gotcha is that you have to check if the "Bookmark All Tabs" menu item is selectable or not, and adjust the number of down-arrows accordingly.

Change the number in the last "Repeat" to match the bookmark's position in the list:

4th Bookmark in Chrome.kmmacros (5.3 KB)

Image

Ah, good thought, that would work. I had assumed that AppleScript has a way to do it without UI scripting — something like:

-- get the menu named "Bookmarks" from the application named "Google Chrome"
-- get the list of menu items within that menu
-- get the nth menu item on that list
-- activate that item

But maybe not?

That is UI scripting -- menus and menu items, rather than acting directly on the app.

But I take back what I said earlier -- I use Safari rather than Chrome, and it turns out that Chrome is a lot better (as in "you can actually do things"!) when it comes to AS and bookmarks.

So if you want to open the nth bookmark in a new tab in the currently active Chrome window:

tell application "Google Chrome"
	-- Put the bookmark index here. '1' is first in the list, '2'is second, etc
	set n to 4
	make new tab at window 1 with properties {URL:(URL of item n of (every bookmark item of bookmark folder "Bookmarks Bar"))}
end tell

Adjust n to suit -- you could even pass it in from an AppleScript dialog or a KM "Prompt User for Input" action.

If you want any changes, just shout.

Using the script will be less distracting than driving the menus with KM, but it might not be any quicker since it takes KM time to instantiate an AppleScript instance then run the script.

1 Like

Nice! Works perfectly, thank you.

If anyone wants to import this feature from Safari to Chrome, here's the macro:

Numbered Bookmarks.kmmacros (26 KB)

1 Like