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

previous window screen name #1424

Open
eugenetaranov opened this issue May 15, 2017 · 2 comments
Open

previous window screen name #1424

eugenetaranov opened this issue May 15, 2017 · 2 comments

Comments

@eugenetaranov
Copy link

Hello,

would it be possible to obtain a screen name of the previous application window?

I tried to grab the window and screen in hs.application.watcher.deactivated but it does not seem to work.

Thanks

@asmagill
Copy link
Member

Does something like this work:

appWatcher = hs.application.watcher.new(function(name, event, app)
    if event == hs.application.watcher.deactivated then
        if app then
            local focusedWindow = app:focusedWindow()
            if focusedWindow then
                local screen = focusedWindow:screen()
                if screen then
                    print(app:name(), focusedWindow:title(), screen:name())
                else
                    print("unable to get screen for " .. tostring(focusedWindow:title()))
                end
            else
                print("unable to get focusedWindow for " .. tostring(app:name()))
            end
        else
            print("no application object returned for " .. tostring(name))
        end
    end
end):start()

I don't have multiple monitors available to me atm, but a quick testing looks like it's working on my machine... a few "headless" applications (i.e. no dock icon) seem to give it trouble with "no focused window for ..." but depending upon your usage this might be sufficient or you could try checking out hs.application:mainWindow or hs.application:visibleWindows instead of focusedWindow and see if they're more reliable.

@eugenetaranov
Copy link
Author

eugenetaranov commented May 25, 2017

@asmagill thanks so much, works like a charm!

I need to move mouse cursor from another screen every time I switch windows, in the meantime I got it working in different way - simply checking whether mouse pointer is located on another screen or not:

function applicationWatcher(appName, eventType, appObject)
    if (eventType == hs.application.watcher.activated) then
  		local win = hs.window.focusedWindow()

      -- move mouse pointer if it is located on different screen
      if (win:screen():id() ~= hs.mouse.getCurrentScreen():id()) then
  			local f = win:frame()
        center_x = f.x + f.w/2 + f.w * 0.2
        center_y = f.y + f.h/2 + f.h * 0.2
        hs.mouse.setAbsolutePosition(hs.geometry.point(center_x, center_y))
      end
      
    end
end
appWatcher = hs.application.watcher.new(applicationWatcher)
appWatcher:start()

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

2 participants