Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
oskarols opened this issue May 29, 2015 · 4 comments
Open

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

oskarols opened this issue May 29, 2015 · 4 comments

Comments

@oskarols
Copy link
Contributor

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)
@oskarols oskarols changed the title launchOrFocus on Finder doesn't work with multiple screens focus Finder windows doesn't work across multiple screens May 29, 2015
@oskarols oskarols changed the title focus Finder windows doesn't work across multiple screens focusing Finder-windows doesn't work across multiple screens May 29, 2015
@cmsj
Copy link
Member

cmsj commented Jun 1, 2015

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
Copy link
Contributor Author

oskarols commented Jun 1, 2015

@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
Copy link
Member

cmsj commented Jun 15, 2015

@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
Copy link
Contributor

lowne commented Jun 16, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants