How Do I Activate Finder Tab for a Given Target Folder?

How Do I Activate Finder Tab for a Given Target Folder?

Running macOS 10.14.6 (Mojave).

Given the POSIX path or alias of a Folder, how do I:

  1. Determine if an existing Finder Tab already has that folder as its target
    • and if so, activate that Finder Tab
      OR
  2. If it is NOT the target of any Finder tab/window, then create a new TAB in the current Finder window and make its target be the given Folder.

As a result, I want to make frontmost a Finder Window with a Tab with the Given Folder.

I don’t find any “tab” objects in the Finder scripting dictionary.

Thanks.

Finder use window (index) there frontmost window become index 1.
Meaning if other tab/window is frontmost or active window it become index 1.

Hey JM,

Apple in their infinite wisdom hasn’t improved access to window tabs one bit in years.

I just looked at my buddy’s Big Sur machine, and it looks like no progress there either.

You can do something like this:

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2021/07/19 01:01
# dMod: 2021/07/19 01:01 
# Appl: Finder
# Task: Bring a Finder Window with a Given Target to the Front.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder
--------------------------------------------------------

set targetAlias to path to downloads folder
set targetWindowList to {}

tell application "Finder"
   
   repeat with theWindow in (get windows)
      if (target of theWindow as alias) = targetAlias then
         set end of targetWindowList to theWindow
      end if
   end repeat
   
   if targetWindowList = {} then
      
      display alert "Window Not Found!"
      
   else if length of targetWindowList = 1 then
      
      set index of item 1 of targetWindowList to 1
      
   else if length of targetWindowList > 1 then
      
      display alert "Multiple Windows Were Found!"
      
   end if
   
end tell

--------------------------------------------------------

As for opening tabs you’re stuck with brute force – either AppleScript UI-Scripting or Keyboard Maestro.

-Chris

I’d suggest using the Finder window class to avoid throwing errors should there be any window open in Finder that doesn’t have a target property.

It might be possible to do something like:

set the targetWindowList to every Finder window ¬
    where its target as alias = the targetAlias

Although I’m aware that, in Catalina at least, Finder has developed new bugs and now chokes on some constructions of whose filters that it previously handled without any issue.

1 Like

Good point.

I’ve seen no variant of this that will work, but please do let me know if you discover one.

I’ve fixed this version to include your suggestion and to filter out search windows (which don’t have a target).

-Chris

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2021/07/19 01:01
# dMod: 2021/07/19 18:40
# Appl: Finder
# Task: Bring a Finder Window with a Given Target to the Front.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder
# Vers: 1.01
--------------------------------------------------------

set targetAlias to path to downloads folder
set targetWindowList to {}

tell application "Finder"
   
   --» •• CHANGED ••
   set windowList to Finder windows whose name does not start with "Searching"
   
   repeat with theWindow in windowList
      if (target of theWindow as alias) = targetAlias then
         set end of targetWindowList to theWindow
      end if
   end repeat
   
   if targetWindowList = {} then
      
      display alert "Window Not Found!"
      
   else if length of targetWindowList = 1 then
      
      set index of item 1 of targetWindowList to 1
      
   else if length of targetWindowList > 1 then
      
      display alert "Multiple Windows Were Found!"
      
   end if
   
end tell

--------------------------------------------------------

Some users may want to be able to create folders starting with the word “Searching” or its localization in their language…
The target value of a search folder is always an empty string. So:

 tell application "Finder"
	activate
	set targetAlias to (path to downloads folder) as string
	
	set theWindows to Finder windows
	repeat with aWin in theWindows
		if ((target of aWin) as text) = targetAlias then set index of aWin to 1
	end repeat
	
end tell
2 Likes

Many thanks Chris @ccstone and @CJK for your suggestions.

Thanks, @ionah. That’s very efficient.
I made one small mod to exit the loop after it finds the first window:

tell application "Finder"
  activate
  set targetAlias to (path to downloads folder) as string
  
  set theWindows to Finder windows
  repeat with aWin in theWindows
    if ((target of aWin) as text) = targetAlias then
      set index of aWin to 1
      exit repeat -- after finding FIRST window/target
    end if
  end repeat
  
end tell