Script for getting windows from beyond display border

applescriptdesktopdisplaywindow-manager

I have a problem with my new monitor. Often when I unlock the screen, there are windows that are hanging beyond desktop border. It is quite irritating having to drag them back to the window centre manually.

Luckily, I've found a script that can do it for me:
https://github.com/bradchoate/gather-windows/blob/master/Gather%20Windows.applescript

Unfortunately, it only works from the script editor window. When I try to run it by clicking on its name in the script menu bar it does nothing. I tried saving it as an application and running it, to the same effect.

Anyone can help me with that?

Best Answer

I went to the URL in your question, click the Raw button, copied the code and pasted it into Script Editor, where I saved it as an application named: Gather Off-Screen Windows.app

I then went into System Preferences > Security & Privacy > Accessibility, as show in the image below, and added the Gather Off-Screen Windows.app.

I moved some windows off-screen, then ran the Gather Off-Screen Windows.app, and it completed without error and moved the off-screen windows fully onto the screen.

Insert Image 1

Now while I added the Gather Off-Screen Windows.app to System Preferences > Security & Privacy > Accessibility after having saved it, had I not done so, I would have expected to see the following dialog boxes when I ran the app, (which sometimes one or both may not appear):

Insert Image 2

Insert Image 3

That said, let me say that while I have not studied the script in depth to have a through understanding of it mechanics, nonetheless, I see what I consider some poor coding in this instance in the beginning and would change the following line of code from:

repeat with proc in application processes

To:

repeat with proc in (application processes whose visible is equal to true)

The reason this change makes sense to me is, as an example, at the moment I have 38 application processes running of which only 10 are visible and therefore it's seems senseless to query further the 28 application processes that aren't even visible.

Also note that I even minimized some of the off-screen windows before running the app and when I unminimized them, they were then completely on screen. So the visible property in this instance is about the process not minimized off-screen windows that while minimized are not visible.