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?

  • This is a great question. I have this problem too! Thanks for sharing the script you found. – T.H. Apr 25 '17 at 22:28

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


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.

  • Thank you for the answer and the tips on modifying the script. Would you have any suggestions on how to modify the script to work with multiple monitors? Right now it gathers the window on multiple monitors only when the window is off-screen at a border that is not adjacent to another monitor. – autoencoder Apr 25 '17 at 14:27
  • @autoencoder, As noted in my answer, "while I have not studied the script in depth to have a through understanding of it mechanics", I'll have to study the code a bit to see if I can address the issue with multiple monitors, although I can only test with two monitors. – user3439894 Apr 25 '17 at 14:44
  • That would be great! Let me know if you come up with something. I looked at the script and it gets dimensions for the whole desktop and checks that each window is within those dimensions. It would be nice for the script to iterate between individual monitors with their corresponding windows. Unfortunately, I have zero experience with Apple Script and don't know where to start. – autoencoder Apr 26 '17 at 15:21

I would use Automator to create a service that runs the Apple Script. And, as a bonus, you could also use System Preferences to assign a keyboard shortcut to the service.

Below are the steps involved for each.

1. Create a service using Automator

  1. Launch Automator (usually found within your Applications folder)
  2. Go to File > New
  3. Select Service and click Choose
  4. In the top right hand of the window that appears, ensure that "No input" is selected from the Service receives drop-down list
  5. Ensure the In drop-down list is showing "Any application"
  6. In the second column containing the long list, find "Run AppleScript" and double-click on it
  7. This will add the Run AppleScript window on the right
  8. Now, replace the (* Your script goes here *) with the script you want
  9. Save the service using a meaningful name (e.g. Gather windows).
  10. Quit Automator

Now I would test the service by moving a couple of windows off screen. You can run the service from any application by going to the Services list within any Application menu (e.g. Finder > Services, Safari > Services, TextEdit > Services, etc) and select the service you just created.

Assuming it works, you can also create a keyboard shortcut to run it.

2. Creating your shortcut

  1. Go to System Preferences > Keyboard > Shortcuts
  2. Select Services from the sidebar
  3. Find your service (it'll be in the list on the right-hand side)
  4. Add a shortcut by double clicking on the service name
  5. Now go to System Preferences > Security & Privacy > Privacy
  6. Select Accessibility in the sidebar
  7. Click on the + sign (you may need to unlock the padlock if it's locked)
  8. Add Automator.
  9. Add Finder (to find this you will need to navigate to /System/Library/CoreServices/Finder.app).
  10. Exit your System Preferences.

Now you should be able to use the keyboard shortcut to run the service.

NOTE: I am aware that for some users keyboard shortcuts do not seem to work even though they've been assigned to a service. This is a whole other topic, but if this happens in your case, at least you can run the service from within any application you're using.

Let me know how you go.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .