AppleScript opening/tiling two safari windows

Discussion in 'Mac Programming' started by saha-med, Jan 11, 2013.

  1. macrumors regular

    Joined:
    Dec 2, 2012
    #1
    Hi, im fairly new to applescript. I want to make a script that will open two Safari windows side by side(tiled) with one window on google.com and another on yahoo.com
    So ive been googling various solutions and mixing up different codes to find out a solution. At this point im getting an error with running this applescript, can anyone tell me why?


    Thanks in advance

    Code:
    application "Safari"
    	activate
    	do JavaScript "window.open('http://www.yahoo.com')" in document 1
    	set bounds of window 1 to {160, 80, 980, 700}
    	tell application "System Events"
    		tell process "Safari"
    			click menu item "New Window" of menu "File" of menu bar 1
    			set bounds of window 2 to {160, 80, 980, 700}
    		end tell
    	end tell
    end tell
     
  2. andmr, Jan 11, 2013
    Last edited: Jan 11, 2013

    macrumors member

    Joined:
    Aug 25, 2008
    Location:
    NE Florida
    #2
    Hi,

    You might want to try something like this:
    Code:
    tell application "Safari"
    	activate
    	open location "http://www.google.com"
    	set bounds of window 1 to {10, 123, 513, 457}
    	make new document
    	open location "http://www.yahoo.com"
    	set bounds of window 1 to {513, 123, 1016, 457}
    end tell
    
    In this example I set bounds that would work well on my 15'' iMac's screen. Naturally, you'll want to work on your own settings.

    Tip: To find the bounds of the frontmost Safari window, use this command in a separate AppleScript Editor window:
    Code:
    tell application "Safari" to get bounds of window 1
    The results can be found in the Editor's Result pane.

    Once you have your two Safari windows sized and positioned to your satisfaction, run the "get bounds" command on each window in turn, and then copy and paste the results accordingly into your main script. Good luck.

    Script tested using Mac OS 10.4.11
     
  3. macrumors 6502

    superscape

    Joined:
    Feb 12, 2008
    Location:
    Yorkshire, UK
    #3
    Alternatively...

    If you wanted to be a bit more show-offy, you could calculate the coordinates of the window on the fly. For example:

    Code:
    tell application "Finder"
    	--gets the coords of the desktop
    	set theScreenSize to bounds of window of desktop
    end tell
    
    
    tell application "Safari"
    	activate
    	make new document with properties {URL:"http://www.google.com"}
    	set bounds of window 1 to {0, 0, (item 3 of theScreenSize) / 2, (item 4 of theScreenSize)} --coords of window are calculated from theScreenSize
    	
    	make new document with properties {URL:"http://www.yahoo.com"}
    	set bounds of window 1 to {(item 3 of theScreenSize) / 2, 0, (item 3 of theScreenSize), (item 4 of theScreenSize)}
    	activate
    	
    end tell
    
    

    ...note that this will only really work well if you use a single monitor - if you have a second monitor then things get fiddly!
     

Share This Page