Maximize and Minimize Windows

Hi there,

I am a complete newbie to AppleScript, and need to write the following script.
I want the screen to cycle through a series of application windows that are each running an application. An example would be:

repeat

  1. 2minutes - Maximize window, show X11 application 1, then minimize
  2. 2 minutes - Maximize window, show Safari browser window 1, then minimize
  3. 2 minutes - Maximize window, show Safari window 2, then minimize
  4. 2 minutes - Maximize window, show X11 application 2, then minimize
  5. 2 minutes - Maximize window, show Image in Photoshop, then minimize
    end repeat

What I have so far is one process, which works, but won’t minimize after the shell sleep function is invoked and finished:


property target_URL_1 : "http://www.cnn.com"

-- start Safari
tell application "Safari"
	activate
end tell
set index of window 2 to 1
open location target_URL_1
do shell script "sleep 10"
tell application "System Events"
	tell process "Safari"
		tell menu bar 1
			tell menu bar item "Window"
				click menu item "Minimize"
			end tell
		end tell
	end tell
end tell

the script dies on the command tell menu bar 1 with the following error:

I would appreciate any help you can give me - I will only be working in Safari, X11 and Photoshop (and maybe Preview).

Thanks in advance.

You’re making the classic mistake many newbie AppleScripters make - you’re trying to get AppleScript (via System Events) to do what you’d do manually when there’s a much easier way to do it.

If you check the application dictionary you’ll see that each window has a ‘miniaturized’ flag that indicates whether the window is minimized or not. You can read this property to find the window’s state, and you can set it to change it.

Therefore you can just:

tell application "Safari"
	set miniaturized of window 1 to true
end tell

This approach is the direct approach - telling AppleScript exactly what to do (minimize the window) rather than telling it how to do it (choose this menu item from this menu).

In general, direct actions are much faster and much more portable (e.g. the above script will work on any copy of Safari whether it’s English, French or Japanese, regardless of what the ‘Minimize’ menu item (or the ‘Window’ menu, for that matter) is called.

Also, direct actions like this can be performed whether or not the application is frontmost - not something you can do with System Events/UI scripting.

Great - thanks for the advice - I will try it out tomorrow morning and let you know how it goes. It seems like there is always a new language to be a newbie in… :slight_smile: