stephancasas / stephan-hates-osascript

A collection of "useful" AppleScript and JXA utilities.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Request for the move window JXA script

lfilho opened this issue · comments

Hey thanks a lot for your work! I just posted a comment on Stackoverflow but linking it here as well in case you don't follow the notifications in there:
https://stackoverflow.com/questions/9775268/how-to-move-a-window-to-a-certain-desktop-with-applescript/73561196#comment131568710_73561196

I noticed your script only works for the frontmost window. Would you kindly be able to share examples how to make it work for several apps? My goal is to run a script on startup and place "My App A" on Space 1, "My App B" on Space 3, etc etc. Do you think your script would support that?

Thanks a lot!

Hi,

I've replied to your StackOverflow question, but will also provide detail here because there's more space to type. You can get a window's canonical name via the System Events process:

Application('System Events').applicationProcesses.byName(
  'Brave Browser',
).windows();

After you have the name, you'd provide that name to WindowBrokerPosition() as done on this line, and then follow through with the same script logic — running Cursor.move() and Cursor.drag() in similar fashion.

For multiple windows, you'd have to do this in a loop one-by-one, obviously because the cursor can only drag one object at a time, but also because the broker positions will change as the current Mission Control space becomes less occupied.