4/16/2007
AppleScript to get control over off-screen windows
As I mentioned before, one of my Mac OS X annoyances is the fact that sometimes I manage to get application windows off the screen. I think this happens mostly when I switch from using two monitors to one. Anyhow, it’s a pain in the butt, because in OS X, you can only move a window by dragging its titlebar and you can only resize it using the lower-righthand corner. Thus, you can sometimes get windows in a position where they are mostly off-screen and you can’t move or resize them. Expose doesn’t work because it will let you select but not move or resize.
Well, in the previously mentioned post, someone named Hendrik commented and posted an AppleScript to move the frontmost app back to the top-left corner of the screen. Great, except that it didn’t work on my 10.4.9 system. I kept getting the AppleScript error “NSReceiverEvaluationScriptError: 4″. I searched around and tried a few suggestions like making sure that System Preferences | Universal Access | Enable access for assistive devices was enabled (which it was). No luck, so I tabled it for a while. Then today while cleaning out my email, I came across Hendrik’s comment again and decided to see if I could get it working.
After a bit of trial and error, I got it.
I’m no AppleScript expert, but I believe that the script had been failing, because when it tried to get the frontmost application, it was getting itself and then the script exited before it could receive the messages from itself. My solution was to add some code at the beginning of the script to hide the frontmost application (the script) so that when it queried again for the frontmost application, it would get the one that was in front before the script was launched.
Here’s the script:
tell application "System Events"
tell (a reference to (first process whose frontmost is true))
set visible to false
end tell
end tell
tell application "System Events"
set FrontApplication to ¬
(get name of first process whose frontmost is true) as string
--display dialog "FrontApplication = " & FrontApplication
try
tell process FrontApplication
set position of window 1 to {0, 22}
--window 1 is always the frontmost window.
end tell
on error
display dialog "Exception - FrontApplication = " & FrontApplication
end try
end tell
If you don’t want to type, here’s a download: Reposition off-screen window.scpt
Del.icio.us
Digg
Reddit
Technorati
Possibly related posts
Comments
Leave a reply



Interesting. It probably depends on how you launch the script. I used Quicksilver to assign a keyboard combination to the script. I saved the script as a .scpt file and chose ‘Run’ as the action in Quicksilver. Which works just fine.
But I just tried saving my original version of the script as an application and run it by double-clicking and did indeed get the same cryptic error message you mention.
Glad that you could fix it and make it work for you (and others).
Awesome!!!
I was using DivX Converter and it was off screen and I could not get back on screen. Your script worked great for me!
DUDE…seriously, I appreciate this so much! I had the same issue as the poster before me, DivX Converter.
Thanks!!
Your solution worked great for me, and the previous scripts I’d tried failed.
Cheers Marc, appreciate getting my VirtualBox window back.
OS X needs UltraMon Mac yo. Bad.