Moving Windows

I can’t agree more with that, and I’m wrong again in the future about something for sure. I wasn’t offended and my post was more as an confirmation than a correction or whatsoever. I want to keep things clear as well and when anyone’s post could lead into wrong interpretations, seems he didn’t understand it completely or still has some questions I keep responding till everything is clear. Not only for the topic starter but also when someone searches MacScripter and read the topic when the topic is a few weeks old (forgotten). So it’s not personal, stubbornness or anything like that, it’s just that I want to have everything as clear as you. :slight_smile:

Agreed!

It is the dialogue that is important. It is a Norwegian saying, that when men speak (or women), it is like lighting a torch in the dark. Now, if everybody was afraid of saying or writing somethinging wrong, it wouldn’t be much development would it? :slight_smile: And I say and do a lot of wrong stuff, not only now, but for the future too I guess.

This hasn’t been polished by any means, but this is what I worked up for centering the frontmost window on either of my two screens. At least it works for me. :lol:


global details, tApp

(*HANDLERS*)
-- Get screen parameters
to getScreens()
	tell (do shell script "echo `defaults read /Library/Preferences/com.apple.windowserver | grep -wA 1 -m 2 OriginX``defaults read /Library/Preferences/com.apple.windowserver | grep -w -m 2 Height``defaults read /Library/Preferences/com.apple.windowserver | grep -w -m 2 Width``defaults read /Library/Preferences/com.apple.windowserver | grep   DisplayLayoutToRight``defaults read com.apple.dock | grep  orientation``defaults read com.apple.dock | grep tilesize`") to set details to {word 3, word 6, word 9, word 12, word 15, word 18, word 21, word 24, word 27, word 30, word 33}
	return details
end getScreens

--> Origin1 x, y; Origin2 x, y; Height 1, 2; Width 1, 2; DisplayLayout 1 = Right; DockPosition 0 = Left; DockSize; Extent x, y. The screen with origin 0, 0 has the menu bar, or if Display2toRight = 1, then the menu bar is on the left.

to getCenterMain()
	tell details -- not general, specific to my own setup (no calculation of which screen is which and dock position)
		-- X center, main screen is width - dock size over 2 + dock size
		set ctr_Main_X to (((item 7 as integer) - (item 11)) / 2 + (item 11)) as integer
		-- Y center, main screen is hight - menu bar size over 2 + menu bar height.
		set ctr_Main_Y to (((item 5 as integer) - 22) / 2 + 22) as integer
	end tell
	return {ctr_Main_X, ctr_Main_Y}
end getCenterMain

to getCenterSecondary()
	tell details
		set ctr_Sec_X to ((item 8 as integer)) / 2 as integer
		set ctr_Sec_Y to ((item 6 as integer) / 2) as integer
	end tell
	return {ctr_Sec_X, ctr_Sec_Y}
end getCenterSecondary

-- Get running apps, identify frontmost, get its window size and position
to getFrontmostWindow()
	tell application "Finder" to set |running| to name of processes whose visible is true
	-- Find the frontmost
	tell application "System Events"
		repeat with anApp in |running|
			if get frontmost of process anApp then
				tell application process anApp to tell window 1
					set {{p1, p2}, {s1, s2}, tApp} to {position, size, contents of anApp}
				end tell
			end if
		end repeat
		return {{p1, p2}, {s1, s2}}
	end tell
end getFrontmostWindow

--- MAIN ---
set tScreens to getScreens() -- get the screen centers
set {tPosition, tSize} to getFrontmostWindow() -- get postion and extent of window
-- if the window is more than half way into the second screen choose second screen for centering.
if (item 1 of tPosition as integer) + (item 1 of tSize) / 2 > item 3 of tScreens as integer then
	set Ctr to getCenterSecondary()
	set Scr to "Sec"
else
	set Ctr to getCenterMain()
	set Scr to "Main"
end if
-- Find bounds to center the window without changing its size.
set {Tx, Ty} to {(item 1 of Ctr) - (item 1 of tSize) / 2, (item 2 of Ctr) - (item 2 of tSize) / 2}
-- Set new bounds for the window to center it.
tell application tApp
	if Scr is "Sec" then
		set Tx to Tx + (item 3 of details)
		set Ty to Ty + (item 4 of details)
	end if
	set bounds of window 1 to {Tx, Ty, Tx + (item 1 of tSize), Ty + (item 2 of tSize)}
end tell



Hello Adam!

That is so cool! I think I am going to snag some pieces of it, for a project coming up soon!

It is really impressive, and it works for me too!

Hello!

I am curious about the advantages of using this:


tell application "Finder" to set |running| to name of processes whose visible is true

Instead of System Events. I guess it is out of speed concerns, but I am not sure, that is why I am asking.

there is no advantage on local systems. The Finder just launches System Events
On remote systems “ to send Remote Apple Events “ it’s advantageous to be able to launch System Events via the Finder

Ahh! Thanks Stefan! :slight_smile:

Normally the Finder is sluggish, but in this particular case several OS Xs ago, it seemed faster than System Events. Now it’s as Stefan says.