Minimize all windows!

Man I’m having so much fun this holiday learning Applescript!

I thought of a new learning project: minimize every single window in sight regardless of application!

With the Finder I can do:

tell application "Finder"
	tell every window
		set collapsed to true
	end tell
end tell

But Safari (like many other apps) doesn’t know “collapsed” isn’t that stupid!!!

So I have to do:

tell application "System Events"
	tell every application
		tell every window
			keystroke "m" using {command down}
		end tell
	end tell
end tell

But only the top window of the top app is minimized. Hmm. What am I doing wrong?

Should I repeat-loop through all the active apps and minimize eacht front app’s windows? How is this done?

regards, Vincent (who is enjoying himself immensely :slight_smile: )

Edit:
Now trying a loop (does not work):

tell application "System Events"
	copy number of application processes to n
	repeat with i from 1 to n
		activate application process i
		tell application process i
			tell every window
				keystroke "m" using {command down}
			end tell
		end tell
	end repeat
end tell

the problem is that processes like the Dock, loginwindow etc. also are considered…

What you’re trying to do is difficult, because there are many factors involved. First, some apps behave and are developed differently than others. Cocoa apps behave differently than carbon ones. Secondly, developers have the ability to remove functionality that apple deems “standard” according to their interface guidelines (which they themselves often ignore or don’t implement). Third, even if a developer does add scripting support, that does not automatically imply that they use the same syntax or methods as others do. That said, there likely will not be a good method of doing what you’d like. I have a couple apps that do not support ‘collapse’ or ‘miniaturized’, and do not have an interface element other than double-clicking the title bar which allows the user to minimize the window. I also have apps, some made by apple, that are not scriptable or do not appear to have any windows under certain circumstances… even though they are plainly visible and active. To make matters even uglier, the script will often-times fail silently or without error, rather than failing with an explicit error… making error detection and alternative methods harder to implement. You may not find a way to ensure that if one way doesn’t work, that you can just try another.

The most reliable method I found after a bit of testing was something to this effect…

tell application "System Events"
	set currentProcesses to name of every application process whose visible = true
end tell

set tmpWindows to 0

repeat with tmpProcess in currentProcesses
	tell application tmpProcess
		activate
		try
			set tmpWindows to count windows
		on error
			set tmpWindows to 1 --> # of windows to try to force closed if it doesn't 'appear' to have any windows
		end try
	end tell
	
	if tmpWindows > 0 then
		tell application "System Events"
			tell application tmpProcess
				repeat tmpWindows times
					try
						keystroke "m" using command down
					end try
				end repeat
			end tell
		end tell
	end if
	
	set tmpWindows to 0
	
end repeat

Note that some apps list windows that are actually invisible, to you’ll hear an error sound every time it tries to close one. It also plays error sounds under other circumstances, so you’ll have to mess with error detection and workarounds. This is certainly not an exact science, and you’ll have a terrible time trying to cover every possible variable for every app.

Good luck… you’re gonna need it… :smiley:
j

Thanks j for the clear explanation!

Studying your script was very helpfull too: now I see how to go about this sort of thing.

You script works fine most of the time, but sometime it just stops. i’m going to figure that one out myself!

thanks again, Vincent

I was just reading this and I think I found another way.

Under the application menu in interface builder, add a shortcut key like cmd-o to hide others. Then just use this to hide everything but your app

tell application “System Events”
keystroke “o” using command down
end tell

First, cmd-o is one of the most reserved key combinations in osx… as in “Open” from the file menu. Using that for anything would probably conflict with something, somewhere… and would not be a recommended use of a standardized hotkey.

Second, this topic is discussing ‘minimizing’ (i.e. sending a window to the dock), not hiding. Hiding toggles the visibility of windows, and does not put them in the dock.

Third, I’m not sure where you propose adding this key combination. This forum is for osx scripts, not asstudio apps. Assuming we were talking about creating a custom app in xcode, this approach may work… but we would still need some derivative of the code posted above to actually perform the action. Plus, then we’d already be in our app and telling system events to tell our app to do something… which would be an unnecessarily redundant method of executing a routine in our app.

j