How can I control the stacking order of document windows

Hello scriptologists,

I can access and alter TextEdit document windows by name, id and index, but i still can’t figure out how to bring the back one to the front, etc. :confused: Please help. Also, please explain if your solution works for document windows of all applications, or only TextEdit.

Bonus questions: See included script.

Many thanks,
icta



tell application "TextEdit"
	launch
	
	-- Both of these statements create a new document at the front of the stack. Why?:
	
	make document at beginning of documents
	
	make document at end of documents
	
	-- What does "at beginning of documents" really mean?
	
end tell


tell application "TextEdit"
	-- Bring the back window to the front.
	-- The indices of all the open windows are changed as necessary.
	set index of window -1 to 1
end tell

It probably works with lots of other (scriptable) applications. But since applications are free to implement their own methods to a certain extent, it’s a bit pointless to look for a universal solution.

There doesn’t appear to be any difference as far as TextEdit documents are concerned, but the insertion points may have different effects in other applications and/or with different kinds of item.

Excellent! Thank you Nigel. This gets me moving forward again. :slight_smile:

I’m glad it helped. :slight_smile:

Looking into it more closely, it seems it’s only possible to bring TextEdit windows forward, not to send them further back. (In Jaguar, nothing happens; in Tiger, the problem of which way to displace the other windows leads to inaccurate results.) Here’s an attempt at a universal TextEdit window index changer:

on changeTEWindowIndex from i to j given restacking:restacking
	tell application "TextEdit"
		set w to (count windows)
		if (i < 0) then set i to i + w + 1
		if (j < 0) then set j to j + w + 1
		if (j < i) then
			set index of window i to j
		else
			repeat with i from (i + 1) to j
				set index of window i to (i - 1)
			end repeat
		end if
		if (restacking) then
			set {e, f} to {31, 22}
			repeat with i from w to 1 by -1
				set {a, b, c, d} to bounds of window i
				set bounds of window i to {e, f, e + c - a, f + d - b}
				set {e, f} to {e + 22, f + 22}
			end repeat
		end if
	end tell
end changeTEWindowIndex

changeTEWindowIndex from 1 to -1 with restacking -- Front window to back and restack.
-- changeTEWindowIndex from 1 to 2 without restacking -- Swap front two windows, but don't restack.

NG,

This is really cool! I’ve learned a lot from it. Thanks!

DG