Safari – How Do You Determine if the Current Open Tab Has More History When Selecting "Back"

Hi,

What I want to do:

In Safari's active tab selecting "back" cmd[ will close tab when there is no longer any history to go back to (instead of just ignoring the back request when there is no more history within that tab).

So my question is: how do I determine when the tab is as far back in history for that tab and trigger the macro when this happens?

Thanks so much

Devon

Hey Devon,

You can use the If action to determine if the Back menu item is enabled (indicating there is a "history" and you can indeed back up a page), and if so, trigger that menu item.

If it is NOT enabled (indicating there is no previous page to back up to), then you can trigger a different menu item to close the front tab.

See the attached macro for an example.

-Chris

Safari- Go back or close tab.kmmacros (3.5 KB)

Macro screenshot (click to expand/collapse)

EDIT: Hey Devon (@devon) perhaps I misunderstood your post... it sounds like you don't want to close the front tab if there's no history... if that's the case just remove the close tab menu item action in the If action.

Wha OS are you running though? Because I'm on Big Sur and ⌘[ doesn't close the tab at all... even if there's no more history. Did you perhaps do some custom key binding in your system preferences or through another app?

Hey @devon,

I've never seen Safari do that, and it would be a very bad UI decision for even Apple to make.

On any version of macOS I've seen the back keyboard shortcut will simple stop when there is no more history.

The current tab is not closed.

Could you be doing this with either a macro or a Safari extension?

If not then please provide:

  • The version of macOS you're using.
  • The version of Safari you're using.

-Chris

Hi Chris,

Thanks so much for your response. I am out of the office but will try later this week and let you know. Thanks for taking the time

Devon

Hi Chris,
You may have misread my post. It starts with "what I want" . The second half is what is current and fully agrees with your comments. Thanks

Devon

1 Like

Gotcha. Well assuming I understand now what you’re trying to achieve, the macro I built incidentally does just that.

If there's history, it will go back one page. If there isn't, it will close the tab.

A note however, if there is no other tab open (in other words you have only one page in the browser window), the Close Tab menu item doesn’t exist; only the Close Window menu item. So this macro won't do anything else at that point. If you want it to close the tab or the window, then there's a solution to have it do that depending on which menu item exists. I imagine you can figure that part out from here, but if you do run into issues figuring it out just let me know and I can help out with that too.

-Chris

Hi Chris,

Thanks again. I had a chance to run the macro and it works as initially desired. I am now setting up a dialog box which asks if you want the tab to close with yes / no buttons and yes button as default. Now I am trying to figure out when the dialog box opens, how to remember current position of mouse, move mouse over the yes button, then when dialog box closes, move cursor back to original position.

Quick question: I know there is always different ways to do things but I am wondering if you choose the option of musing menu and names vs shortcut. For example, going back in history: cmd+[ vs menu selection process you used. Just curious.

Thanks again,

Devon

Hey Devon, that's relatively easy and is sort of built-in to Keyboard Maestro. See the example below. Place the first action before the dialog prompt, and the second action after the dialog prompt.

Set current mouse position and return.kmmacros (2.1 KB)

Macro screenshot (click to expand/collapse)

Yes, menu items rarely if ever change position. Shortcuts can be changed or overriden by system preferences or other apps. The menu items also have the advantage of being accessible even when the app is not at the front, whereas a shortcut has to have the app at the front for it to work. So typically menu items are a more reliable method of interaction.

Hope all this helps!

-Chris