Minimize All but the Front Window

Is there a better way to minimize all windows than the front Finder or Safari window than this?

tell application "Finder"
	repeat 30 times
		tell window 2
			set collapsed to true
		end tell
	end repeat
end tell

tell application "Safari"
	repeat 30 times
		tell window 2
			set miniaturized to true --hide
		end tell
	end repeat
end tell

I know the syntax is wrong but I was thinking something like the following based on http://developer.apple.com/library/mac/#documentation/applescript/conceptual/applescriptlangguide/reference/ASLR_reference_forms.html

tell application "Finder"
	set miniaturized to true for every window whose name is not "Window 1"
end tell

or even close every other window but window 1

tell application "Finder"
	close (every window whose name is not "Window 1")
end tell

Sorry to bother I have spent the day on this and can’t figure it out. I have looked all over AppleScript 1-2-3 and this forum but I am not sure what to even search anymore. It’s painful being such a noob.

I did learn from the book the reason “collapsed” is used is a hold over from OS 9 feature and why they use it instead of “miniaturized”. Why the term minimized wasn’t chosen sure leaves me to wonder, I guess it sounded too much like MS Windows, but I digress.

Hi. My arrangement would be preferable to the first two examples, which use arbitrary repeat values:

tell application "Finder" to set (windows whose name is not "Window 1")'s collapsed to true

Your last one would’ve worked, but you needed an explicit “get”.

Not terribly elegant but it works for me with one window each – this script clicks the minimize button of each application. I didn’t play with it for multiple open windows.


tell application "System Events"
	set Proc to name of processes whose visible is true and name is not "Finder" and name is not "Safari"
	repeat with oneP in Proc
		activate process oneP -- bring it to the front
		click button 3 of window 1 of application process oneP -- the yellow button on the top left.
	end repeat
end tell

I’m not clear if you want to minimise every window of every open application except the front ones in the Finder and Safari (which can be complicated) or minimise every window in the Finder and Safari except the front ones (which is easy, since their scripting dictionaries both allow it).

A better way to define the repeat here would be:

tell application "Finder"
	repeat ((count windows) - 1) times -- Only executes if (count windows) > 1.
		set collapsed of window 2 to true
	end repeat
end tell

Or:

tell application "Finder"
	repeat with i from (count windows) to 2 by -1 -- Only executes if (count windows) > 1.
		set collapsed of window i to true
	end repeat
end tell

If you want to use a filter reference, you could do this:

tell application "Finder"
	if ((count windows) > 1) then set collapsed of every window whose index > 1 to true
end tell

Thank you all so much for all the help, I learned a lot from it.

The latter, minimize every window in the Finder and Safari except the front ones. I apologize for the confusion, your posts were spot on what I was looking for, thanks.

Adam, that script is neat, thanks for the great ideas you gave me. It gives errors on several of the applications I have open like MS Excel, Pomodario and others, but no worries I have several things to use that are working for me now.

Marc
Here is what I put together based on what you wrote which works great. What is the necessity of the " 's " after the parenthesis? It of course doesn’t work without it, but I would have never thought to include it.

tell application "Finder" to set (windows whose index is not "1")'s collapsed to true

I ran the following script to make sure Safari is using the same index names as Finder does for the front window (which it seems to be), then I modify your script to do the same for Safari but it doesn’t work?

tell application "Safari"
	get the index of the first window
end tell

What this instead does instead is minimize all the windows even when I run it directly from within Safari. Any ideas?

tell application "Safari" to set (windows whose index is not "1")'s miniaturized to true

Nigel, holy cow thanks for going over the top and including all those possibilities. It was SO helpful going through all those and learning new things (especially helpful when it is directly related to what I am trying accomplish)!

These all work just fine when modified to work with Safari so I would think it is something in Safari’s dictionary that is not supporting the one above. I guess my only question now is which seems the most efficient. My first post worked in Finder and Safari but seemed like it would waist time repeating 30 times if there were only 2 windows open.

They all seem the same but for me the 3rd one is the easiest to read.

tell application "Safari"
	repeat ((count windows) - 1) times -- Only executes if (count windows) > 1.
		set miniaturized of window 2 to true
	end repeat
end tell
tell application "Safari"
	repeat with i from (count windows) to 2 by -1 -- Only executes if (count windows) > 1.
		set miniaturized of window i to true
	end repeat
end tell
tell application "Safari"
	if ((count windows) > 1) then set miniaturized of every window whose index > 1 to true
end tell

The index of a window is a number. “1” (in quotes) is text, so none of the windows’ indices are equal to it (ie. all the windows’ indices are not equal to it).

Edit: By the way, the apostrophe-s you were asking about is just a possessive ending for the whole of the parenthesised expression, which you can do in AppleScript, even though it looks strange in English.

(windows whose index is not 1)'s miniaturized = miniaturized of (windows whose index is not 1)

Great thanks again Nigel for the syntax clarification.

For some reason both still minimize all the windows when run from AppleScript Editor or directly from Safari.

tell application "Safari" to set miniaturized of (windows whose index is not 1) to true
tell application "Safari" to set (windows whose index is not 1)'s miniaturized to true

Strange. :confused: They both work exactly as expected for me ” except that they don’t error when there are less than two windows open.

Interesting, I just restarted Safari and it worked right for a few times and then stopped working. I must having a plug-in or something that is messing with Safari on my computer. Thanks for letting me know I wasn’t missing something in what you posted.

Much thanks.