Relative path in Automator

applescriptautomator

I want to make an Automator action that copies a set of files from a folder to another folder that is one folder up and then two folders down from the original files. In other words: ../Folder1/Folder2. The Automator action would be in the same folder as the files, but the root folder will be duplicated and renamed across multiple computers, so the path MUST be relative.

My understanding is that I need to run an Applescript to grab the current path and then do…something. I have zero scripting experience. Can anyone help?

enter image description here

Best Answer

Instead of AppleScript, you could do it by adding a Run Shell Script module with this content:

for F in "$@"; do
    cp "$F" "${F%/*/*}/Folder1/Folder2/${F##*/}"
done 

Edit the names of Folder1/Folder2 to match your actual case.

Choose As arguments from the Pass Input: pop-up at right where it says to stdin.

$@ stores the full path of all files selected. $F is each individual file's path as it is processed by the for loop. The weird ${F%/*/*} is the full path of the file minus the file name and its parent folder name. The weird ${F##*/} is the file name itself.

Test first for safety!

Image of script

The other way to do it, which I think would be easier in the long run:

  1. Open a terminal window.
  2. Type cdspace and drag the folder containing your files into the window. (This will paste in the path to that folder. Be sure to include the space after cd before you drag-and-drop.)
  3. Type cp *Video* ../Folder1/Folder2
  4. Done!