help with first script

I just bought my first mac and I am a recent windows convert, so please be patient with my noob questions :stuck_out_tongue:

I am trying to write a script that opens up two safari windows of equal size side by side, I have gotten this far:

tell application "Safari"
	make new document with properties {URL:"http://www.google.ca"}

	set the screen_width to (do JavaScript "screen.availWidth" in document 1) as integer
	do JavaScript ("moveTo(0,0);self.resizeTo(" & (screen_width / 2) as string) & ",screen.availHeight)" in document 1

	make new document with properties {URL:"http://www.google.ca"}
	do JavaScript ("self.resizeTo(" & ((screen_width / 2) - 1) as string) & ",screen.availHeight);moveTo(720,0)" in document 1
end tell

This opens up two windows side by side, however they are blank and I want them to display http://www.google.ca. It seems as though after the do JavaScript function the windows go blank and I have no idea why. Any ideas?

[Moved to AppleScript | OS X forum]

AppleScript | OS is for OS 9 and previous systems.

As far as helping goes, I have two screens, and that seems to completely confuse Safari so the script doesn’t work as intended with or without Google.

hi dank,

i got this to work using js:


tell application "Safari"
	make new document
	set the screen_width to (do JavaScript "screen.availWidth" in document 1) as integer
	do JavaScript ("moveTo(0,0);self.resizeTo(" & (screen_width / 2) as string) & ",screen.availHeight)" in document 1
	do JavaScript ("window.location.href = \"http://www.google.ca\"") in document 1
	
	make new document
	set the screen_width to (do JavaScript "screen.availWidth" in document 1) as integer
	do JavaScript ("moveTo(720,0);self.resizeTo(" & ((screen_width / 2) - 1) as string) & ",screen.availHeight);" in document 1
	do JavaScript ("window.location.href = \"http://www.google.ca\"") in document 1
end tell

however, only one of the windows opens up in the right place. not sure what to do about that.

Hi,

If the javascript is run before the page fully loads, then you lose the url. You could a delay or check, but I’d position windows first, then load the page as waltr tried.

gl,

Hey thanks alot, I figured out why yours didn’t move to the right location:


tell application "Safari"
	make new document
	set the screen_width to (do JavaScript "screen.availWidth" in document 1) as integer
	do JavaScript ("moveTo(0,0);self.resizeTo(" & (screen_width / 2) as string) & ",screen.availHeight)" in document 1
	do JavaScript ("window.location.href = \"http://www.google.ca\"") in document 1
	
	make new document
	set the screen_width to (do JavaScript "screen.availWidth" in document 1) as integer
	do JavaScript ("self.resizeTo(" & ((screen_width / 2) - 1) as string) & ",screen.availHeight);moveTo(720,0);" in document 1
	do JavaScript ("window.location.href = \"http://www.google.ca\"") in document 1
end tell

you have to move after you resize because the window resizes the same length both left and right from the center

Still doesn’t work for me on dual screens. Ahh well.

Hi dankj,

I have a different size screen. To avoid overlapping windows, I came up with:


tell application "Safari"
	repeat 2 times
		make new document
		set the screen_width to (do JavaScript "screen.availWidth" in document 1) as integer
		do JavaScript ("moveTo(0,0);self.resizeTo(" & ((screen_width / 2) - 1) as string) & ",screen.availHeight)" in document 1
		do JavaScript ("window.location.href = \"http://www.google.ca\"") in document 1
		
	end repeat
	
	set {b1, b2, b3, b4} to (get bounds of window 1)
	set theBounds to {b1, b2, b3, b4}
	set the bounds of window 1 to {(b1 + b3 +2), b2, (b3 * 2), b4}
	
	
end tell

I’m sure somebody more knowledgeable can clean that up a bit.

j

thanks J,

i tried the earlier script but couldn’t get it to work. must be because i’ve got a different sized screen–never thought of that.

this one works for me.

Hi again waltr,

After all the help I received, I’m happy to have something to contribute.

j