Sometimes when calling for Finder, it doesn't switch to focus of the window

Very often when I switch between various applications and finder, when I switch to Finder, the focus is brought to Finder as can be seen in the menu bar :

But I can't straight away use my arrow keys to move within the file system because the window itself is not in focus. Is there a way I can make sure that a window is always in focus when I switch to Finder in this way :

Thank you. Perhaps some macro with trigger of Finder opening and maybe some applescript is what I need.

I’ve been searching for an answer for this for a long time. While I was composing a response to you, a thought came to mind (strange how the mind works sometimes).

After wandering down a few rabbit holes, and trying a lot of different things, this appears to work:

tell application "Finder"
    select the front window
end tell

Now, I’m the first to admit that this makes no sense, yet I believe it works. You can test it by clicking on the desktop, then triggering the above AppleScript.

@ccstone - Do you think I’ve finally found the answer? Let me know what you think - I know you’ll think of scenarios I haven’t considered, so I’d really like your input on this. Thanks.

1 Like

I had this exact problem some time ago, and Chris Stone (@ccstone) was kind enough to provide the solution. Just add a “Execute Applescript” Action AFTER you have activated the Finder in KM.

###Script

(*
=======================================================
  HOW TO MAKE FINDER WINDOW FRONTMOST WHEN YOU HAVE MULTIPLE SCREENS
=======================================================
  This will always make Finder Window frontmost, regardless of number of screens

DATE:       Sat, Aug 22, 2015
AUTHOR:   Chris Stone (@ccstone on KM forum)
REF:        https://forum.keyboardmaestro.com/t/how-to-close-finder-tab/1894/13

*** The Issue this solves ***

  • Normally an AS or KM action to "Activate" the Finder would bring its windows to the front
  • But when you have multiple moniter setup, and you run the script/action on a different screen than the Finder is on, then the Finder is make "active", but its frontmost window is not actually fully active/selected.
  • So, if you then try to (via script) do something with the Finder, like close the current Tab, it fails.

*)

------------------------------------------------------------
set myDesktop to path to desktop
------------------------------------------------------------
tell application "System Events"
  if quit delay ≠ 0 then set quit delay to 0
  tell application process "Finder" to set frontmost to true
end tell
------------------------------------------------------------
tell application "Finder"
  if (insertion location as alias) = myDesktop then
    if (count of windows) > 0 then
      set index of window 1 to 1
    end if
  end if
end tell
------------------------------------------------------------
4 Likes