How can I copy one file to multiple folders, at same time?

A few years back - a thread for this was really useful:


How can I copy one file to multiple folde… - Apple Community


But I think the code is now out-of-date?


Does anyone know how to re-write the script in the thread so it works under Sequoia - I have tried using it as a Quick Action - but it fails to place any files into the folders?


Here is a brief overview of the reply by VikingOSX (VikingOSX’s Profile - Apple Community) :


One launches Automator, clicks New Document at Desktop, selects either Service, or Quick Action depending on the operating system, and then clicks Choose. This will present a workflow whose heading should be configured as:



Next, you visit the Utilities Library in the left panel, and drag and drop the Run Shell Script action onto the large workflow window. It should be configured to look like this:



Select the contents and remove them, as they will be replaced with the following code that you copy/paste into that action:



#!/bin/zsh

: <<'COMMENT'

Reference: https://discussions.apple.com/thread/253031808

Script to receive a Finder selected filename as a first argument followed by one
or more folders where the file is to be copied. Displays an AppleScript dialog
showing the copied filename, and the sorted list of recipient folders.

Usage[1] file2nfolder.zsh ./filename ./folder1 ./folder2 ./foldern
Usage[2] An Automator Service or Quick Action with a Zsh shell and arguments
         In the Finder, select the filename first, followed by one or more folders

Assumption: file to copy and folders are in the same directory
Tested: macOS 11.5.1; zsh v5.8; macOS 10.13.6, zsh v5.3; macOS 10.14.6, zsh v5.3
VikingOSX, 2021-08-09, Apple Support Communities

COMMENT
# initialize two arrays
typeset -a ARGS=() DIRS=()

function show_results () {
    osascript <<-AS
    use scripting additions

    set afile to "${1}"
    set afolders to "${2}"
    set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
    set adirs to (words of afolders) as text
    set AppleScript's text item delimiters to TID

    set aformat to "Copied:  " & afile & return & return & "To Folders:" & return & return & adirs
    display dialog aformat with title "Copy of single file to multiple folders"
AS
    return
}

# first argument is a file and there must be at least one folder
[[ $# -ge 2 ]] && [[ -e "${1}" ]] || { osascript -e 'display dialog "You must select a file to copy and at least one folder."';exit 1 }


OS_VERS=$(/usr/bin/sw_vers -productVersion | cut -d. -f1-2)

ARGS=( "$@" )

if [[ $OS_VERS -lt "10.14" ]]; then
    # High Sierra Finder/Automator reverses selection order. A file selected first is last.
    FILE="${@[-1]}"
    # all but the last argument which is a filename
    DIRS=( "${ARGS[@]:0:$((${#ARGS} - 1))}" )
else
    # Mojave and later maintain Finder/Automator selection order. A file appears first in arguments.
    FILE="${@[1]}"
    DIRS=( "${ARGS[@]:1:${#ARGS}}" )
fi

# two variations on how to copy one file into n-tuple folders
# find "${DIRS}" -maxdepth 0 -exec cp -a "${FILE}" {} \;
/usr/bin/xargs -n 1 cp -a "${FILE:a}" <<<"${DIRS}"

# uses the -o print option to sort the array elements ascending
show_results "${FILE:t}" "$(print -o "${DIRS[@]:t}")"
unset ARGS DIRS
exit 0



Save the Automator Service or Quick Action and give it a meaningful, but short name (e.g. File to n-tuple Folders). Automator will save Service and Quick Actions in the same location, your /Users/username/Library/Services folder. Quit Automator.


Open a FInder window to the folder containing the file and the folders it is to be copied too. Select the filename first, and then the folders by holding down the shift or command key so that you have the file and all folders selected.


On High Sierra, where there are no Quick Actions, you want to right-click on the selected filename, and from the contextual menu, choose Service > File to n-tuple Folders. This will run the Automator Service and when done, you should have a copy of your file in each of the selected folders. On Mojave and later, where Quick Actions have been implemented in the operating system, your also right-click on the selected file, and from the contextual menu, select Quick Actions > File to n-tuple Folders. Same result.


The script will produce a dialog:




iMac Pro, macOS 15.5

Posted on Jul 16, 2025 9:48 AM

Reply
10 replies

Jul 16, 2025 10:30 AM in response to mtbmark

Without any changes, the original code in an Automator Quick Action works for me on Sequoia v15.5.



Here, I chose the file first, then using the cmd-key, selected three folders on my Desktop as targets for the copy. I then right-clicked on the file icon and chose Quick Actions > Copy 1 File to n-Tuple folders.


If I find time, I may update this to an Apple Shortcut Quick Action.

Jul 17, 2025 6:40 AM in response to mtbmark

Ok. I have made some syntax adjustments and it tests fine here on Sequoia v15.5 — both in sending one file to n-tuple folders, but also the final display of those folders affected.



The tilde (~) is UNIX shorthand for /Users/username and improves on the limited horizontal space of this dialog.


The (revised) Run Shell Script contents for Automator:


#!/bin/zsh

: <<'COMMENT'

Reference: https://discussions.apple.com/thread/253031808

Script to receive a Finder selected filename as a first argument followed by one
or more folders where the file is to be copied. Displays an AppleScript dialog
showing the copied filename, and the sorted list of recipient folders.

Usage[1] file2nfolder.zsh ./filename ./folder1 ./folder2 ./foldern
Usage[2] An Automator Service or Quick Action with a Zsh shell and arguments
         In the Finder, select the filename first, followed by one or more folders

Assumption: file to copy and folders are in the same directory
Tested: macOS 11.5.1; zsh v5.8; macOS 10.13.6, zsh v5.3; macOS 10.14.6, zsh v5.3
Tested: macOS Sequoia v15.5
VikingOSX, 2021-08-09, Apple Support Communities
VikingOSX, revision (2025-07-17, Apple Support Communities

COMMENT
# initialize two arrays
typeset -a ARGS=() DIRS=()

function show_results () {
    osascript <<-AS
    use scripting additions

    set afile to "${1}"
	-- note that this is a string of folders separated by white space
    set afolders to "${2}"

    set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {return, " "}}
    set adirs to (text items of afolders) as list

    set aformat to "Copied:  " & afile & return & return & "To Folders:" & return & return & (items of adirs) as text
    display dialog aformat with title "Copy of single file to multiple folders"
    set AppleScript's text item delimiters to TID
AS
    return
}

# first argument is a file and there must be at least one folder
[[ $# -ge 2 ]] && [[ -e "${1}" ]] || { osascript -e 'display dialog "You must select a file to copy and at least one folder."';exit 1 }


OS_VERS=$(/usr/bin/sw_vers -productVersion | cut -d. -f1-2)

ARGS=( "${@}" )

if [[ $OS_VERS -lt "10.14" ]]; then
    # High Sierra Finder/Automator reverses selection order. A file selected first is last.
    FILE="${@[-1]}"
    # all but the last argument which is a filename
    DIRS=( "${ARGS[@]:0:$((${#ARGS} - 1))}" )
else
    # Mojave and later maintain Finder/Automator selection order. A file appears first in arguments.
    FILE="${@[1]}"
	-- use (o) to sort array of dirs alphabetically
    DIRS=( "${(o)ARGS[@]:1:${#ARGS}}" )
fi

# two variations on how to copy one file into n-tuple folders
# find "${(@F)DIRS}" -maxdepth 0 -exec cp -a "${FILE}" {} \;
/usr/bin/xargs -n 1 cp -a "${FILE:a}" <<<"${DIRS}"

-- the print -Dr -- sends the tilde dirs separated by a space as a text string to AppleScript
-- if you want full directory paths, then omit the -D switch
show_results "$(print -D "${FILE:a}")" "$(print -Dr -- "${DIRS[@]}")"
unset ARGS DIRS
exit 0



Ironically, I wrote this in a much shorter AppleScript that worked in Script Editor, but the same exact code failed without error in Automator or a Shortcuts implementation. Hard to debug without any error indication.

Jul 18, 2025 8:47 AM in response to mtbmark

Here is an Automator solution that is written in AppleScript-Objective-C that handles regular folder names as well as folder names with spaces. Less code, less arcane syntax than the previous Zsh shell coding exercise. Still same output with sorted folder names regardless of their order of selection in the Finder:



The AppleScript source that replaces the default content of a Run AppleScript action in Automator:

use framework "Foundation"
use scripting additions

property NSMutableArray : a reference to current application's NSMutableArray
property NSString : a reference to current application's NSString
property NSSortDescriptor : a reference to current application's NSSortDescriptor


on run {input, parameters}
	
	set folderList to {}
	
	tell application "Finder"
		set theStuff to input
		-- not a folder
		if not my is_folder(item 1 of theStuff) then
			set outfile to item 1 of theStuff
		end if
		repeat with f in rest of theStuff
			repeat 1 times
				if my is_folder(f) is true then
					copy f as alias to the end of folderList
				else
					-- if not a folder
					exit repeat
				end if
			end repeat
		end repeat
		
		repeat with i from 1 to count of folderList
			duplicate outfile to (item i of folderList) with replacing and exact copy
		end repeat
		set outfile to POSIX path of (outfile as text)
		set poutfile to (NSString's stringWithString:outfile)'s stringByAbbreviatingWithTildeInPath()
		
		try
			display dialog "File Copied: " & poutfile & return & return & "To: " & return & return & my tilde_folders(folderList) with title "Copy one file to n-tuple selected folders"
		end try
		
	end tell
	
	return input
end run

on is_folder(arg)
	-- returns false if not folder
	-- because System Events will return false if testing for a package folder on a folder
	return (folder of (info for (arg as alias)))
end is_folder

on tilde_folders(alist)
	-- process list of folder paths to their alphabetically ascending POSIX tilde equivalents
	set ascending to NSSortDescriptor's alloc()'s initWithKey:(missing value) ascending:true
	set nsm to NSMutableArray's array()
	repeat with anItem in alist
		set thisFile to POSIX path of (anItem as text)
		(nsm's addObject:((NSString's stringWithString:thisFile)'s stringByAbbreviatingWithTildeInPath()))
	end repeat
	-- because it is a mutable Array, we can sort ascending in place
	nsm's sortUsingDescriptors:{ascending}
	-- a text string with one item per line
	return (nsm's componentsJoinedByString:return) as text
end tilde_folders



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.

How can I copy one file to multiple folders, at same time?

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