3

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

2 Answers 2

2

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) 
Sign up to request clarification or add additional context in comments.

Comments

0

Thanks for HarsilSPatel for inspiring my solution. In my case I wanted to re-arrange certain apps into specific screens.

For example when coming home from the office, the screens in the OS are different, and the apps would open in different screens when you go back and forth.

hs.application.enableSpotlightForNameSearches(true) function rearrangeWindows() return function() local screens = hs.screen.allScreens() local leftScreen = screens[2] local rightScreen = screens[3] local mainScreen = screens[1] -- Left moveWindowToScreen("Warp", leftScreen, true) -- Main moveWindowToScreen("Finder", mainScreen, false) moveWindowToScreen("Visual Studio Code", mainScreen, false) -- Right moveWindowToScreen("Chrome", rightScreen, true) moveWindowToScreen("Notion", rightScreen, true) hs.alert.show("Rearranged apps", {}, hs.screen.primaryScreen()) end end function moveWindowToScreen(app, screen, maximize) local win = hs.application.find(app) if win == nil then return end -- print("Found window for " .. app) local mainWindow = win:mainWindow() if mainWindow == nil then return end -- print("Moving " .. app .. " to " .. screen:name()) mainWindow:moveToScreen(screen, true, true, 0) if maximize then mainWindow:maximize(0) end end hs.hotkey.bind({"alt", "cmd"}, "pad0", rearrangeWindows()) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.