How to Close Finder Tab?

This seems obvious, and should be easy, but it is not working for me.
I want to close the current/active tab on the frontmost Finder window.

See my simple test macro below.
What am I doing wrong?

I tried it with by the menu close and the keystroke close, and with and without the "Bring App Windows to Front".
Neither works.


###Answered on Sat, Aug 22, 2015! :+1:
See the solution by @ccstone in the post below.


Hey JM,

I recreated this without the pause, and it works for me with KM7.0.1 on OSX 10.10.5

I used F1 as the Hot Key for the macro.

-Chris

Thanks for testing Chris.

Something strange is going on with my system.
The Finder is "activated", but not fully until I click on it.

The Finder does NOT receive any commands UNTIL I click on it.

Any ideas?

Running KM7.0.1 on OSX 10.10.4

I've also tried using AppleScript, but the results are the same:

tell application "Finder"
	activate
	set frontmost to true
	set visible to true
end tell -- Finder

Hy JM,

Try this instead of talking to the Finder.

tell application "System Events"
  tell application process "Finder"
    set frontmost to true
  end tell
end tell

Close all Finder windows.

Restart the Finder.

Restart the Keyboard Maestro Engine.

Reboot your system.

Upgrade to 10.10.5.

If none of that works:

Put this AppleScript in a macro, and output to a text-window:

path to frontmost application as text

Activate the Finder to the degree that it will activate and run the macro — make sure the Finder is in fact the frontmost app.

Open your Activity Monitor and look for any odd processes.

That's all I can think of at the moment.

-Chris

"Macintosh HD:System:Library:CoreServices:Finder.app:"

As my system is now. I will now do the other stuff you suggested.

Thanks.

. . . keep butting your head against the brick wall until something gives. :grin:

Hey Chris. Thanks for all your help and suggestions.

After doing everything you suggested, it still failed on an intermittent basis.
For a while I suspected SnagIT, and the Parallels Desktop. I even reinstalled SnagIT, and tested without it installed. No help.

But I finally figured it out, I think: If the Finder window is in a different monitor than where the macro/script is initiated, then it fails.
When activated, the Finder menu is active on Screen 1, but the Finder Window is on Screen 2, showing in front of everything else, yet not truly/fully active.

What's strange is that the "Activate" command works fine for other apps, regardless of which screen they are on.

Go figure!?!?

From your vast experience with System Events, is there anyway to detect which screen the Finder is on, and somehow use that to bring it to the front?

Thanks.

Hey JM,

Are you just using a multi-monitor setup — or are you using Spaces?

-Chris

Multi-monitor.

Hey JM,

What do you get when you run this from a Keyboard Maestro macro and output to a text window?

tell application "Finder"
  activate
  set insLoc to insertion location as alias
  set insLocName to name of insLoc
end tell

-Chris

Thanks for the suggestion.

When I run the below macro when the current focus is on an app (Outlook) in a different screen than the Finder:

RESULTS: Desktop

When the current focus of the other app and Finder are in the same screen:
RESULTS: Photos
which is the name of the Finder Tab.

Hey JM,

Okay. In the former case run this with output to a text-window and see what you get:

------------------------------------------------------------
# List Finder Window Index & Name
------------------------------------------------------------
tell application "System Events"
  if quit delay ≠ 0 then set quit delay to 0
  tell application process "Finder" to set frontmost to true
end tell
set AppleScript's text item delimiters to ""
tell application "Finder" to set {nameList, indexList} to {name, index} of windows
if nameList ≠ {} then
  set _cntr to 0
  repeat with i in nameList
    set _cntr to _cntr + 1
    set contents of i to ((item _cntr of indexList) & tab & (contents of i)) as text
  end repeat
end if
set AppleScript's text item delimiters to linefeed
nameList as text
------------------------------------------------------------

-Chris

Thanks for the diagnostic script.

When I run the above macro with your new script at the end, when the current focus is on an app (Outlook) in a different screen than the Finder:

RESULTS:

1	Public
2	Applications

"Public" is the frontmost window of Finder.

Hey JM,

If I send you a script to run in a diagnostic context please do not add it to other scripts. I'm looking for a specific result without possible side-effects.

Okay, try running this before your close-tab menu action.

------------------------------------------------------------
set myDesktop to path to desktop
------------------------------------------------------------
tell application "System Events"
  if quit delay ≠ 0 then set quit delay to 0
  tell application process "Finder" to set frontmost to true
end tell
------------------------------------------------------------
tell application "Finder"
  if (insertion location as alias) = myDesktop then
    if (count of windows) > 0 then
      set index of window 1 to 1
    end if
  end if
end tell
------------------------------------------------------------

-Chris

1 Like

Sorry, I guess I misunderstood you. Will follow this instruction in the future.

Many thanks! This works! :+1:
Another AppleScript mystery solved by Detective Stone. :sunglasses:
EverNoted!