Posted By

bradchoate on 09/27/08


Tagged

applescript


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

bradchoate
wdso
atr000
tmbrenneke
mathwrath


Gather Windows


 / Published in: AppleScript
 

A little script to gather any application windows that are partially offscreen in some way and shift them back to the desktop. This is particularly useful if you use a MacBook with an external display.

  1. tell application "Finder"
  2. -- get desktop dimensions (dw = desktop width; dh = desktop height)
  3. set db to bounds of window of desktop
  4. set {dw, dh} to {item 3 of db, item 4 of db}
  5. end tell
  6.  
  7. tell application "System Events"
  8. repeat with proc in application processes
  9. tell proc
  10. repeat with win in windows
  11. -- get window dimensions (w = width; h = height)
  12. set {w, h} to size of win
  13.  
  14. -- get window postion (l = left of window; t = top of window)
  15. set {l, t} to position of win
  16.  
  17. -- nh = new window height; nw = new window width
  18. set {nh, nw} to {h, w}
  19.  
  20. -- window width is bigger than desktop size,
  21. -- so set new window width to match the desktop
  22. if (w > dw) then ¬
  23. set nw to dw
  24.  
  25. -- window height is bigger than the desktop size (minus menu bar),
  26. -- so set new window height to be desktop height - 22 pixels
  27. if (h > dh - 22) then ¬
  28. set nh to dh - 22
  29.  
  30. -- r = right coordinate of window; b = bottom coordinate of window
  31. set {r, b} to {l + nw, t + nh}
  32.  
  33. -- nl = new left coordinate; nt = new top coordinate
  34. set {nl, nt} to {l, t}
  35.  
  36. -- left coordinate is off screen, so set new left coordinate
  37. -- to be 0 (at the left edge of the desktop)
  38. if (l < 0) then ¬
  39. set nl to 0
  40.  
  41. -- top coordinate is above bottom of menu bar (22 pixels tall),
  42. -- so set new top coordinate to be 22
  43. if (t < 22) then ¬
  44. set nt to 22
  45.  
  46. -- right coordinate extends beyond desktop width,
  47. -- so set new left coordinate to be desktop width - window width
  48. if (r > dw) then ¬
  49. set nl to dw - nw
  50.  
  51. -- bottom coordinate extends beyond desktop height,
  52. -- so set new top coordinate to be desktop height - window height
  53. if (b > dh) then ¬
  54. set nt to dh - nh
  55.  
  56. -- if we have calculated a new top or left coordinate, reposition window
  57. if (l ≠ nl or t ≠ nt) then ¬
  58. set position of win to {nl, nt}
  59.  
  60. -- if we have calculated a new height or width, resize window
  61. if (h ≠ nh or w ≠ nw) then ¬
  62. set size of win to {nw, nh}
  63. end repeat
  64. end tell
  65. end repeat
  66. end tell

Report this snippet  

You need to login to post a comment.