OSX Lost Windows 
Sometimes I run applications with multiple monitors on OSX and unplug the external monitor and a few applications will mysteriously disappear. They are actually still running, but for some reason they don't move over to the active screen (e.g. the laptop's LCD).

To make matters worse, exiting and reopening the application doesn't work either. They apparently re-spawn to their last location which doesn't exist anymore. Plugging back in the external monitor will bring back the application, but access to my external monitor isn't always possible when I need to use a "lost" application.

A few applications will allow you select Window from the File Menu and choose something like "Tile" which will work for recovery. However, I've run across a few applications that don't have any roundabout solution like that to move the lost window. (SQLiteStudio has been the worst offender for me.)

A bit of Googling found a solution with various similar AppleScripts that will check all windows and make sure they are visible and if not move them into view. This requires enabling System Preferences->Universal Access->[checkbox]Enabled Access for Assistive Devices to allow for this automation (potentially a security risk, but I digress).

Here is an example script: Gather Lost Windows Script

This sounded great and I gave it a shot, but was immediately greeted with an error: "Access for assistive devices is disabled" when running the script. This was a bit perplexing because I had already checked that it was in fact enabled. I proceeded to play around with checking and unchecking the box in the Universal Access panel but nothing worked.

I just assumed my laptop was somehow corrupted. However, Googling didn't indicate anyone else had a similar problem. So I did a little bit of script debugging and realized that some windows were successfully being tested for being "off screen" before the error finally triggered on one particular process. Toggling OFF the "access for assistive devices" would cause the very first process to trigger the error, while ON something like the 5th process would trigger it.

I came to realize that the error that AppleScript returns is actually a permissions problem of some sort, just with a very poor and misleading error message. So I wrapped a TRY block around the offending code to allow the script to go to completion and it worked for recovering my SQLiteStudio window at least. I'm not sure if other applications that get stuck offscreen may fail (due to the mystery permissions error), but I can at least gather my main offending application. My modified script follows (based off of the script linked above). Feel free to use it! And thanks to the original author for getting me most of the way there.

-- Example list of processes to ignore: {"xGestures"} or {"xGestures", "OtherApp", ...}
property processesToIgnore : {}

-- Get the size of the Display(s), only useful if there is one display
-- otherwise it will grab the total size of both displays
tell application "Finder"
set _b to bounds of window of desktop
set screen_width to item 3 of _b
set screen_height to item 4 of _b
end tell

tell application "System Events"
set allProcesses to application processes
set _results to ""
repeat with i from 1 to count allProcesses
set doIt to 1
repeat with z from 1 to count processesToIgnore
if process i = process (item z of processesToIgnore) then
set doIt to 0
end if
end repeat

if doIt = 1 then
tell process i
try
repeat with x from 1 to (count windows)
set winPos to position of window x
set _x to item 1 of winPos
set _y to item 2 of winPos

if (_x < 0 or _y < 0 or _x > screen_width or _y > screen_height) then
set position of window x to {0, 22}
end if
end repeat
on error errmesg number errnumber

end try
end tell
end if
end repeat
end tell



[ add comment ] ( 4 views )   |  [ 0 trackbacks ]   |  permalink

<Back | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | Next> Last>>