Apple - Script for getting windows from beyond display border
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.
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):
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.
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
- Launch Automator (usually found within your Applications folder)
- Go to File > New
- Select Service and click Choose
- In the top right hand of the window that appears, ensure that "No input" is selected from the Service receives drop-down list
- Ensure the In drop-down list is showing "Any application"
- In the second column containing the long list, find "Run AppleScript" and double-click on it
- This will add the Run AppleScript window on the right
- Now, replace the (* Your script goes here *) with the script you want
- Save the service using a meaningful name (e.g. Gather windows).
- 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
- Go to System Preferences > Keyboard > Shortcuts
- Select Services from the sidebar
- Find your service (it'll be in the list on the right-hand side)
- Add a shortcut by double clicking on the service name
- Now go to System Preferences > Security & Privacy > Privacy
- Select Accessibility in the sidebar
- Click on the + sign (you may need to unlock the padlock if it's locked)
- Add Automator.
- Add Finder (to find this you will need to navigate to /System/Library/CoreServices/Finder.app).
- 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.