Hammerspoon / hammerspoon

Staggeringly powerful macOS desktop automation with Lua
http://www.hammerspoon.org
MIT License
11.97k stars 580 forks source link

focusing Finder-windows doesn't work across multiple screens #304

Open oskarols opened 9 years ago

oskarols commented 9 years ago

Basic test case: have 2 screens, finder on one screen, and have the other screen focused.

Try running:

hs.application.launchOrFocus("Finder")

Finder will not be focused.

Behind the scenes Finder is running a non-standard window, it seems to be the case that when you do not have an actual Finder window on the same screen then focus instead goes to this hidden window.

Edit: Actually, it's more curious than this. Even if you manually filter out the non-standard window and focus only the actual Finder window — it will still not receive focus, unless you trigger :focus() three times.

For the time being, this seems to do the trick as a quick workaround:

hs.hotkey.bind({"cmd"}, "§", function()
  local app = hs.appfinder.appFromName("Finder")

  local function hasRealWindows(application)
    local windows = application:allWindows()
    windows = hs.fnutils.filter(windows, hs.window.isStandard)
    windows = hs.fnutils.filter(windows, hs.window.isVisible)
    return #windows ~= 0
  end

  if app == nil or not hasRealWindows(app) then
    hs.application.launchOrFocus("Finder")
  else
    windows = app:visibleWindows()
    windows = hs.fnutils.filter(windows, hs.window.isStandard)

    if windows[1] then
      -- yes, this is actually needed 3 times, else you have to
      -- press the hotkey twice
      windows[1]:focus()
      windows[1]:focus()
      windows[1]:focus()
    end
  end
end)
cmsj commented 9 years ago

Oddly, this seems to not be reproducible from the Hammerspoon Console (on 10.10.3). I have a Finder window on my second monitor, under several other windows, the Console open on my primary monitor, with focus, and running hs.application.launchOrFocus("Finder") pops the Finder window to the front of the second monitor and gives it focus.

oskarols commented 9 years ago

@cmsj I think I was able to do the same thing, as long as the console was created in the same window as a Finder window.

cmsj commented 9 years ago

@oskarols good point, I was able to reproduce this using the console if it was created on a different screen to a Finder window.

So it definitely looks to me like this is handing focus to the desktop, which is a "special" Finder window.

The question is, what should Hammerspoon be doing about this? I'd really really like to avoid having lots of whitelisting/blacklisting in the APIs. I think I'd prefer to just add a note to the API docs that launchOrFocus() may behave in unusual ways for apps that have "special" windows.

Thoughts?

lowne commented 9 years ago

At least over here, the system shortcut `cmd-`` always cycles through (what I presume to be, since the icons mysteriously vanished) the desktop, so it seems indeed a case of working-as-intended. Then again I don't know what the point of this behaviour is (so IMHO this might deserve an exceptional 'fix' for launchOrFocus) - I mean, if Finder at least triggered the 'show desktop' action, it might make more sense - but there must probably be some (historical) reason.