Get second frontmost app / processes in activation order

Is there a way to do it without GUI scripting? (Like ⌘⇥ or hiding the frontmost application.)

Judging from the search results, I’d guess no…

How about for windows that aren’t hidden or minimized?

Hi. Welcome to MacScripter.

This seems to work in OS 10.6:

tell application "System Events"
	set frontmostProcess to first process where it is frontmost
	set visible of frontmostProcess to false
	repeat while (frontmostProcess is frontmost)
		delay 0.2
	end repeat
	set secondFrontmost to name of first process where it is frontmost
	set frontmost of frontmostProcess to true
end tell

tell application (path to frontmost application as text)
	display dialog secondFrontmost
end tell

It does in fact hide the frontmost application, but only briefly.

1 Like

Also works on 10.7. :slight_smile: I had already tried to do it like that though.

Unrelated, but is there some reason why you need to use tell application here?

tell application (path to frontmost application as text)
	display dialog secondFrontmost
end tell

I’ve obviously missed something!

It simply ensures that the dialog isn’t “displayed” behind something else. If you know for sure that the application running the script will be frontmost at that moment, the ‘tell’ statement won’t be necessary ” though it won’t hurt either.