The Finder always takes the focus

I was curious about the manner in which macOS works. To illustrate:

  1. I open Safari, TextEdit, Mail, and one Finder window in this order.

  2. I close the Finder window with Command-W, and Finder retains the focus. I click on Mail.

  3. I quit Mail with Command-Q and the Finder takes the focus. I click on TextEdit.

  4. I quit TextEdit with Command-Q and the Finder takes the focus. I click on Safari.

Why is the focus taken by the Finder after closing an app?

This became a small issue in a script and I included the following code, which is called whenever all Finder windows are closed or an app is quit. This works but not very well (apparently because of timing issues). Is there a different way to do this? Thanks.

tell application "System Events"
	if (name of first process whose frontmost is true) = "Finder" then
		tell process (name of (item -1 of (every process where background only is false)))
			set frontmost to true
		end tell
	end if
end tell

In your example, because it’s the previously active app. It’s nothing particular to the Finder.

Shane, thanks for the response.

What you say makes sense but it doesn’t seem to work that way if the Finder is not involved. For example,

  1. Open Mail, TextEdit, and Safari in that order.

  2. Close the Safari window with Command-W and click on TextEdit.

  3. Quit TextEdit with Command-Q and Mail has the focus. If I understand you correctly, Safari should have the focus after quitting TextEdit.

Perhaps the difference is that the Finder has the desktop while Safari has no windows open?

Whether Safari has a window open makes a difference, but I’m not sure how this example relates to the Finder.

Shane. In my first example, the focus returns to the Finder and you indicate this is because it’s the previously active app. That makes sense.

In step 3 of my second example, I would expect the focus to return to Safari, which is the previously active app, but that doesn’t happen. As things go, this is probably of little consequence, but it does seem inconsistent to me.

You can quit and start Finder as other apps.

tell application "Finder" to quit 

I see what you mean, but I don’t believe the Finder is relevant. Rather, I think it’s behaving a bit like a tabbed window. Say you have a tabbed window in TextEdit with a dozen tabs. if you close the right-most one, it falls back to the next, and so on. So with tab 12 active, tab 11 is the next in line. But let’s say you then click on tab 4. The act of making tab 4 active also makes tab 3 the next in line, with tab 11 shunted down the theoretical order.

And what is the general meaning of your code? I ask because the order of the processes in the list is in the usual alphabetical order and has no relation to the order of opening the windows.

Getting all opened windows:

tell application "System Events"
	set theWindows to {}
	repeat with theProcess in (application processes where background only is false)
		set theWindows to theWindows & (value of (first attribute whose name is "AXWindows") of theProcess)
	end repeat
end tell

Having the windows you can switch to certain window telling to window perform action “AXRaise”

set theCertainWindow to item 1 of theWindows
tell application "System Events" to tell theCertainWindow to perform action "AXRaise"

The order of processes in the list is the order in which the processes were opened.

Yes you are right. After re-checking, I was convinced that for the first time it was myself, that I accidentally opened programs in alphabetical order :

tell application "System Events" to name of every process where background only is false
-->{"Finder", "Safari", "Script Editor", "Script Debugger", "TextEdit", "Mail", "Menu Helper", "CrossOver", "BitTorrent"}

:slight_smile:

But the order of launching programs may again differ from the order of opening windows. Or you should remember yourself !!! order of launching applications and open windows strictly in that order. Let’s agree, it is not serious.

NOTE: I checked: after turning off the “Finder” application with command

tell application "Finder" to quit

the focus transition occurs in the order of opening and closing of windows.

Thanks everyone for the responses. This whole issue arose because I personally find it irritating for the focus to be placed on the desktop when I quit an app (as illustrated in my first post). I understand that other users may like this behavior.

To accomplish what I want, it appears that I can:

  • Quit the Finder as KniazidisR suggests. This causes the top menu bar and icons on the desktop to flash off and on but otherwise works as I want.

  • Stop using a Finder window for file management and instead use a file-manager app that I can quit.

  • Use a script to quit apps, which I’ll work on.

Thanks again.

If you need in your script file manager and to do without a other file manager, you can use best approach: you can use the Hide Finder command instead of the Quit Finder command:

set visible of application "Finder" to false

To reopen last Finder window when you need:

set visible of application "Finder" to true

And… generally: To transfer focus to another application, you can give the same command to an application that has focus at the current moment:

set visible of application "SomeApp" to false

Thanks KniazidisR for the ideas.

I wrote a script which works reasonably well. This seems a lot of effort to achieve a simple result–preventing the desktop from taking the focus–but because of the extensive use I make of the Finder this was worthwhile.


--Set variables to the active app and all open apps.
tell application "System Events"
	set activeApp to name of first process whose frontmost is true
	set openApps to name of every process whose background only is false
end tell

--Set variable to the number of open apps.
set appCount to (count openApps)

--Set variable to item number of active app in open apps.
repeat with i from 1 to appCount
	if (item i of openApps) = activeApp then exit repeat
end repeat

--Close active Finder window or quit active app.
if activeApp = "Finder" then
	tell application "Finder"
		if (count windows) > 0 then close every window
	end tell
else
	tell application activeApp to quit
end if

--Change focus as necessary.
tell application "System Events"
	if i > 2 then
		tell process (item (i - 1) of openApps) to set frontmost to true
	else if i = 1 and appCount > 1 then
		tell process (item -1 of openApps) to set frontmost to true
	else if i = 2 and appCount > 2 then
		tell process (item -1 of openApps) to set frontmost to true
	end if
end tell