Search code examples
applescriptautomator

Mac Copy directory to any directory created in another directory


So I have a directory which users copy folders into. I'd like to setup an automated way of copying a predefined directory into any folder added to the original root folder.

to explain what I mean:

I have a root folder /Root. Users can copy any folder they like into /Root. When they do, I'd like to copy the /Addins/Images folder into the /Root/<user created folder>/ directory automatically. Result should be /Root/<user created folder>/Images - including all sub directories and files in the /Addins/Images folder.

I've heard automator might be able to do such a thing but have no idea how to use it. Any/all help appreciated.


Solution

  • You can do this with Automator or Applescript, using action folder.

    The Applescript bellow copy the folder Images (located in Addins) to any new folder added to MyRoot folder.

    on adding folder items to myRoot after receiving FileList
    set Addins to POSIX path of "Users:your_path:Addins:Images"
    set Folderkind to "folder" -- value depends of your system language
    tell application "Finder" -- loop on each item aded into the folder
    repeat with one_Item in FileList
        if (kind of one_Item) is Folderkind then -- user has added a folder
            do shell script "cp -R " & (quoted form of Addins) & " " & quoted form of (POSIX path of one_Item)
        end if
    end repeat -- end loop on each added item
    end tell
    end adding folder items to
    

    You must change the path to your folder Addins/Images (line 2).

    Once compiled in Applescript Editor, you save this script in ~library/Scripts/Folder Action Scripts. Then, in Finder, select your folder "/root" and right click to add folder action on it (this folder action !). Then, each time a folder will be added in this folder, the script will run.(because of "if" row 6, it will only run when a folder is added, not a simple file)

    Warning: I am concerned about the /root folder you are using and I hope it is not the real root folder, but just an example. If not you will have authorization issues with real root folder.