How to Activate Finder Tabs with Shortcuts Cmd+1, Cmd+2

Is it possible that tell front window of application "Finder" to set current tab to tab 1 by "cmd+1"?
And active tab 2 by 'cmd+2'.
And active tab 3 by 'cmd+3'.

I found a same topic in https://stackoverflow.com/questions/26523153/change-active-finder-tab

But i copy its code to my Applescript action, and it cannot run normally.

(In other apps, i can implement it by remapping shortcuts for selecting tab. But Finder is specially without corresponding original shortcuts)

The short answer is: No.

At least not as you would expect Finder Tabs to be numbered.

Turns out that the Finder treats Tabs like Windows, and whichever Tab is currently active/frontmost is returned as radio button 1, even if it is physically the 2nd or 3rd or nth tab.

So CMD 1 will not always activate the first physical tab.

I'm looking for a workaround, but I don't have much hope.

You could click on the first tab (which has a fixed location), and then Show Next Tab N-1 times.

It sounds not good. Mouseless may be the ideal goal.

Similarly, imitating a click may be a way.

/**
 * @param offsetTop: `The offsetTop of a tab in a Finder window` is always a constant value.
 *   Artificially set this constant config.
 * @param offsetLeft: OffsetLeft decided by the realtime whole width of the Finder window and the total of tabs.
 *   For example:
 *   cmd+1:  offsetLeft ≈ 1 / the total of tabs * whole width
 *   cmd+2:  offsetLeft ≈ 2 / the total of tabs * whole width
 */
function triggerAClick (offsetTop, offsetLeft) {
    // imitate a click in the frontmost Finder window at the offset position
}

And cite from @JMichaelTX , it may cannot get the index of the active tab. Is it possible that get the total number of tabs?

offset_position

A post was merged into an existing topic: Can You Move/Arrange a Finder Tab With Applescript?

Hey @az22c,

This is a complex problem and what worked years ago on Sierra no longer works in later versions of macOS.

I took a shot at the problem, but I'm still on Sierra and can't make certain it works on High Sierra or later. Maybe @JMichaelTX will test these for me at least on Mojave.

There are 5 macros that handle tabs 1-5. (Of course you can add more if needed.)

The sort code in the AppleScript is courtesy of @ComplexPoint on the LNS forum:

Download the file and double-click it in the Finder to install.

-Chris


Finder Macros -- Select Tab by Number.kmmacros (55.8 KB)
image

1 Like

https://forum.keyboardmaestro.com/t/can-you-move-arrange-a-finder-tab-with-applescript/3396/11?u=az22c

@ccstore Thanks for your job. It's awesome.
The good new is that the script works well in MacOs 10.13.6 High Sierra.
But the Finder, as for mine, responses to the script with a short delay.

I will try to read the source code later and try to find out why.

Hey @az22c,

That won't help you, because it is Keyboard Maestro itself that's the problem.

Keyboard Maestro uses osascript to run AppleScripts, and this is inherently a trifle slow.

You can improve upon this by making the AppleScript actions run a compiled AppleScript instead of a text-script, but it will still be slow.

If you want to run them at maximum speed you'll need to install FastScripts, and run the scripts from it.

I use FastScripts to run nearly all my (hundreds) of AppleScripts for that very reason and only use Keyboard Maestro to run AppleScripts when they are required as part of a larger macro.

The free version of FastScripts gives you an AppleScript menu with unlimited scripts, however it only allows you to assign a keyboard shortcut to 10 scripts. To unlock unlimited keyboard shortcuts you'll have to buy the paid version.

I've run Keyboard Maestro and FastScripts concurrently since about the beginning of 2004.


I just tested the scripts with FastScripts, and they're significantly faster then when run from Keyboard Maestro (although NOT instant). For that matter directly clicking in the Finder's UI isn't instant either.

Then again, my hardware is old -- a 17" Mid-2010 i7 MacBook Pro with only 8 GB of memory.

-Chris

1 Like

Oh. I saw something like sorting commands, map manipulations, unzipping commands and etc., and wonderer whether the code needed to be so complex.

But now, here with your professional commentary, i might give up the trial of modifying.

Waiting for more testing ~~ And thanks.

Vanilla AppleScript doesn't have the ability to sort lists of lists, so I borrowed some long-winded code to do this in my script.

It is very fast and does not affect the speed of the script.

The speed problem comes from running the script from Keyboard Maestro and the limitations of the macOS.

When run from FastScripts the script is nearly instant.

-Chris

2 Likes