Moving Application Windows in OS X via Apple Script

May 8th, 2008 · 4 Comments


Those of you lucky enough to have multiple monitors hooked up to you MacBook or MacBook Pro when you work may have come across this issue. When you remove that extra monitor some applications don't get re-positioned back on to your laptop display. I had this with one particular application that I use frequently, KeePassX. Well somewhere along the way I dug up this use full terminal/apple script that solves the issue.

  1.  
  2. property processesToIgnore : {}
  3. tell application "Finder"
  4. set _b to bounds of window of desktop
  5. set screen_width to item 3 of _b
  6. set screen_height to item 4 of _b
  7. end tell
  8. tell application "System Events"
  9. set allProcesses to application processes
  10. set _results to ""
  11. repeat with i from 1 to count allProcesses
  12. set doIt to 1
  13. repeat with z from 1 to count processesToIgnore
  14. if process i = process (item z of processesToIgnore) then
  15. set doIt to 0
  16. end if
  17. end repeat
  18. if doIt = 1 then
  19. tell process i
  20. repeat with x from 1 to (count windows)
  21. set winPos to position of window x
  22. set _x to item 1 of winPos
  23. set _y to item 2 of winPos
  24. if (_x < 0 or _y < 0 or _x > screen_width or _y > screen_height) then
  25. set position of window x to {0, 22}
  26. end if
  27. end repeat
  28. end tell
  29. end if
  30. end repeat
  31. end tell
  32.  

I don't recall where I may have dug this up, but if you think you know, please let me know in the comments so I can give credit.

How about donating a coffee, beer or even a round of beers for my efforts here? Thats around £2.50 for a coffee or a pint or about £10 for a round of beers. Cheers!

Tags: BitTube Thoughts

4 responses so far ↓

  • 1 Tink // May 8, 2008 at 11:41 pm

    I come across this issue all the time. Changing the display res, then changing it back always sorts it.

  • 2 Dave // May 16, 2008 at 2:41 pm

    Actually not always, KeePass X is one app that changing screen resolution does not work :(

  • 3 Eric // Apr 6, 2009 at 4:50 pm

    I’ve tried a number of scripts that claimed to solve this problem, but yours is the only one I’ve found to work with KeePassX. Excellent, many thanks.

  • 4 Fix for KeePassX window positioning off screen after changing monitors « Clinical Bootstraps // Nov 20, 2009 at 8:02 pm

    [...] came across a much nicer solution on bittube. The solution is great, but the source found there is a bit tricky to copy & paste so [...]

Leave a Comment