Friday, July 20, 2018

#1 2014-01-02 11:19:34 am

scott721
Member
Registered: 2011-08-30
Posts: 3

Create new Safari window (or tab) and center on screen

I find it annoying that whenever I create a new window in Safari it appears to open in a tiled fashion.  I'm a neat freak and like my windows to all be centered on the screen and touching the top edge (at the menu bar line).  With the help of some bits and pieces I collected from here and around I came up with this.  I hope it helps other OCD/neat freaks out, too.

Applescript:

tell application "System Events"
   set theName to name of the first process whose frontmost is true
end tell

if theName is "Safari" then
   
   (*Creates a new browser window*)
   tell application "Safari" to activate
   tell application "Safari"
       make new document
   end tell
   
   (*Centers window on screen*)
   tell application "Finder"
       set screenSize to bounds of window of desktop
       set screenWidth to item 3 of screenSize
   end tell
   
   tell application "System Events"
       set myFrontMost to name of first item of ¬
           (processes whose frontmost is true)
   end tell
   
   try
       tell application myFrontMost
           set windowSize to bounds of window 1
           set windowXl to item 1 of windowSize
           set windowYt to item 2 of windowSize
           set windowXr to item 3 of windowSize
           set windowYb to item 4 of windowSize
           
           set windowWidth to windowXr - windowXl
           
           set bounds of window 1 to {¬
               (screenWidth - windowWidth) / 2.0, ¬
               windowYt, ¬
               (screenWidth + windowWidth) / 2.0, ¬
               windowYb}
       end tell
   end try
   (*Moves window to top edge of screen*)
   set cur_app to (path to frontmost application as Unicode text)
   if cur_app ends with ":Finder.app:" then
       set Finder to true
       tell application "Finder"
           set tool_vis to toolbar visible of front window
       end tell
   else
       set Finder to false
   end if
   
   
   tell application cur_app
       tell front window
           set {x1, y1, x2, y2} to (get bounds)
           set win_height to (y2 - y1)
           set room_top to y1
           if room_top > win_height then
               if Finder then
                   set y1 to (y1 - win_height)
                   set y2 to (y2 - win_height)
               else
                   set y1 to (y1 - win_height)
                   set y2 to (y2 - win_height)
               end if
           else
               if Finder then
                   set y1 to 43
                   set y2 to (y1 + win_height)
               else
                   set y1 to 0
                   set y2 to win_height
               end if
           end if
           
           # stupid hack for 10.5/10.6 Terminal.app
           # see [url]http://openradar.appspot.com/5765608[/url]
           if cur_app ends with ":Terminal.app:" then
               set win_height to (y2 - y1)
               set y1 to (y1 + win_height)
               set y2 to (y2 + win_height)
           end if
           
           set bounds to {x1, y1, x2, y2}
           
       end tell
   end tell
end if


Filed under: safari, center, new window

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)