Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move specific application to a specific screen

Tags:

hammerspoon

I have read over here how to move an application to a specific screen. In my case I have a variation of this. In this case I want to open for example Todoist on a specific screen. This code below opens Todoist but on my wrong screen.

How can I solve this?

  local screens = hs.screen.allScreens()
  hs.application.open("Todoist")
  local win = hs.application:findWindow("Todoist")
  win.moveToScreen(screens[1])

like image 513
sanders Avatar asked Jan 22 '26 03:01

sanders


1 Answers

findWindow() is an instance method, so it cannot be called directly as hs.application:findWindow(). To properly call this method, you must create an instance of the hs.application class and then call findWindow() on that instance.

The following snippet should work, although you may need to adjust the wait time (and the screens index). It is generally recommended to use hs.application.watcher to watch for when an app has been launched, rather than using a timer.

local notes = hs.application.open("Notes")
hs.timer.doAfter(1, function()
  -- `notes:mainWindow()` will return `nil` if called immediately after opening the app,
  -- so we wait for a second to allow the window to be launched.
  local notesMainWindow = notes:mainWindow()
  local screens = hs.screen.allScreens()
  notesMainWindow:moveToScreen(screens[1])
end)
like image 73
HarsilSPatel Avatar answered Jan 25 '26 18:01

HarsilSPatel