Find duplicate folder name and open it

Hi,


I'd like to create an AppleScript or Automator service. I'm very new to this and trying to learn with a brand new computer.

I have 2 directories containing folders with the same names but different contents.

I'd like, when I select one folder in the 1st directory to have a shorcut or Automator service to search for the same folder in the other directory and open it.

Or when I select multiple folders to do the same and open them in different finder tabs.


Thanks for your help




MacBook, macOS 10.12

Posted on Aug 10, 2022 01:39 AM

Reply
Question marked as Top-ranking reply

Posted on Aug 11, 2022 07:50 AM

Well, I know that the solution that I posted works as I tested it here on two different operating systems, both as a script in the Terminal, and again in an Automator application. Your Automator application and actions should look exactly as the posted workflow image that I provided. My test folders look like this:



When I pass the parent folders A, then B into the Terminal script, or Automator Ask for Finder Items action, a Finder window appears with two tabs: H and K, because these are first level folders common between the two parent folders, and present in parent folder B:



I did not test this using parent folders with spaces or periods in their name.

Similar questions

6 replies
Question marked as Top-ranking reply

Aug 11, 2022 07:50 AM in response to Tanpopo33

Well, I know that the solution that I posted works as I tested it here on two different operating systems, both as a script in the Terminal, and again in an Automator application. Your Automator application and actions should look exactly as the posted workflow image that I provided. My test folders look like this:



When I pass the parent folders A, then B into the Terminal script, or Automator Ask for Finder Items action, a Finder window appears with two tabs: H and K, because these are first level folders common between the two parent folders, and present in parent folder B:



I did not test this using parent folders with spaces or periods in their name.

Aug 10, 2022 08:55 PM in response to Tanpopo33

I have written and tested a Zsh script that takes two parent folders as arguments and then opens a Finder window with the common first-level folder names as new tabs. This has been tested on macOS 11.6.8 and 12.5, both as a script run in the Terminal application, and as an Automator application. I have not done this in a Monterey Shortcut due to time available.


Because of Big Sur and Monterey security issues, one will initially be greeted with two dialogs during the first Automator run:

  1. Automator wants access to control "System Events"
  2. Automator wants access to control "Finder"




In each case, when you click OK, an entry will be placed in System Preferences > Security & Privacy > Privacy > Automation for you and that dispenses with future Automator security challenges.


Here is a tested Automator application with just two actions. In the Ask For Finder Items, you choose the first folder, press the ⌘ key, and then choose the second folder. The next thing you will see is a Finder window with tabs bearing the folder names in the second folder.


When you drag and drop the Utilities Library > Run Shell Script, it will present some loop text when you select Pass input: as arguments. You remove that boilerplate and then copy/paste the following code into that Run Shell Script window:


#!/bin/zsh

: <<"COMMENT"
Provide two parent folders whose where their first level folder names may match. Open Finder tabs
for each matching folder in the second parent folder.

Dependency: Does not care if the Finder Preference > General is set to open in tabs or not
            The System Events in the AppleScript function requires security privileges set in:
            System Preferences > Security & Privacy > Privacy > Automation > Terminal > √ System Events

Usage: scriptname.zsh Parent_Folder_1 Parent_Folder_2

Reference: https://discussions.apple.com/thread/254102537
Tested: macOS 11.6.8, 12.5
Version: 1.0

Zsh modifiers: a  absolute path
               s  substitution
               N  Null glob prevents script from bombing if files are missing
               /  strictly folders
               on sort ascending on filename
               t  ordinarily the filename.ext or just foldername without path
               U  in arrays, it forces uniqueness (no duplicates)

Author: VikingOSX, 2022-08-10, Apple Support Communities, No warranties expressed or implied.
COMMENT

# initialize arrays
typeset -ga f1=() f2=() common=() preface=()

# the two main folders are provided as arguments to the script and made absolute paths
# can use tilde paths or just plain folder names if in the current directory
FOLDER_1="${1:a:s/\~\//}"
FOLDER_2="${2:a:s/\~\//}"

function new_finder_tab () {
    /usr/bin/osascript 1> /dev/null <<AS
    use scripting additions

    tell application "Finder"
        set fullpath to POSIX file "${1}"

        tell application "System Events" to tell process "Finder"
        set frontmost to true
        -- new Finder window tab
        keystroke "t" using command down
        end tell
        -- the folder to be assigned to the new tab
        set the target of front Finder window to fullpath
        -- set toolbar visible of front Finder window to true
        -- set pathbar visible of front Finder window to true
    end tell
AS
}

# arrays of alpha sorted sub-folder names found in respective first level folders
f1=( ${FOLDER_1}/*(N/on:t) )
f2=( ${FOLDER_2}/*(N/on:t) )

# perform array intersection to discover unique (u) common folder names
common=( ${(u)${f1:*f2}} )

# prepend the FOLDER_2 path to every array element
preface=( ${FOLDER_2}/$^common )

# just loop and open new Finder window tabs with every matching folder name
for f in ${preface};
do
    new_finder_tab "${f}"
done

# clean up
unset f1 f2 common preface
exit 0


The finished Automator application looks like the following:




This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Find duplicate folder name and open it

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.