1

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])

sanders
  • 10,794
  • 27
  • 85
  • 127

1 Answers1

0

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)
HarsilSPatel
  • 1
  • 1
  • 3