Mac: Tile all Windows of frontmost application


/ Published in: AppleScript
Save to your folder(s)

Put the script in the app "Spark" (download from http://www.shadowlab.org/Software/spark.php) and give it a shortcut


Copy this code and paste it in your HTML
  1. --tile windows of frontmost applications in a grid
  2. --this script is useful for
  3. --multiple window chatting
  4. --working side by side of several windows of the same app
  5.  
  6. --make need to make it as a stay open application later
  7. --for now assume that it is opened and closed per invokation
  8.  
  9. property horizontalSpacing : 10 -- sets the horizontal spacing between windows
  10. property verticalSpacing : 10 -- sets the vertical spacing between windows
  11. property maxRows : 2
  12. property maxCols : 2
  13.  
  14. on run {}
  15. local a
  16. set userscreen to my getUserScreen()
  17.  
  18. --display dialog (getFrntApp() as string)
  19. try
  20. set applist to getFrntApp()
  21. if length of applist = 0 then
  22. return
  23. end if
  24. set a to item 1 of getFrntApp()
  25. on error the error_message number the error_number
  26. display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
  27. end try
  28.  
  29. try
  30. tileScriptable(a, userscreen)
  31. on error the error_message number the error_number
  32. --display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
  33. try
  34. tileUnscriptable(a, userscreen)
  35. on error the error_message number the error_number
  36. display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
  37. end try
  38. end try
  39.  
  40. end run
  41.  
  42. on tileScriptable(a, screen)
  43. local i, c
  44. set i to 1
  45. tell application named a
  46. set theWindows to every window of application a whose visible is true and floating is false and ¬
  47. modal is false -- and miniaturized is false
  48. set c to count theWindows
  49. if c = 0 then
  50. return
  51. end if
  52. set tiles to calTileBounds(c, screen, 1)
  53. repeat with theWindow in theWindows
  54. my tileScriptableWindow(a, theWindow, item i of tiles)
  55. set i to i + 1
  56. end repeat
  57. end tell
  58. end tileScriptable
  59.  
  60. on tileUnscriptable(a, screeninfo)
  61. -- unscriptable app
  62. local i, c
  63. set i to 1
  64. tell application "System Events"
  65. set theWindows to (every window of application process a)
  66. --set theWindows to my filterUnscriptableInvisible(theWindows)
  67.  
  68. set c to count theWindows
  69.  
  70. if c = 0 then
  71. return
  72. end if
  73.  
  74. --display dialog screeninfo as string giving up after 5
  75. set tiles to my calTileBounds(c, screeninfo, 1)
  76. repeat with theWindow in theWindows
  77. --display dialog (class of visible of theWindow)
  78. my tileUnScriptableWindow(a, theWindow, item i of tiles)
  79. set i to i + 1
  80. end repeat
  81.  
  82. end tell
  83. end tileUnscriptable
  84.  
  85. on filterUnscriptableInvisible(ws)
  86. -- filter out from ws windows that are docked
  87. set newws to {}
  88. set docklist to getNamesDocked()
  89. --display dialog (docklist as string)
  90. repeat with theWindow in ws
  91. if name of theWindow is not in docklist then
  92. set end of newws to theWindow
  93. end if
  94. end repeat
  95.  
  96. --display dialog (count newws)
  97. return newws
  98. end filterUnscriptableInvisible
  99.  
  100. on getNamesDocked()
  101. tell application "System Events" to tell process "Dock"'s list 1
  102. set l to name of UI elements whose subrole is "AXMinimizedWindowDockItem"
  103. end tell
  104.  
  105. return l
  106. end getNamesDocked
  107.  
  108. on tileScriptableWindow(a, w, bound)
  109. tell application a
  110. set bounds of w to bound
  111. end tell
  112. end tileScriptableWindow
  113.  
  114. on tileUnScriptableWindow(a, w, bound)
  115. tell application "System Events"
  116. --display dialog (count position of w)
  117. set AppleScript's text item delimiters to " "
  118.  
  119. set position of w to {(item 1 of bound), (item 2 of bound)}
  120.  
  121. -- why the -5?
  122. set size of w to {(item 3 of bound) - (item 1 of bound) - 5, ¬
  123. (item 4 of bound) - (item 2 of bound) - 5}
  124. --display dialog (count properties of w)
  125. end tell
  126. end tileUnScriptableWindow
  127.  
  128. on calTileBounds(nWindows, screen, direction)
  129. -- return a list of lists of window bounds
  130. -- a simple tile algo that tiles along direction (current only 1=horizontal)
  131.  
  132. local nRows, nColumns, irow, icolumn, nSpacingWidth, nSpacingHeight, nWindowWidth, nWindowHeight
  133. set {x0, y0, availScreenWidth, availScreenHeight} to screen
  134. set ret to {}
  135.  
  136. set nRows to (nWindows div maxCols)
  137. if (nWindows mod maxCols) � 0 then
  138. set nRows to nRows + 1
  139. end if
  140.  
  141. if nRows < maxRows then
  142. set nSpacingHeight to (nRows - 1) * verticalSpacing
  143. set nWindowHeight to (availScreenHeight - nSpacingHeight) / nRows
  144. else
  145. set nSpacingHeight to (maxRows - 1) * verticalSpacing
  146. set nWindowHeight to (availScreenHeight - nSpacingHeight) / maxRows
  147. end if
  148.  
  149. repeat with irow from 0 to nRows - 1
  150. if nRows � maxRows and irow = nRows - 1 then
  151. set nColumns to nWindows - irow * maxCols
  152. else
  153. set nColumns to maxCols
  154. end if
  155. set nSpacingWidth to (nColumns - 1) * horizontalSpacing
  156. set nWindowWidth to (availScreenWidth - nSpacingWidth) / nColumns
  157. set nTop to y0 + (irow mod maxRows) * (verticalSpacing + nWindowHeight)
  158. --display dialog "Top: " & nTop buttons {"OK"} default button 1
  159. repeat with icolumn from 0 to nColumns - 1
  160. set nLeft to x0 + (icolumn) * (horizontalSpacing + nWindowWidth)
  161. set itile to {¬
  162. nLeft, ¬
  163. nTop, ¬
  164. nLeft + nWindowWidth, ¬
  165. nTop + nWindowHeight}
  166. set end of ret to itile
  167. --display dialog item 3 of itile as string
  168. --set itile to {x0 + (icolumn - 1) * wgrid, y0, wgrid, hgrid}
  169. --set item 3 of itile to ((item 1 of itile) + (item 3 of itile))
  170. --set item 4 of itile to ((item 2 of itile) + (item 4 of itile))
  171. end repeat
  172. end repeat
  173.  
  174. return ret
  175. end calTileBounds
  176.  
  177.  
  178.  
  179. on getFrntApp()
  180. tell application "System Events" to set frntProc to ¬
  181. name of every process whose frontmost is true and visible � false
  182. return frntProc
  183. end getFrntApp
  184.  
  185. on getUserScreen()
  186. -- size of the menubar
  187. tell application "System Events"
  188. set {menuBarWidth, menuBarHeight} to size of UI element 1 of application process "SystemUIServer"
  189. --display dialog "Menubar width: " & menubarWidth & ", height: " & menubarHeight
  190. set dockApp to (application process "Dock")
  191. set {dockWidth, dockHeight} to size of UI element 1 of dockApp
  192. --display dialog "Dock width: " & dockWidth & ", height: " & dockHeight
  193. set dockPos to position of UI element 1 of dockApp
  194. --display dialog "Dock x: " & (item 1 of dockPos) & ", y: " & (item 2 of dockPos)
  195. end tell
  196.  
  197. -- size of the full screen
  198. (*
  199. {word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Width") as number, ¬
  200. word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Height") as number}
  201. *)
  202. tell application "Finder"
  203. set screenSize to bounds of window of desktop
  204. set screenWidth to item 3 of screenSize
  205. set screenHeight to item 4 of screenSize
  206. end tell
  207. --display dialog "Screen width: " & screenWidth & ", height: " & screenHeight
  208.  
  209. -- by default, set the available screen size to the full screen size
  210. set availableWidth to screenWidth
  211. set availableHeight to screenHeight - menuBarHeight
  212. set availableX to 0
  213. set availableY to menuBarHeight
  214.  
  215. --determine the userscreen origin and size
  216.  
  217. -- case 0: hidden dock
  218. -- if (item 1 of dockPos < 0 or item 1 of dockPos � screenHeight) then
  219. -- no need to change anything
  220. -- end if
  221.  
  222. -- case 1: bottom dock
  223. if ((item 2 of dockPos) + dockHeight = screenHeight) then
  224. set availableHeight to availableHeight - dockHeight
  225. end if
  226.  
  227. -- case 2: left dock
  228. if (item 1 of dockPos = 0) then
  229. set availableWidth to availableWidth - dockWidth
  230. set availableX to dockWidth
  231. end if
  232.  
  233. -- case 3: right dock
  234. if ((item 1 of dockPos) + dockWidth = screenWidth) then
  235. set availableWidth to availableWidth - dockWidth
  236. end if
  237.  
  238. return {availableX, availableY, availableWidth, availableHeight}
  239. end getUserScreen

URL: http://www.shadowlab.org/Software/spark.php

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.