Bring a window to the front in Mail.app

In my ongoing struggle to script Apple’s Mail, I want to bring one of its windows to the front. I thought this would be a simple case of setting the index value of the window:

tell application "Mail"
	set theWindow to item 2 of (windows whose visible is true)
	set index of theWindow to 1
end tell

If I run the above script in a script editor, I can see the window pop forward. But when I switch to Mail, it pops back and the original window ordering remains unchanged. If I save the above as a script and run it from within Mail (using the script menu), I can see the window pop forward briefly and then immediately pop back to its original position. Is this yet another bug in Mail’s scripting, or is there a better/correct way to bring a window to the front?

Thanks,
Jolin

Yep, I can’t do it either. It clearly switches the window position but reverts back when Mail is brought frontmost. Maybe this helps…

tell application "Mail"
	activate
	set theWindow to item 2 of (windows whose visible is true)
	set windowTitle to name of theWindow
end tell

tell application "System Events"
	tell process "Mail"
		click menu item windowTitle of menu 1 of menu bar item "Window" of menu bar 1
	end tell
end tell

Hi Hank,

Thanks for your reply and the workaround. A shame that a ‘hack’ is needed, but it works so I’m happy for now. :slight_smile:

Thanks very much,
Jolin

This worked for me without invoking System Events:

tell application "Mail"
	set x to get id of window 1
	activate
	if id of window 1 is x then
		set index of window 2 to 1
	end if
end tell

The trick seemed to be getting the id of window 1 before telling Mail to activate. Hope this helps.

Script tested using Mac OS 10.4.11 and Mail 2.1.3.

That does work better andmr, but it isn’t quite right either. When I run the script, the second window does come in front of the previous frontmost window, however the window title bar and close buttons are still grayed out. The previous frontmost window seems to still be active. You have to click in the new frontmost window to make it active.

So you do have the window position changing however the window still is not actively frontmost as evidenced of it still being grayed out in the title bar. Something is still missing but you’re a little closer.

And actually the whole if statement stuff isn’t necessary as this script acts the same…

tell application "Mail"
	activate
	set index of window 2 to 1
end tell

Hank,

You’re correct to point out that the new frontmost window’s buttons are grayed out. I had noticed that too. However, since the new front window’s contents do become viewable, I don’t know how much of an issue that would be. Perhaps that’s all that’s being asked for.

The trouble I had with your stripped-down version my script is that it works only once. Try launching it a second time and, sure enough, the same window returns to the front.

With the if statement included, the script acts as a toggle, reliably, each time it’s run:

tell application "Mail"
   set x to get id of window 1
   activate
   if id of window 1 is x then
       set index of window 2 to 1
   end if
end tell

Hi andmr,

Thanks for this, that is interesting that it’s possible to toggle the window by getting the id, telling Mail to activate, and then setting the window’s index. However, I do need the new front window to have the keyboard focus, so the bug of the greyed-out buttons ruins this for me. :frowning: Thanks for the help, though.

Cheers,
Jolin

jolin,

Understood.

In that case, you might have a look at the script below:

tell application "Mail"
	set x to get id of window 1
	activate
	if id of window 1 is x then
		set index of window 2 to 1
	end if
end tell
tell application "System Events"
	tell process "Mail"
		perform action "AXRaise" of window 1
	end tell
end tell

While this updated version also uses System Events to get the job done, it does so without manipulating any menus or menu items. As a result, you should notice that Mail’s Window menu doesn’t flash.

Just another thought. Good luck.

Thanks andmr. I’ll play around with that.

Cheers,
Jolin